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

Function generateImport

packages/language-service/src/utils/ts_utils.ts:619–647  ·  view source on GitHub ↗
(
  localName: string,
  exportedSpecifierName: string | null,
  rawModuleSpecifier: string,
  useSingleQuotes = false,
)

Source from the content-addressed store, hash-verified

617 * be omitted.
618 */
619export function generateImport(
620 localName: string,
621 exportedSpecifierName: string | null,
622 rawModuleSpecifier: string,
623 useSingleQuotes = false,
624): ts.ImportDeclaration {
625 let propName: ts.Identifier | undefined;
626 if (exportedSpecifierName !== null && exportedSpecifierName !== localName) {
627 propName = ts.factory.createIdentifier(exportedSpecifierName);
628 }
629 const name = ts.factory.createIdentifier(localName);
630 const moduleSpec = ts.factory.createStringLiteral(rawModuleSpecifier, useSingleQuotes);
631 let importClauseName: ts.Identifier | undefined;
632 let importBindings: ts.NamedImportBindings | undefined;
633
634 if (localName === 'default' && exportedSpecifierName !== null) {
635 importClauseName = ts.factory.createIdentifier(exportedSpecifierName);
636 } else {
637 importBindings = ts.factory.createNamedImports([
638 ts.factory.createImportSpecifier(false, propName, name),
639 ]);
640 }
641 return ts.factory.createImportDeclaration(
642 undefined,
643 ts.factory.createImportClause(undefined, importClauseName, importBindings),
644 moduleSpec,
645 undefined,
646 );
647}
648
649/**
650 * 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