|
| 1 | +/* @internal */ |
| 2 | +namespace ts.refactor.generateGetAccessorAndSetAccessor { |
| 3 | + const refactorName = "Convert import"; |
| 4 | + const actionNameNamespaceToNamed = "Convert namespace import to named imports"; |
| 5 | + const actionNameNamedToNamespace = "Convert named imports to namespace import"; |
| 6 | + registerRefactor(refactorName, { |
| 7 | + getAvailableActions(context): ApplicableRefactorInfo[] | undefined { |
| 8 | + const i = getImportToConvert(context); |
| 9 | + if (!i) return undefined; |
| 10 | + const description = i.kind === SyntaxKind.NamespaceImport ? Diagnostics.Convert_namespace_import_to_named_imports.message : Diagnostics.Convert_named_imports_to_namespace_import.message; |
| 11 | + const actionName = i.kind === SyntaxKind.NamespaceImport ? actionNameNamespaceToNamed : actionNameNamedToNamespace; |
| 12 | + return [{ name: refactorName, description, actions: [{ name: actionName, description }] }]; |
| 13 | + }, |
| 14 | + getEditsForAction(context, actionName): RefactorEditInfo { |
| 15 | + Debug.assert(actionName === actionNameNamespaceToNamed || actionName === actionNameNamedToNamespace); |
| 16 | + const edits = textChanges.ChangeTracker.with(context, t => doChange(context.file, context.program, t, Debug.assertDefined(getImportToConvert(context)))); |
| 17 | + return { edits, renameFilename: undefined, renameLocation: undefined }; |
| 18 | + } |
| 19 | + }); |
| 20 | + |
| 21 | + // Can convert imports of the form `import * as m from "m";` or `import d, { x, y } from "m";`. |
| 22 | + function getImportToConvert(context: RefactorContext): NamedImportBindings | undefined { |
| 23 | + const { file } = context; |
| 24 | + const span = getRefactorContextSpan(context); |
| 25 | + const token = getTokenAtPosition(file, span.start, /*includeJsDocComment*/ false); |
| 26 | + const importDecl = getParentNodeInSpan(token, file, span); |
| 27 | + if (!importDecl || !isImportDeclaration(importDecl)) return undefined; |
| 28 | + const { importClause } = importDecl; |
| 29 | + return importClause && importClause.namedBindings; |
| 30 | + } |
| 31 | + |
| 32 | + function doChange(sourceFile: SourceFile, program: Program, changes: textChanges.ChangeTracker, toConvert: NamedImportBindings): void { |
| 33 | + const checker = program.getTypeChecker(); |
| 34 | + if (toConvert.kind === SyntaxKind.NamespaceImport) { |
| 35 | + doChangeNamespaceToNamed(sourceFile, checker, changes, toConvert, getAllowSyntheticDefaultImports(program.getCompilerOptions())); |
| 36 | + } |
| 37 | + else { |
| 38 | + doChangeNamedToNamespace(sourceFile, checker, changes, toConvert); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + function doChangeNamespaceToNamed(sourceFile: SourceFile, checker: TypeChecker, changes: textChanges.ChangeTracker, toConvert: NamespaceImport, allowSyntheticDefaultImports: boolean): void { |
| 43 | + let usedAsNamespaceOrDefault = false; |
| 44 | + |
| 45 | + const nodesToReplace: PropertyAccessExpression[] = []; |
| 46 | + const conflictingNames = createMap<true>(); |
| 47 | + |
| 48 | + FindAllReferences.Core.eachSymbolReferenceInFile(toConvert.name, checker, sourceFile, id => { |
| 49 | + if (!isPropertyAccessExpression(id.parent)) { |
| 50 | + usedAsNamespaceOrDefault = true; |
| 51 | + } |
| 52 | + else { |
| 53 | + const parent = cast(id.parent, isPropertyAccessExpression); |
| 54 | + const exportName = parent.name.text; |
| 55 | + if (checker.resolveName(exportName, id, SymbolFlags.All, /*excludeGlobals*/ true)) { |
| 56 | + conflictingNames.set(exportName, true); |
| 57 | + } |
| 58 | + Debug.assert(parent.expression === id); |
| 59 | + nodesToReplace.push(parent); |
| 60 | + } |
| 61 | + }); |
| 62 | + |
| 63 | + // We may need to change `mod.x` to `_x` to avoid a name conflict. |
| 64 | + const exportNameToImportName = createMap<string>(); |
| 65 | + |
| 66 | + for (const propertyAccess of nodesToReplace) { |
| 67 | + const exportName = propertyAccess.name.text; |
| 68 | + let importName = exportNameToImportName.get(exportName); |
| 69 | + if (importName === undefined) { |
| 70 | + exportNameToImportName.set(exportName, importName = conflictingNames.has(exportName) ? getUniqueName(exportName, sourceFile) : exportName); |
| 71 | + } |
| 72 | + changes.replaceNode(sourceFile, propertyAccess, createIdentifier(importName)); |
| 73 | + } |
| 74 | + |
| 75 | + const importSpecifiers: ImportSpecifier[] = []; |
| 76 | + exportNameToImportName.forEach((name, propertyName) => { |
| 77 | + importSpecifiers.push(createImportSpecifier(name === propertyName ? undefined : createIdentifier(propertyName), createIdentifier(name))); |
| 78 | + }); |
| 79 | + |
| 80 | + const importDecl = toConvert.parent.parent; |
| 81 | + if (usedAsNamespaceOrDefault && !allowSyntheticDefaultImports) { |
| 82 | + // Need to leave the namespace import alone |
| 83 | + changes.insertNodeAfter(sourceFile, importDecl, updateImport(importDecl, /*defaultImportName*/ undefined, importSpecifiers)); |
| 84 | + } |
| 85 | + else { |
| 86 | + changes.replaceNode(sourceFile, importDecl, updateImport(importDecl, usedAsNamespaceOrDefault ? createIdentifier(toConvert.name.text) : undefined, importSpecifiers)); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + function doChangeNamedToNamespace(sourceFile: SourceFile, checker: TypeChecker, changes: textChanges.ChangeTracker, toConvert: NamedImports): void { |
| 91 | + const importDecl = toConvert.parent.parent; |
| 92 | + const { moduleSpecifier } = importDecl; |
| 93 | + |
| 94 | + const preferredName = moduleSpecifier && isStringLiteral(moduleSpecifier) ? codefix.moduleSpecifierToValidIdentifier(moduleSpecifier.text, ScriptTarget.ESNext) : "module"; |
| 95 | + const namespaceNameConflicts = toConvert.elements.some(element => |
| 96 | + FindAllReferences.Core.eachSymbolReferenceInFile(element.name, checker, sourceFile, id => |
| 97 | + !!checker.resolveName(preferredName, id, SymbolFlags.All, /*excludeGlobals*/ true)) || false); |
| 98 | + const namespaceImportName = namespaceNameConflicts ? getUniqueName(preferredName, sourceFile) : preferredName; |
| 99 | + |
| 100 | + const neededNamedImports: ImportSpecifier[] = []; |
| 101 | + |
| 102 | + for (const element of toConvert.elements) { |
| 103 | + const propertyName = (element.propertyName || element.name).text; |
| 104 | + FindAllReferences.Core.eachSymbolReferenceInFile(element.name, checker, sourceFile, id => { |
| 105 | + const access = createPropertyAccess(createIdentifier(namespaceImportName), propertyName); |
| 106 | + if (isShorthandPropertyAssignment(id.parent)) { |
| 107 | + changes.replaceNode(sourceFile, id.parent, createPropertyAssignment(id.text, access)); |
| 108 | + } |
| 109 | + else if (isExportSpecifier(id.parent) && !id.parent.propertyName) { |
| 110 | + if (!neededNamedImports.some(n => n.name === element.name)) { |
| 111 | + neededNamedImports.push(createImportSpecifier(element.propertyName && createIdentifier(element.propertyName.text), createIdentifier(element.name.text))); |
| 112 | + } |
| 113 | + } |
| 114 | + else { |
| 115 | + changes.replaceNode(sourceFile, id, access); |
| 116 | + } |
| 117 | + }); |
| 118 | + } |
| 119 | + |
| 120 | + changes.replaceNode(sourceFile, toConvert, createNamespaceImport(createIdentifier(namespaceImportName))); |
| 121 | + if (neededNamedImports.length) { |
| 122 | + changes.insertNodeAfter(sourceFile, toConvert.parent.parent, updateImport(importDecl, /*defaultImportName*/ undefined, neededNamedImports)); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + function updateImport(old: ImportDeclaration, defaultImportName: Identifier | undefined, elements: ReadonlyArray<ImportSpecifier> | undefined): ImportDeclaration { |
| 127 | + return createImportDeclaration(/*decorators*/ undefined, /*modifiers*/ undefined, |
| 128 | + createImportClause(defaultImportName, elements && elements.length ? createNamedImports(elements) : undefined), old.moduleSpecifier); |
| 129 | + } |
| 130 | +} |
0 commit comments