-
Notifications
You must be signed in to change notification settings - Fork 12.8k
/
Copy pathfixEnableJsxFlag.ts
43 lines (39 loc) · 1.46 KB
/
fixEnableJsxFlag.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
import {
codeFixAll,
createCodeFixActionWithoutFixAll,
registerCodeFix,
setJsonCompilerOptionValue,
} from "../_namespaces/ts.codefix.js";
import {
Diagnostics,
factory,
textChanges,
TsConfigSourceFile,
} from "../_namespaces/ts.js";
const fixID = "fixEnableJsxFlag";
const errorCodes = [Diagnostics.Cannot_use_JSX_unless_the_jsx_flag_is_provided.code];
registerCodeFix({
errorCodes,
getCodeActions: function getCodeActionsToFixEnableJsxFlag(context) {
const { configFile } = context.program.getCompilerOptions();
if (configFile === undefined) {
return undefined;
}
const changes = textChanges.ChangeTracker.with(context, changeTracker => doChange(changeTracker, configFile));
return [
createCodeFixActionWithoutFixAll(fixID, changes, Diagnostics.Enable_the_jsx_flag_in_your_configuration_file),
];
},
fixIds: [fixID],
getAllCodeActions: context =>
codeFixAll(context, errorCodes, changes => {
const { configFile } = context.program.getCompilerOptions();
if (configFile === undefined) {
return undefined;
}
doChange(changes, configFile);
}),
});
function doChange(changeTracker: textChanges.ChangeTracker, configFile: TsConfigSourceFile) {
setJsonCompilerOptionValue(changeTracker, configFile, "jsx", factory.createStringLiteral("react"));
}