public override importStatement()

in src/languages/python.ts [168:209]


  public override importStatement(node: ImportStatement, context: PythonVisitorContext): OTree {
    if (node.imports.import === 'full') {
      const moduleName = fmap(node.moduleSymbol, findPythonName) ?? guessPythonPackageName(node.packageName);

      const importName = node.imports.alias ?? node.imports.sourceName;
      this.addImport({
        importedFqn: node.moduleSymbol?.fqn ?? node.packageName,
        importName,
      });

      return new OTree([`import ${moduleName} as ${mangleIdentifier(importName)}`], [], {
        canBreakLine: true,
      });
    }
    if (node.imports.import === 'selective') {
      for (const im of node.imports.elements) {
        if (im.importedSymbol) {
          this.addImport({
            importName: im.alias ? im.alias : im.sourceName,
            importedFqn: im.importedSymbol.fqn,
          });
        }
      }

      const imports = node.imports.elements.map((im) => {
        const localName = im.alias ?? im.sourceName;
        const originalName = fmap(fmap(im.importedSymbol, findPythonName), simpleName) ?? im.sourceName;

        return localName === originalName
          ? mangleIdentifier(originalName)
          : `${mangleIdentifier(originalName)} as ${mangleIdentifier(localName)}`;
      });

      const moduleName = fmap(node.moduleSymbol, findPythonName) ?? guessPythonPackageName(node.packageName);

      return new OTree([`from ${moduleName} import ${imports.join(', ')}`], [], {
        canBreakLine: true,
      });
    }

    return nimpl(node.node, context);
  }