public override methodDeclaration()

in src/languages/go.ts [783:819]


  public override methodDeclaration(node: ts.MethodDeclaration, renderer: AstRenderer<GoLanguageContext>): OTree {
    if (ts.isObjectLiteralExpression(node.parent)) {
      return super.methodDeclaration(node, renderer);
    }

    const className = node.parent.name
      ? this.goName(
          node.parent.name.text,
          renderer.updateContext({ isExported: isExported(node.parent) }),
          renderer.typeChecker.getSymbolAtLocation(node.parent.name),
        )
      : 'anonymous';

    const returnType = determineReturnType(renderer.typeChecker, node);
    const goReturnType =
      returnType && this.renderType(node.type ?? node, returnType.symbol, returnType, true, 'interface{}', renderer);

    return new OTree(
      [
        'func (this *',
        className,
        ') ',
        renderer.updateContext({ isExported: renderer.currentContext.isExported && isPublic(node) }).convert(node.name),
        '(',
        new OTree([], renderer.convertAll(node.parameters), { separator: ', ' }),
        ') ',
        goReturnType,
        goReturnType ? ' ' : '',
        '{',
      ],
      [
        this.defaultArgValues(node.parameters, renderer),
        ...(node.body ? renderer.convertAll(node.body.statements) : []),
      ],
      { canBreakLine: true, suffix: node.body && node.body.statements.length > 0 ? '\n}' : '}', indent: 1 },
    );
  }