private renderProcedure()

in src/languages/java.ts [765:818]


  private renderProcedure(
    node: ts.ConstructorDeclaration | ts.MethodDeclaration | ts.FunctionDeclaration,
    renderer: JavaRenderer,
    methodOrConstructorName: ts.Node | undefined,
    returnType: string | undefined,
  ): OTree {
    const overloads = new Array<OTree>();
    for (let i = 0; i < node.parameters.length; i++) {
      const param = node.parameters[i];
      if (!!param.questionToken || !!param.initializer) {
        // The parameter is either optional, or has a default -
        // render an overload that delegates to a version with one more parameter.
        // Note that we don't check that all parameters with indexes > i are also optional/have a default -
        // we assume the TypeScript compiler does that for us.

        // parameters up to but excluding the current one
        const parametersUpToIth = node.parameters.slice(0, i);
        // how should the call to the next overload look
        const callExpr = ts.isConstructorDeclaration(node) ? 'this' : renderer.convert(methodOrConstructorName);

        overloads.push(
          this.renderOverload(
            returnType,
            renderer,
            methodOrConstructorName,
            parametersUpToIth,
            // the body is the call to the next overload
            this.renderBlock([
              new OTree(
                ['\n', callExpr, '('],
                [
                  ...parametersUpToIth.map((p) => renderer.convert(p.name)),
                  param.initializer ? renderer.convert(param.initializer) : 'null',
                ],
                {
                  separator: ', ',
                  suffix: ');',
                },
              ),
            ]),
          ),
        );
      }
    }
    // render the primary overload
    overloads.push(
      this.renderOverload(returnType, renderer, methodOrConstructorName, node.parameters, renderer.convert(node.body)),
    );

    return new OTree([], overloads, {
      canBreakLine: true,
      separator: '\n\n',
    });
  }