public override importStatement()

in src/languages/csharp.ts [138:181]


  public override importStatement(importStatement: ImportStatement, context: CSharpRenderer): OTree {
    const guessedNamespace = guessDotnetNamespace(importStatement.packageName);
    const namespace = fmap(importStatement.moduleSymbol, findDotnetName) ?? guessedNamespace;

    if (importStatement.imports.import === 'full') {
      this.dropPropertyAccesses.add(importStatement.imports.sourceName);
      this.alreadyImportedNamespaces.add(namespace);
      return new OTree([`using ${namespace};`], [], { canBreakLine: true });
    }
    if (importStatement.imports.import === 'selective') {
      const statements = new Array<string>();

      for (const el of importStatement.imports.elements) {
        const dotnetNs = fmap(el.importedSymbol, findDotnetName) ?? `${guessedNamespace}.${ucFirst(el.sourceName)}`;

        // If this is an alias, we only honor it if it's NOT for sure a module
        // (could be an alias import of a class or enum).
        if (el.alias && el.importedSymbol?.symbolType !== 'module') {
          this.renamedSymbols.set(el.alias, simpleName(dotnetNs));
          statements.push(`using ${ucFirst(el.alias)} = ${dotnetNs};`);
          continue;
        }

        // If we are importing a module directly, drop the occurrences of that
        // identifier further down (turn `mod.MyClass` into `MyClass`).
        if (el.importedSymbol?.symbolType === 'module') {
          this.dropPropertyAccesses.add(el.alias ?? el.sourceName);
        }

        // Output an import statement for the containing namespace
        const importableNamespace = el.importedSymbol?.symbolType === 'module' ? dotnetNs : namespaceName(dotnetNs);
        if (this.alreadyImportedNamespaces.has(importableNamespace)) {
          continue;
        }

        this.alreadyImportedNamespaces.add(importableNamespace);
        statements.push(`using ${importableNamespace};`);
      }

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

    return nimpl(importStatement.node, context);
  }