forked from microsoft/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplitTypeOnlyImport.ts
More file actions
45 lines (42 loc) · 2.26 KB
/
splitTypeOnlyImport.ts
File metadata and controls
45 lines (42 loc) · 2.26 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
/* @internal */
namespace ts.codefix {
const errorCodes = [Diagnostics.A_type_only_import_can_specify_a_default_import_or_named_bindings_but_not_both.code];
const fixId = "splitTypeOnlyImport";
registerCodeFix({
errorCodes,
fixIds: [fixId],
getCodeActions: context => {
const changes = textChanges.ChangeTracker.with(context, t => {
return splitTypeOnlyImport(t, getImportDeclaration(context.sourceFile, context.span), context);
});
if (changes.length) {
return [createCodeFixAction(fixId, changes, Diagnostics.Split_into_two_separate_import_declarations, fixId, Diagnostics.Split_all_invalid_type_only_imports)];
}
},
getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, error) => {
splitTypeOnlyImport(changes, getImportDeclaration(context.sourceFile, error), context);
}),
});
function getImportDeclaration(sourceFile: SourceFile, span: TextSpan) {
return findAncestor(getTokenAtPosition(sourceFile, span.start), isImportDeclaration);
}
function splitTypeOnlyImport(changes: textChanges.ChangeTracker, importDeclaration: ImportDeclaration | undefined, context: CodeFixContextBase) {
if (!importDeclaration) {
return;
}
const importClause = Debug.checkDefined(importDeclaration.importClause);
changes.replaceNode(context.sourceFile, importDeclaration, factory.updateImportDeclaration(
importDeclaration,
importDeclaration.decorators,
importDeclaration.modifiers,
factory.updateImportClause(importClause, importClause.isTypeOnly, importClause.name, /*namedBindings*/ undefined),
importDeclaration.moduleSpecifier,
importDeclaration.assertClause));
changes.insertNodeAfter(context.sourceFile, importDeclaration, factory.createImportDeclaration(
/*decorators*/ undefined,
/*modifiers*/ undefined,
factory.updateImportClause(importClause, importClause.isTypeOnly, /*name*/ undefined, importClause.namedBindings),
importDeclaration.moduleSpecifier,
importDeclaration.assertClause));
}
}