forked from microsoft/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvertToTypeOnlyImport.ts
More file actions
149 lines (143 loc) · 7.27 KB
/
convertToTypeOnlyImport.ts
File metadata and controls
149 lines (143 loc) · 7.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import {
Diagnostics,
factory,
FindAllReferences,
getSynthesizedDeepClone,
getSynthesizedDeepClones,
getTokenAtPosition,
ImportClause,
ImportDeclaration,
ImportSpecifier,
isImportDeclaration,
isImportSpecifier,
isValidTypeOnlyAliasUseSite,
Program,
sameMap,
some,
SourceFile,
SyntaxKind,
textChanges,
} from "../_namespaces/ts";
import {
codeFixAll,
createCodeFixAction,
createCodeFixActionWithoutFixAll,
registerCodeFix,
} from "../_namespaces/ts.codefix";
const errorCodes = [
Diagnostics._0_is_a_type_and_must_be_imported_using_a_type_only_import_when_verbatimModuleSyntax_is_enabled.code,
Diagnostics._0_resolves_to_a_type_only_declaration_and_must_be_imported_using_a_type_only_import_when_verbatimModuleSyntax_is_enabled.code,
];
const fixId = "convertToTypeOnlyImport";
registerCodeFix({
errorCodes,
getCodeActions: function getCodeActionsToConvertToTypeOnlyImport(context) {
const declaration = getDeclaration(context.sourceFile, context.span.start);
if (declaration) {
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, context.sourceFile, declaration));
const importDeclarationChanges = declaration.kind === SyntaxKind.ImportSpecifier && canConvertImportDeclarationForSpecifier(declaration, context.sourceFile, context.program)
? textChanges.ChangeTracker.with(context, t => doChange(t, context.sourceFile, declaration.parent.parent.parent))
: undefined;
const mainAction = createCodeFixAction(
fixId,
changes,
declaration.kind === SyntaxKind.ImportSpecifier
? [Diagnostics.Use_type_0, declaration.propertyName?.text ?? declaration.name.text]
: Diagnostics.Use_import_type,
fixId,
Diagnostics.Fix_all_with_type_only_imports,
);
if (some(importDeclarationChanges)) {
return [
createCodeFixActionWithoutFixAll(fixId, importDeclarationChanges, Diagnostics.Use_import_type),
mainAction,
];
}
return [mainAction];
}
return undefined;
},
fixIds: [fixId],
getAllCodeActions: function getAllCodeActionsToConvertToTypeOnlyImport(context) {
const fixedImportDeclarations = new Set<ImportDeclaration>();
return codeFixAll(context, errorCodes, (changes, diag) => {
const errorDeclaration = getDeclaration(diag.file, diag.start);
if (errorDeclaration?.kind === SyntaxKind.ImportDeclaration && !fixedImportDeclarations.has(errorDeclaration)) {
doChange(changes, diag.file, errorDeclaration);
fixedImportDeclarations.add(errorDeclaration);
}
else if (
errorDeclaration?.kind === SyntaxKind.ImportSpecifier
&& !fixedImportDeclarations.has(errorDeclaration.parent.parent.parent)
&& canConvertImportDeclarationForSpecifier(errorDeclaration, diag.file, context.program)
) {
doChange(changes, diag.file, errorDeclaration.parent.parent.parent);
fixedImportDeclarations.add(errorDeclaration.parent.parent.parent);
}
else if (errorDeclaration?.kind === SyntaxKind.ImportSpecifier) {
doChange(changes, diag.file, errorDeclaration);
}
});
},
});
function getDeclaration(sourceFile: SourceFile, pos: number) {
const { parent } = getTokenAtPosition(sourceFile, pos);
return isImportSpecifier(parent) || isImportDeclaration(parent) && parent.importClause ? parent : undefined;
}
function canConvertImportDeclarationForSpecifier(specifier: ImportSpecifier, sourceFile: SourceFile, program: Program): boolean {
if (specifier.parent.parent.name) {
// An import declaration with a default import and named bindings can't be type-only
return false;
}
const nonTypeOnlySpecifiers = specifier.parent.elements.filter(e => !e.isTypeOnly);
if (nonTypeOnlySpecifiers.length === 1) {
// If the error specifier is on the only non-type-only specifier, we can convert the whole import
return true;
}
// Otherwise, we need to check the usage of the other specifiers
const checker = program.getTypeChecker();
for (const specifier of nonTypeOnlySpecifiers) {
const isUsedAsValue = FindAllReferences.Core.eachSymbolReferenceInFile(specifier.name, checker, sourceFile, usage => {
return !isValidTypeOnlyAliasUseSite(usage);
});
if (isUsedAsValue) {
return false;
}
}
// No other specifiers are used as values, so we can convert the whole import
return true;
}
function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, declaration: ImportDeclaration | ImportSpecifier) {
if (isImportSpecifier(declaration)) {
changes.replaceNode(sourceFile, declaration, factory.updateImportSpecifier(declaration, /*isTypeOnly*/ true, declaration.propertyName, declaration.name));
}
else {
const importClause = declaration.importClause as ImportClause;
if (importClause.name && importClause.namedBindings) {
changes.replaceNodeWithNodes(sourceFile, declaration, [
factory.createImportDeclaration(
getSynthesizedDeepClones(declaration.modifiers, /*includeTrivia*/ true),
factory.createImportClause(/*isTypeOnly*/ true, getSynthesizedDeepClone(importClause.name, /*includeTrivia*/ true), /*namedBindings*/ undefined),
getSynthesizedDeepClone(declaration.moduleSpecifier, /*includeTrivia*/ true),
getSynthesizedDeepClone(declaration.attributes, /*includeTrivia*/ true),
),
factory.createImportDeclaration(
getSynthesizedDeepClones(declaration.modifiers, /*includeTrivia*/ true),
factory.createImportClause(/*isTypeOnly*/ true, /*name*/ undefined, getSynthesizedDeepClone(importClause.namedBindings, /*includeTrivia*/ true)),
getSynthesizedDeepClone(declaration.moduleSpecifier, /*includeTrivia*/ true),
getSynthesizedDeepClone(declaration.attributes, /*includeTrivia*/ true),
),
]);
}
else {
const newNamedBindings = importClause.namedBindings?.kind === SyntaxKind.NamedImports
? factory.updateNamedImports(
importClause.namedBindings,
sameMap(importClause.namedBindings.elements, e => factory.updateImportSpecifier(e, /*isTypeOnly*/ false, e.propertyName, e.name)),
)
: importClause.namedBindings;
const importDeclaration = factory.updateImportDeclaration(declaration, declaration.modifiers, factory.updateImportClause(importClause, /*isTypeOnly*/ true, importClause.name, newNamedBindings), declaration.moduleSpecifier, declaration.attributes);
changes.replaceNode(sourceFile, declaration, importDeclaration);
}
}
}