public override newExpression()

in src/languages/go.ts [193:255]


  public override newExpression(node: ts.NewExpression, renderer: GoRenderer): OTree {
    const { classNamespace, className } = determineClassName.call(this, node.expression);
    return new OTree(
      [
        ...(classNamespace ? [classNamespace, '.'] : []),
        'New', // Should this be "new" if the class is unexported?
        className,
        '(',
      ],
      renderer.updateContext({ wrapPtr: true }).convertAll(node.arguments ?? []),
      { canBreakLine: true, separator: ', ', suffix: ')' },
    );

    function determineClassName(this: GoVisitor, expr: ts.Expression): { classNamespace?: OTree; className: string } {
      if (ts.isIdentifier(expr)) {
        // Imported names are referred to by the original (i.e: exported) name, qualified with the source module's go
        // package name.
        const symbol = renderer.typeChecker.getSymbolAtLocation(expr);
        const declaration = symbol?.valueDeclaration ?? symbol?.declarations?.[0];
        if (declaration && ts.isImportSpecifier(declaration)) {
          const importInfo = analyzeImportDeclaration(declaration.parent.parent.parent, renderer);
          const packageName =
            importInfo.moduleSymbol?.sourceAssembly?.packageJson.jsii?.targets?.go?.packageName ??
            this.goName(importInfo.packageName, renderer, undefined);

          return {
            classNamespace: new OTree([packageName]),
            className: this.goName(
              (declaration.propertyName ?? declaration.name).text,
              renderer.updateContext({ isExported: true }),
              symbol,
            ),
          };
        }

        return { className: ucFirst(expr.text) };
      }
      if (ts.isPropertyAccessExpression(expr)) {
        if (ts.isIdentifier(expr.expression)) {
          return {
            className: ucFirst(expr.name.text),
            classNamespace: renderer.updateContext({ isExported: false }).convert(expr.expression),
          };
        } else if (
          ts.isPropertyAccessExpression(expr.expression) &&
          renderer.submoduleReferences.has(expr.expression)
        ) {
          const submodule = renderer.submoduleReferences.get(expr.expression)!;
          return {
            className: ucFirst(expr.name.text),
            classNamespace: renderer.updateContext({ isExported: false }).convert(submodule.lastNode),
          };
        }
        renderer.reportUnsupported(expr.expression, TargetLanguage.GO);
        return {
          className: ucFirst(expr.name.text),
          classNamespace: new OTree(['#error#']),
        };
      }
      renderer.reportUnsupported(expr, TargetLanguage.GO);
      return { className: expr.getText(expr.getSourceFile()) };
    }
  }