-
Notifications
You must be signed in to change notification settings - Fork 12.8k
/
Copy pathfixModuleAndTargetOptions.ts
47 lines (41 loc) · 2.62 KB
/
fixModuleAndTargetOptions.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
/* @internal */
namespace ts.codefix {
ts.codefix.registerCodeFix({
errorCodes: [
ts.Diagnostics.Top_level_await_expressions_are_only_allowed_when_the_module_option_is_set_to_es2022_esnext_system_node16_or_nodenext_and_the_target_option_is_set_to_es2017_or_higher.code,
ts.Diagnostics.Top_level_for_await_loops_are_only_allowed_when_the_module_option_is_set_to_es2022_esnext_system_node16_or_nodenext_and_the_target_option_is_set_to_es2017_or_higher.code,
],
getCodeActions: function getCodeActionsToFixModuleAndTarget(context) {
const compilerOptions = context.program.getCompilerOptions();
const { configFile } = compilerOptions;
if (configFile === undefined) {
return undefined;
}
const codeFixes: ts.CodeFixAction[] = [];
const moduleKind = ts.getEmitModuleKind(compilerOptions);
const moduleOutOfRange = moduleKind >= ts.ModuleKind.ES2015 && moduleKind < ts.ModuleKind.ESNext;
if (moduleOutOfRange) {
const changes = ts.textChanges.ChangeTracker.with(context, changes => {
ts.codefix.setJsonCompilerOptionValue(changes, configFile, "module", ts.factory.createStringLiteral("esnext"));
});
codeFixes.push(ts.codefix.createCodeFixActionWithoutFixAll("fixModuleOption", changes, [ts.Diagnostics.Set_the_module_option_in_your_configuration_file_to_0, "esnext"]));
}
const target = ts.getEmitScriptTarget(compilerOptions);
const targetOutOfRange = target < ts.ScriptTarget.ES2017 || target > ts.ScriptTarget.ESNext;
if (targetOutOfRange) {
const changes = ts.textChanges.ChangeTracker.with(context, tracker => {
const configObject = ts.getTsConfigObjectLiteralExpression(configFile);
if (!configObject) return;
const options: [string, ts.Expression][] = [["target", ts.factory.createStringLiteral("es2017")]];
if (moduleKind === ts.ModuleKind.CommonJS) {
// Ensure we preserve the default module kind (commonjs), as targets >= ES2015 have a default module kind of es2015.
options.push(["module", ts.factory.createStringLiteral("commonjs")]);
}
ts.codefix.setJsonCompilerOptionValues(tracker, configFile, options);
});
codeFixes.push(ts.codefix.createCodeFixActionWithoutFixAll("fixTargetOption", changes, [ts.Diagnostics.Set_the_target_option_in_your_configuration_file_to_0, "es2017"]));
}
return codeFixes.length ? codeFixes : undefined;
}
});
}