Skip to content

Commit 46b7972

Browse files
hoangpham95RyanCavanaugh
authored andcommitted
Add JSX codefix if available (#32281)
* Add JSX codefix if available * Update react jsx. * Update diagnostic code.
1 parent f45add0 commit 46b7972

10 files changed

+179
-0
lines changed

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5112,6 +5112,10 @@
51125112
"category": "Message",
51135113
"code": 95087
51145114
},
5115+
"Enable the '--jsx' flag in your configuration file": {
5116+
"category": "Message",
5117+
"code": 95088
5118+
},
51155119

51165120
"No value exists in scope for the shorthand property '{0}'. Either declare one or provide an initializer.": {
51175121
"category": "Error",
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/* @internal */
2+
namespace ts.codefix {
3+
const fixID = "fixEnableJsxFlag";
4+
const errorCodes = [Diagnostics.Cannot_use_JSX_unless_the_jsx_flag_is_provided.code];
5+
registerCodeFix({
6+
errorCodes,
7+
getCodeActions: context => {
8+
const { configFile } = context.program.getCompilerOptions();
9+
if (configFile === undefined) {
10+
return undefined;
11+
}
12+
13+
const changes = textChanges.ChangeTracker.with(context, changeTracker =>
14+
doChange(changeTracker, configFile)
15+
);
16+
return [
17+
createCodeFixActionNoFixId(fixID, changes, Diagnostics.Enable_the_jsx_flag_in_your_configuration_file)
18+
];
19+
},
20+
fixIds: [fixID],
21+
getAllCodeActions: context =>
22+
codeFixAll(context, errorCodes, changes => {
23+
const { configFile } = context.program.getCompilerOptions();
24+
if (configFile === undefined) {
25+
return undefined;
26+
}
27+
28+
doChange(changes, configFile);
29+
})
30+
});
31+
32+
function doChange(changeTracker: textChanges.ChangeTracker, configFile: TsConfigSourceFile) {
33+
setJsonCompilerOptionValue(changeTracker, configFile, "jsx", createStringLiteral("react"));
34+
}
35+
}

src/services/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"codefixes/fixClassSuperMustPrecedeThisAccess.ts",
6666
"codefixes/fixConstructorForDerivedNeedSuperCall.ts",
6767
"codefixes/fixEnableExperimentalDecorators.ts",
68+
"codefixes/fixEnableJsxFlag.ts",
6869
"codefixes/fixExtendsInterfaceBecomesImplements.ts",
6970
"codefixes/fixForgottenThisPropertyAccess.ts",
7071
"codefixes/fixUnusedIdentifier.ts",
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @Filename: /dir/a.tsx
4+
////export const Component = () => <></>
5+
6+
// @Filename: /dir/jsconfig.json
7+
////{
8+
//// "compilerOptions": {
9+
//// }
10+
////}
11+
12+
goTo.file("/dir/a.tsx");
13+
verify.codeFix({
14+
description: "Enable the '--jsx' flag in your configuration file",
15+
newFileContent: {
16+
"/dir/jsconfig.json":
17+
`{
18+
"compilerOptions": {
19+
"jsx": "react",
20+
}
21+
}`,
22+
},
23+
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @Filename: /dir/a.tsx
4+
////export const Component = () => <></>;
5+
6+
// @Filename: /dir/tsconfig.json
7+
////{
8+
//// "compilerOptions": {
9+
//// }
10+
////}
11+
12+
goTo.file("/dir/a.tsx");
13+
verify.codeFix({
14+
description: "Enable the '--jsx' flag in your configuration file",
15+
newFileContent: {
16+
"/dir/tsconfig.json":
17+
`{
18+
"compilerOptions": {
19+
"jsx": "react",
20+
}
21+
}`,
22+
},
23+
});
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @Filename: /dir/a.tsx
4+
////export const Component = () => <></>;
5+
6+
// @Filename: /dir/jsconfig.json
7+
////{
8+
//// "compilerOptions": {
9+
//// "jsx": false,
10+
//// }
11+
////}
12+
13+
goTo.file("/dir/a.tsx");
14+
verify.codeFix({
15+
description: "Enable the '--jsx' flag in your configuration file",
16+
newFileContent: {
17+
"/dir/jsconfig.json":
18+
`{
19+
"compilerOptions": {
20+
"jsx": "react",
21+
}
22+
}`,
23+
},
24+
});
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @Filename: /dir/a.tsx
4+
////export const Component = () => <></>
5+
6+
// @Filename: /dir/tsconfig.json
7+
////{
8+
//// "compilerOptions": {
9+
//// "jsx": false,
10+
//// }
11+
////}
12+
13+
goTo.file("/dir/a.tsx");
14+
verify.codeFix({
15+
description: "Enable the '--jsx' flag in your configuration file",
16+
newFileContent: {
17+
"/dir/tsconfig.json":
18+
`{
19+
"compilerOptions": {
20+
"jsx": "react",
21+
}
22+
}`,
23+
},
24+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @Filename: /dir/a.tsx
4+
////export const Component = () => <></>
5+
6+
// @Filename: /dir/jsconfig.json
7+
////{
8+
////}
9+
10+
goTo.file("/dir/a.tsx");
11+
verify.codeFix({
12+
description: "Enable the '--jsx' flag in your configuration file",
13+
newFileContent: {
14+
"/dir/jsconfig.json":
15+
`{
16+
"compilerOptions": { "jsx": "react" },
17+
}`,
18+
},
19+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @Filename: /dir/a.tsx
4+
////export const Component = () => <></>
5+
6+
// @Filename: /dir/tsconfig.json
7+
////{
8+
////}
9+
10+
goTo.file("/dir/a.tsx");
11+
verify.codeFix({
12+
description: "Enable the '--jsx' flag in your configuration file",
13+
newFileContent: {
14+
"/dir/tsconfig.json":
15+
`{
16+
"compilerOptions": { "jsx": "react" },
17+
}`,
18+
},
19+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @Filename: /dir/a.tsx
4+
////export const Component = () => <></>
5+
6+
goTo.file("/dir/a.tsx");
7+
verify.not.codeFixAvailable();

0 commit comments

Comments
 (0)