MCPcopy Create free account
hub / github.com/angular/angular / updateImport

Function updateImport

packages/language-service/src/utils/ts_utils.ts:656–694  ·  view source on GitHub ↗
(
  importDeclaration: ts.ImportDeclaration,
  localName: string,
  exportedSpecifierName: string | null,
)

Source from the content-addressed store, hash-verified

654 * is used as the default import name.
655 */
656export function updateImport(
657 importDeclaration: ts.ImportDeclaration,
658 localName: string,
659 exportedSpecifierName: string | null,
660): ts.ImportClause | undefined {
661 const importClause = importDeclaration.importClause;
662 if (importClause === undefined) {
663 return undefined;
664 }
665 const bindings = importClause.namedBindings;
666 if (bindings !== undefined && ts.isNamespaceImport(bindings)) {
667 // This should be impossible. If a namespace import is present, the symbol was already
668 // considered imported above.
669 console.error(`Unexpected namespace import ${importDeclaration.getText()}`);
670 return undefined;
671 }
672 if (localName === 'default' && exportedSpecifierName !== null) {
673 const importClauseName = ts.factory.createIdentifier(exportedSpecifierName);
674 return ts.factory.updateImportClause(
675 importClause,
676 false,
677 importClauseName,
678 importClause.namedBindings,
679 );
680 }
681 let propertyName: ts.Identifier | undefined;
682 if (exportedSpecifierName !== null && exportedSpecifierName !== localName) {
683 propertyName = ts.factory.createIdentifier(exportedSpecifierName);
684 }
685 const name = ts.factory.createIdentifier(localName);
686 const newImport = ts.factory.createImportSpecifier(false, propertyName, name);
687 let namedImport: ts.NamedImports;
688 if (bindings === undefined) {
689 namedImport = ts.factory.createNamedImports([newImport]);
690 } else {
691 namedImport = ts.factory.updateNamedImports(bindings, [...bindings.elements, newImport]);
692 }
693 return ts.factory.updateImportClause(importClause, false, importClause.name, namedImport);
694}
695
696let printer: ts.Printer | null = null;
697

Callers 2

ts_utils_spec.tsFile · 0.90

Calls 3

getTextMethod · 0.80
createIdentifierMethod · 0.80
errorMethod · 0.65

Tested by

no test coverage detected