|
| 1 | +/* @internal */ |
| 2 | +namespace ts.refactor.installTypesForPackage { |
| 3 | + const actionName = "Convert to default import"; |
| 4 | + |
| 5 | + const useDefaultImport: Refactor = { |
| 6 | + name: actionName, |
| 7 | + description: getLocaleSpecificMessage(Diagnostics.Convert_to_default_import), |
| 8 | + getEditsForAction, |
| 9 | + getAvailableActions, |
| 10 | + }; |
| 11 | + |
| 12 | + registerRefactor(useDefaultImport); |
| 13 | + |
| 14 | + function getAvailableActions(context: RefactorContext): ApplicableRefactorInfo[] | undefined { |
| 15 | + const { file, startPosition, program } = context; |
| 16 | + |
| 17 | + if (!program.getCompilerOptions().allowSyntheticDefaultImports) { |
| 18 | + return undefined; |
| 19 | + } |
| 20 | + |
| 21 | + const importInfo = getConvertibleImportAtPosition(file, startPosition); |
| 22 | + if (!importInfo) { |
| 23 | + return undefined; |
| 24 | + } |
| 25 | + |
| 26 | + const module = ts.getResolvedModule(file, importInfo.moduleSpecifier.text); |
| 27 | + const resolvedFile = program.getSourceFile(module.resolvedFileName); |
| 28 | + if (!(resolvedFile.externalModuleIndicator && isExportAssignment(resolvedFile.externalModuleIndicator) && resolvedFile.externalModuleIndicator.isExportEquals)) { |
| 29 | + return undefined; |
| 30 | + } |
| 31 | + |
| 32 | + return [ |
| 33 | + { |
| 34 | + name: useDefaultImport.name, |
| 35 | + description: useDefaultImport.description, |
| 36 | + actions: [ |
| 37 | + { |
| 38 | + description: useDefaultImport.description, |
| 39 | + name: actionName, |
| 40 | + }, |
| 41 | + ], |
| 42 | + }, |
| 43 | + ]; |
| 44 | + } |
| 45 | + |
| 46 | + function getEditsForAction(context: RefactorContext, _actionName: string): RefactorEditInfo | undefined { |
| 47 | + const { file, startPosition } = context; |
| 48 | + Debug.assertEqual(actionName, _actionName); |
| 49 | + const importInfo = getConvertibleImportAtPosition(file, startPosition); |
| 50 | + if (!importInfo) { |
| 51 | + return undefined; |
| 52 | + } |
| 53 | + const { importStatement, name, moduleSpecifier } = importInfo; |
| 54 | + const newImportClause = createImportClause(name, /*namedBindings*/ undefined); |
| 55 | + const newImportStatement = ts.createImportDeclaration(/*decorators*/ undefined, /*modifiers*/ undefined, newImportClause, moduleSpecifier); |
| 56 | + return { |
| 57 | + edits: textChanges.ChangeTracker.with(context, t => t.replaceNode(file, importStatement, newImportStatement)), |
| 58 | + renameFilename: undefined, |
| 59 | + renameLocation: undefined, |
| 60 | + }; |
| 61 | + } |
| 62 | + |
| 63 | + function getConvertibleImportAtPosition( |
| 64 | + file: SourceFile, |
| 65 | + startPosition: number, |
| 66 | + ): { importStatement: AnyImportSyntax, name: Identifier, moduleSpecifier: StringLiteral } | undefined { |
| 67 | + let node = getTokenAtPosition(file, startPosition, /*includeJsDocComment*/ false); |
| 68 | + while (true) { |
| 69 | + switch (node.kind) { |
| 70 | + case SyntaxKind.ImportEqualsDeclaration: |
| 71 | + const eq = node as ImportEqualsDeclaration; |
| 72 | + const { moduleReference } = eq; |
| 73 | + return moduleReference.kind === SyntaxKind.ExternalModuleReference && isStringLiteral(moduleReference.expression) |
| 74 | + ? { importStatement: eq, name: eq.name, moduleSpecifier: moduleReference.expression } |
| 75 | + : undefined; |
| 76 | + case SyntaxKind.ImportDeclaration: |
| 77 | + const d = node as ImportDeclaration; |
| 78 | + const { importClause } = d; |
| 79 | + return !importClause.name && importClause.namedBindings.kind === SyntaxKind.NamespaceImport && isStringLiteral(d.moduleSpecifier) |
| 80 | + ? { importStatement: d, name: importClause.namedBindings.name, moduleSpecifier: d.moduleSpecifier } |
| 81 | + : undefined; |
| 82 | + // For known child node kinds of convertible imports, try again with parent node. |
| 83 | + case SyntaxKind.NamespaceImport: |
| 84 | + case SyntaxKind.ExternalModuleReference: |
| 85 | + case SyntaxKind.ImportKeyword: |
| 86 | + case SyntaxKind.Identifier: |
| 87 | + case SyntaxKind.StringLiteral: |
| 88 | + case SyntaxKind.AsteriskToken: |
| 89 | + break; |
| 90 | + default: |
| 91 | + return undefined; |
| 92 | + } |
| 93 | + node = node.parent; |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments