-
Notifications
You must be signed in to change notification settings - Fork 12.8k
/
Copy pathrequireInTs.ts
110 lines (103 loc) · 4.34 KB
/
requireInTs.ts
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
import {
codeFixAll,
createCodeFixAction,
registerCodeFix,
} from "../_namespaces/ts.codefix.js";
import {
cast,
Debug,
Diagnostics,
factory,
first,
getAllowSyntheticDefaultImports,
getQuotePreference,
getTokenAtPosition,
Identifier,
ImportSpecifier,
isIdentifier,
isNoSubstitutionTemplateLiteral,
isObjectBindingPattern,
isRequireCall,
isVariableDeclaration,
isVariableStatement,
NamedImports,
ObjectBindingPattern,
Program,
QuotePreference,
SourceFile,
StringLiteralLike,
textChanges,
tryCast,
UserPreferences,
VariableStatement,
} from "../_namespaces/ts.js";
const fixId = "requireInTs";
const errorCodes = [Diagnostics.require_call_may_be_converted_to_an_import.code];
registerCodeFix({
errorCodes,
getCodeActions(context) {
const info = getInfo(context.sourceFile, context.program, context.span.start, context.preferences);
if (!info) {
return undefined;
}
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, context.sourceFile, info));
return [createCodeFixAction(fixId, changes, Diagnostics.Convert_require_to_import, fixId, Diagnostics.Convert_all_require_to_import)];
},
fixIds: [fixId],
getAllCodeActions: context =>
codeFixAll(context, errorCodes, (changes, diag) => {
const info = getInfo(diag.file, context.program, diag.start, context.preferences);
if (info) {
doChange(changes, context.sourceFile, info);
}
}),
});
function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, info: Info) {
const { allowSyntheticDefaults, defaultImportName, namedImports, statement, moduleSpecifier } = info;
changes.replaceNode(
sourceFile,
statement,
defaultImportName && !allowSyntheticDefaults
? factory.createImportEqualsDeclaration(/*modifiers*/ undefined, /*isTypeOnly*/ false, defaultImportName, factory.createExternalModuleReference(moduleSpecifier))
: factory.createImportDeclaration(/*modifiers*/ undefined, factory.createImportClause(/*isTypeOnly*/ false, defaultImportName, namedImports), moduleSpecifier, /*attributes*/ undefined),
);
}
interface Info {
readonly allowSyntheticDefaults: boolean;
readonly defaultImportName: Identifier | undefined;
readonly namedImports: NamedImports | undefined;
readonly statement: VariableStatement;
readonly moduleSpecifier: StringLiteralLike;
}
function getInfo(sourceFile: SourceFile, program: Program, pos: number, preferences: UserPreferences): Info | undefined {
const { parent } = getTokenAtPosition(sourceFile, pos);
if (!isRequireCall(parent, /*requireStringLiteralLikeArgument*/ true)) {
Debug.failBadSyntaxKind(parent);
}
const decl = cast(parent.parent, isVariableDeclaration);
const quotePreference = getQuotePreference(sourceFile, preferences);
const defaultImportName = tryCast(decl.name, isIdentifier);
const namedImports = isObjectBindingPattern(decl.name) ? tryCreateNamedImportsFromObjectBindingPattern(decl.name) : undefined;
if (defaultImportName || namedImports) {
const moduleSpecifier = first(parent.arguments);
return {
allowSyntheticDefaults: getAllowSyntheticDefaultImports(program.getCompilerOptions()),
defaultImportName,
namedImports,
statement: cast(decl.parent.parent, isVariableStatement),
moduleSpecifier: isNoSubstitutionTemplateLiteral(moduleSpecifier) ? factory.createStringLiteral(moduleSpecifier.text, quotePreference === QuotePreference.Single) : moduleSpecifier,
};
}
}
function tryCreateNamedImportsFromObjectBindingPattern(node: ObjectBindingPattern): NamedImports | undefined {
const importSpecifiers: ImportSpecifier[] = [];
for (const element of node.elements) {
if (!isIdentifier(element.name) || element.initializer) {
return undefined;
}
importSpecifiers.push(factory.createImportSpecifier(/*isTypeOnly*/ false, tryCast(element.propertyName, isIdentifier), element.name));
}
if (importSpecifiers.length) {
return factory.createNamedImports(importSpecifiers);
}
}