|
| 1 | +import { |
| 2 | + codeFixAll, |
| 3 | + createCodeFixAction, |
| 4 | + registerCodeFix, |
| 5 | +} from "../_namespaces/ts.codefix.js"; |
| 6 | +import { |
| 7 | + Debug, |
| 8 | + Diagnostics, |
| 9 | + factory, |
| 10 | + findAncestor, |
| 11 | + getQuotePreference, |
| 12 | + getTokenAtPosition, |
| 13 | + isImportDeclaration, |
| 14 | + isImportTypeNode, |
| 15 | + LanguageServiceHost, |
| 16 | + ModuleKind, |
| 17 | + or, |
| 18 | + Program, |
| 19 | + QuotePreference, |
| 20 | + resolveModuleName, |
| 21 | + SourceFile, |
| 22 | + SyntaxKind, |
| 23 | + textChanges, |
| 24 | + tryGetModuleSpecifierFromDeclaration, |
| 25 | + UserPreferences, |
| 26 | +} from "../_namespaces/ts.js"; |
| 27 | + |
| 28 | +const fixId = "addMissingResolutionModeImportAttribute"; |
| 29 | +const errorCodes = [ |
| 30 | + Diagnostics.Type_only_import_of_an_ECMAScript_module_from_a_CommonJS_module_must_have_a_resolution_mode_attribute.code, |
| 31 | + Diagnostics.Type_import_of_an_ECMAScript_module_from_a_CommonJS_module_must_have_a_resolution_mode_attribute.code, |
| 32 | +]; |
| 33 | + |
| 34 | +registerCodeFix({ |
| 35 | + errorCodes, |
| 36 | + getCodeActions: function getCodeActionsToAddMissingResolutionModeImportAttribute(context) { |
| 37 | + const changes = textChanges.ChangeTracker.with(context, t => makeChange(t, context.sourceFile, context.span.start, context.program, context.host, context.preferences)); |
| 38 | + return [createCodeFixAction(fixId, changes, Diagnostics.Add_resolution_mode_import_attribute, fixId, Diagnostics.Add_resolution_mode_import_attribute_to_all_type_only_imports_that_need_it)]; |
| 39 | + }, |
| 40 | + fixIds: [fixId], |
| 41 | + getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => makeChange(changes, diag.file, diag.start, context.program, context.host, context.preferences)), |
| 42 | +}); |
| 43 | + |
| 44 | +function makeChange(changeTracker: textChanges.ChangeTracker, sourceFile: SourceFile, pos: number, program: Program, host: LanguageServiceHost, preferences: UserPreferences) { |
| 45 | + const token = getTokenAtPosition(sourceFile, pos); |
| 46 | + const importNode = findAncestor(token, or(isImportDeclaration, isImportTypeNode))!; |
| 47 | + Debug.assert(!!importNode, "Expected position to be owned by an ImportDeclaration or ImportType."); |
| 48 | + const useSingleQuotes = getQuotePreference(sourceFile, preferences) === QuotePreference.Single; |
| 49 | + const moduleSpecifier = tryGetModuleSpecifierFromDeclaration(importNode); |
| 50 | + const canUseImportMode = !moduleSpecifier || (resolveModuleName( |
| 51 | + moduleSpecifier.text, |
| 52 | + sourceFile.fileName, |
| 53 | + program.getCompilerOptions(), |
| 54 | + host, |
| 55 | + program.getModuleResolutionCache(), |
| 56 | + /*redirectedReference*/ undefined, |
| 57 | + ModuleKind.ESNext, |
| 58 | + ).resolvedModule?.resolvedFileName === program.getResolvedModuleFromModuleSpecifier( |
| 59 | + moduleSpecifier, |
| 60 | + sourceFile, |
| 61 | + )?.resolvedModule?.resolvedFileName); |
| 62 | + |
| 63 | + const attributes = importNode.attributes |
| 64 | + ? factory.updateImportAttributes( |
| 65 | + importNode.attributes, |
| 66 | + factory.createNodeArray([ |
| 67 | + ...importNode.attributes.elements, |
| 68 | + factory.createImportAttribute( |
| 69 | + factory.createStringLiteral("resolution-mode", useSingleQuotes), |
| 70 | + factory.createStringLiteral(canUseImportMode ? "import" : "require", useSingleQuotes), |
| 71 | + ), |
| 72 | + ], importNode.attributes.elements.hasTrailingComma), |
| 73 | + importNode.attributes.multiLine, |
| 74 | + ) |
| 75 | + : factory.createImportAttributes( |
| 76 | + factory.createNodeArray([ |
| 77 | + factory.createImportAttribute( |
| 78 | + factory.createStringLiteral("resolution-mode", useSingleQuotes), |
| 79 | + factory.createStringLiteral(canUseImportMode ? "import" : "require", useSingleQuotes), |
| 80 | + ), |
| 81 | + ]), |
| 82 | + ); |
| 83 | + if (importNode.kind === SyntaxKind.ImportDeclaration) { |
| 84 | + changeTracker.replaceNode( |
| 85 | + sourceFile, |
| 86 | + importNode, |
| 87 | + factory.updateImportDeclaration( |
| 88 | + importNode, |
| 89 | + importNode.modifiers, |
| 90 | + importNode.importClause, |
| 91 | + importNode.moduleSpecifier, |
| 92 | + attributes, |
| 93 | + ), |
| 94 | + ); |
| 95 | + } |
| 96 | + else { |
| 97 | + changeTracker.replaceNode( |
| 98 | + sourceFile, |
| 99 | + importNode, |
| 100 | + factory.updateImportTypeNode( |
| 101 | + importNode, |
| 102 | + importNode.argument, |
| 103 | + attributes, |
| 104 | + importNode.qualifier, |
| 105 | + importNode.typeArguments, |
| 106 | + ), |
| 107 | + ); |
| 108 | + } |
| 109 | +} |
0 commit comments