( importDeclaration: ts.ImportDeclaration, localName: string, exportedSpecifierName: string | null, )
| 654 | * is used as the default import name. |
| 655 | */ |
| 656 | export 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 | |
| 696 | let printer: ts.Printer | null = null; |
| 697 |
no test coverage detected