Add JSX codefix if available (#32281)

* Add JSX codefix if available

* Update react jsx.

* Update diagnostic code.
This commit is contained in:
Hoang Pham 2019-08-16 12:46:55 -07:00 committed by Ryan Cavanaugh
parent f45add0a7d
commit 46b7972885
10 changed files with 179 additions and 0 deletions

View file

@ -5112,6 +5112,10 @@
"category": "Message",
"code": 95087
},
"Enable the '--jsx' flag in your configuration file": {
"category": "Message",
"code": 95088
},
"No value exists in scope for the shorthand property '{0}'. Either declare one or provide an initializer.": {
"category": "Error",

View file

@ -0,0 +1,35 @@
/* @internal */
namespace ts.codefix {
const fixID = "fixEnableJsxFlag";
const errorCodes = [Diagnostics.Cannot_use_JSX_unless_the_jsx_flag_is_provided.code];
registerCodeFix({
errorCodes,
getCodeActions: context => {
const { configFile } = context.program.getCompilerOptions();
if (configFile === undefined) {
return undefined;
}
const changes = textChanges.ChangeTracker.with(context, changeTracker =>
doChange(changeTracker, configFile)
);
return [
createCodeFixActionNoFixId(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", createStringLiteral("react"));
}
}

View file

@ -65,6 +65,7 @@
"codefixes/fixClassSuperMustPrecedeThisAccess.ts",
"codefixes/fixConstructorForDerivedNeedSuperCall.ts",
"codefixes/fixEnableExperimentalDecorators.ts",
"codefixes/fixEnableJsxFlag.ts",
"codefixes/fixExtendsInterfaceBecomesImplements.ts",
"codefixes/fixForgottenThisPropertyAccess.ts",
"codefixes/fixUnusedIdentifier.ts",

View file

@ -0,0 +1,23 @@
/// <reference path='fourslash.ts' />
// @Filename: /dir/a.tsx
////export const Component = () => <></>
// @Filename: /dir/jsconfig.json
////{
//// "compilerOptions": {
//// }
////}
goTo.file("/dir/a.tsx");
verify.codeFix({
description: "Enable the '--jsx' flag in your configuration file",
newFileContent: {
"/dir/jsconfig.json":
`{
"compilerOptions": {
"jsx": "react",
}
}`,
},
});

View file

@ -0,0 +1,23 @@
/// <reference path='fourslash.ts' />
// @Filename: /dir/a.tsx
////export const Component = () => <></>;
// @Filename: /dir/tsconfig.json
////{
//// "compilerOptions": {
//// }
////}
goTo.file("/dir/a.tsx");
verify.codeFix({
description: "Enable the '--jsx' flag in your configuration file",
newFileContent: {
"/dir/tsconfig.json":
`{
"compilerOptions": {
"jsx": "react",
}
}`,
},
});

View file

@ -0,0 +1,24 @@
/// <reference path='fourslash.ts' />
// @Filename: /dir/a.tsx
////export const Component = () => <></>;
// @Filename: /dir/jsconfig.json
////{
//// "compilerOptions": {
//// "jsx": false,
//// }
////}
goTo.file("/dir/a.tsx");
verify.codeFix({
description: "Enable the '--jsx' flag in your configuration file",
newFileContent: {
"/dir/jsconfig.json":
`{
"compilerOptions": {
"jsx": "react",
}
}`,
},
});

View file

@ -0,0 +1,24 @@
/// <reference path='fourslash.ts' />
// @Filename: /dir/a.tsx
////export const Component = () => <></>
// @Filename: /dir/tsconfig.json
////{
//// "compilerOptions": {
//// "jsx": false,
//// }
////}
goTo.file("/dir/a.tsx");
verify.codeFix({
description: "Enable the '--jsx' flag in your configuration file",
newFileContent: {
"/dir/tsconfig.json":
`{
"compilerOptions": {
"jsx": "react",
}
}`,
},
});

View file

@ -0,0 +1,19 @@
/// <reference path='fourslash.ts' />
// @Filename: /dir/a.tsx
////export const Component = () => <></>
// @Filename: /dir/jsconfig.json
////{
////}
goTo.file("/dir/a.tsx");
verify.codeFix({
description: "Enable the '--jsx' flag in your configuration file",
newFileContent: {
"/dir/jsconfig.json":
`{
"compilerOptions": { "jsx": "react" },
}`,
},
});

View file

@ -0,0 +1,19 @@
/// <reference path='fourslash.ts' />
// @Filename: /dir/a.tsx
////export const Component = () => <></>
// @Filename: /dir/tsconfig.json
////{
////}
goTo.file("/dir/a.tsx");
verify.codeFix({
description: "Enable the '--jsx' flag in your configuration file",
newFileContent: {
"/dir/tsconfig.json":
`{
"compilerOptions": { "jsx": "react" },
}`,
},
});

View file

@ -0,0 +1,7 @@
/// <reference path='fourslash.ts' />
// @Filename: /dir/a.tsx
////export const Component = () => <></>
goTo.file("/dir/a.tsx");
verify.not.codeFixAvailable();