-
Notifications
You must be signed in to change notification settings - Fork 12.8k
/
Copy pathfixModuleAndTargetOptions.ts
64 lines (57 loc) · 3.13 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import {
createCodeFixActionWithoutFixAll,
registerCodeFix,
setJsonCompilerOptionValue,
setJsonCompilerOptionValues,
} from "../_namespaces/ts.codefix.js";
import {
CodeFixAction,
Diagnostics,
Expression,
factory,
getEmitModuleKind,
getEmitScriptTarget,
getTsConfigObjectLiteralExpression,
ModuleKind,
ScriptTarget,
textChanges,
} from "../_namespaces/ts.js";
registerCodeFix({
errorCodes: [
Diagnostics.Top_level_await_expressions_are_only_allowed_when_the_module_option_is_set_to_es2022_esnext_system_node16_node18_nodenext_or_preserve_and_the_target_option_is_set_to_es2017_or_higher.code,
Diagnostics.Top_level_await_using_statements_are_only_allowed_when_the_module_option_is_set_to_es2022_esnext_system_node16_node18_nodenext_or_preserve_and_the_target_option_is_set_to_es2017_or_higher.code,
Diagnostics.Top_level_for_await_loops_are_only_allowed_when_the_module_option_is_set_to_es2022_esnext_system_node16_node18_nodenext_or_preserve_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: CodeFixAction[] = [];
const moduleKind = getEmitModuleKind(compilerOptions);
const moduleOutOfRange = moduleKind >= ModuleKind.ES2015 && moduleKind < ModuleKind.ESNext;
if (moduleOutOfRange) {
const changes = textChanges.ChangeTracker.with(context, changes => {
setJsonCompilerOptionValue(changes, configFile, "module", factory.createStringLiteral("esnext"));
});
codeFixes.push(createCodeFixActionWithoutFixAll("fixModuleOption", changes, [Diagnostics.Set_the_module_option_in_your_configuration_file_to_0, "esnext"]));
}
const target = getEmitScriptTarget(compilerOptions);
const targetOutOfRange = target < ScriptTarget.ES2017 || target > ScriptTarget.ESNext;
if (targetOutOfRange) {
const changes = textChanges.ChangeTracker.with(context, tracker => {
const configObject = getTsConfigObjectLiteralExpression(configFile);
if (!configObject) return;
const options: [string, Expression][] = [["target", factory.createStringLiteral("es2017")]];
if (moduleKind === ModuleKind.CommonJS) {
// Ensure we preserve the default module kind (commonjs), as targets >= ES2015 have a default module kind of es2015.
options.push(["module", factory.createStringLiteral("commonjs")]);
}
setJsonCompilerOptionValues(tracker, configFile, options);
});
codeFixes.push(createCodeFixActionWithoutFixAll("fixTargetOption", changes, [Diagnostics.Set_the_target_option_in_your_configuration_file_to_0, "es2017"]));
}
return codeFixes.length ? codeFixes : undefined;
},
});