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

Function generateImport

packages/language-service/src/utils/ts_utils.ts:589–616  ·  view source on GitHub ↗
(
  localName: string,
  exportedSpecifierName: string | null,
  rawModuleSpecifier: string,
)

Source from the content-addressed store, hash-verified

587 * be omitted.
588 */
589export function generateImport(
590 localName: string,
591 exportedSpecifierName: string | null,
592 rawModuleSpecifier: string,
593): ts.ImportDeclaration {
594 let propName: ts.Identifier | undefined;
595 if (exportedSpecifierName !== null && exportedSpecifierName !== localName) {
596 propName = ts.factory.createIdentifier(exportedSpecifierName);
597 }
598 const name = ts.factory.createIdentifier(localName);
599 const moduleSpec = ts.factory.createStringLiteral(rawModuleSpecifier);
600 let importClauseName: ts.Identifier | undefined;
601 let importBindings: ts.NamedImportBindings | undefined;
602
603 if (localName === 'default' && exportedSpecifierName !== null) {
604 importClauseName = ts.factory.createIdentifier(exportedSpecifierName);
605 } else {
606 importBindings = ts.factory.createNamedImports([
607 ts.factory.createImportSpecifier(false, propName, name),
608 ]);
609 }
610 return ts.factory.createImportDeclaration(
611 undefined,
612 ts.factory.createImportClause(undefined, importClauseName, importBindings),
613 moduleSpec,
614 undefined,
615 );
616}
617
618/**
619 * Update an existing named import with a new member.

Callers 2

ts_utils_spec.tsFile · 0.90

Calls 1

createIdentifierMethod · 0.80

Tested by

no test coverage detected