private emitExtensionMethodBody()

in lib/declaration.ts [1111:1154]


  private emitExtensionMethodBody({constituents, mergedDeclaration}: MergedMember) {
    // Determine all valid arties of this method by going through the overloaded signatures
    const arities: Set<number> = new Set();
    for (const constituent of constituents) {
      const arity = constituent.parameters.length;
      arities.add(arity);
    }
    const sortedArities = Array.from(arities).sort();
    for (const arity of sortedArities) {
      if (arity < mergedDeclaration.parameters.length) {
        const firstOptionalIndex = arity;
        const suppliedParameters = mergedDeclaration.parameters.slice(0, firstOptionalIndex);
        const omittedParameters = mergedDeclaration.parameters.slice(
            firstOptionalIndex, mergedDeclaration.parameters.length);
        // Emit null checks to verify the number of omitted parameters
        this.emit('if (');
        let isFirst = true;
        for (const omitted of omittedParameters) {
          if (isFirst) {
            isFirst = false;
          } else {
            this.emit('&&');
          }
          this.visit(omitted.name);
          this.emit('== null');
        }
        this.emit(') {');
        this.emit('return promiseToFuture(');
        this.emit(PRIVATE_CLASS_INSTANCE_IN_EXTENSIONS);
        this.emit('.');
        this.visitName(mergedDeclaration.name);
        this.visitParameters(ts.createNodeArray(suppliedParameters), {namesOnly: true});
        this.emit('); }\n');
      } else {
        // No parameters were omitted, no null checks are necessary for this call
        this.emit('return promiseToFuture(');
        this.emit(PRIVATE_CLASS_INSTANCE_IN_EXTENSIONS);
        this.emit('.');
        this.visitName(mergedDeclaration.name);
        this.visitParameters(ts.createNodeArray(mergedDeclaration.parameters), {namesOnly: true});
        this.emit(');\n');
      }
    }
  }