( importDeclaration: ts.ImportDeclaration, localName: string, exportedSpecifierName: string | null, )
| 623 | * is used as the default import name. |
| 624 | */ |
| 625 | export function updateImport( |
| 626 | importDeclaration: ts.ImportDeclaration, |
| 627 | localName: string, |
| 628 | exportedSpecifierName: string | null, |
| 629 | ): ts.ImportClause | undefined { |
| 630 | const importClause = importDeclaration.importClause; |
| 631 | if (importClause === undefined) { |
| 632 | return undefined; |
| 633 | } |
| 634 | const bindings = importClause.namedBindings; |
| 635 | if (bindings !== undefined && ts.isNamespaceImport(bindings)) { |
| 636 | // This should be impossible. If a namespace import is present, the symbol was already |
| 637 | // considered imported above. |
| 638 | console.error(`Unexpected namespace import ${importDeclaration.getText()}`); |
| 639 | return undefined; |
| 640 | } |
| 641 | if (localName === 'default' && exportedSpecifierName !== null) { |
| 642 | const importClauseName = ts.factory.createIdentifier(exportedSpecifierName); |
| 643 | return ts.factory.updateImportClause( |
| 644 | importClause, |
| 645 | false, |
| 646 | importClauseName, |
| 647 | importClause.namedBindings, |
| 648 | ); |
| 649 | } |
| 650 | let propertyName: ts.Identifier | undefined; |
| 651 | if (exportedSpecifierName !== null && exportedSpecifierName !== localName) { |
| 652 | propertyName = ts.factory.createIdentifier(exportedSpecifierName); |
| 653 | } |
| 654 | const name = ts.factory.createIdentifier(localName); |
| 655 | const newImport = ts.factory.createImportSpecifier(false, propertyName, name); |
| 656 | let namedImport: ts.NamedImports; |
| 657 | if (bindings === undefined) { |
| 658 | namedImport = ts.factory.createNamedImports([newImport]); |
| 659 | } else { |
| 660 | namedImport = ts.factory.updateNamedImports(bindings, [...bindings.elements, newImport]); |
| 661 | } |
| 662 | return ts.factory.updateImportClause(importClause, false, importClause.name, namedImport); |
| 663 | } |
| 664 | |
| 665 | let printer: ts.Printer | null = null; |
| 666 |
no test coverage detected