Add first example of error suggestion
Lauren Tan committed
Jun 27, 2023 at 14:01 UTC
3fb89373913009eca2a7810d6d1729de414b5379
4 files changed
+42
-3
compiler/forget/packages/babel-plugin-react-forget/src/Entrypoint/Program.ts
+10
-2
@@ -10,6 +10,7 @@ import * as t from "@babel/types";
10
import {
11
CompilerError,
12
CompilerErrorDetail,
13
+ CompilerSuggestionOperation,
14
ErrorSeverity,
15
} from "../CompilerError";
16
import { GeneratedSource } from "../HIR";
@@ -197,7 +198,8 @@ export function compileProgram(
198
},
199
});
200
200
- const reason = `One or more React eslint rules is disabled`;
201
+ const reason =
202
+ "React Forget has bailed out of optimizing this component as one or more React eslint rules were disabled. React Forget only works when your components follow all the rules of React, disabling them may result in undefined behavior";
203
const error = new CompilerError();
204
for (const violation of violations) {
205
if (options.logger != null) {
@@ -214,7 +216,13 @@ export function compileProgram(
216
description: violation.value.trim(),
217
severity: ErrorSeverity.InvalidReact,
218
loc: violation.loc ?? null,
217
- suggestions: null, // TODO(@poteto) add autofix for eslint
219
+ suggestions: [
220
+ {
221
+ description: "Remove the eslint disable",
222
+ range: [violation.start!, violation.end!],
223
+ op: CompilerSuggestionOperation.Remove,
224
+ },
225
+ ],
226
})
227
);
228
}
compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.invalid-sketchy-code-use-forget.expect.md
+1
-1
@@ -16,7 +16,7 @@ function lowercasecomponent() {
16
## Error
17
18
```
19
-[ReactForget] InvalidReact: One or more React eslint rules is disabled. eslint-disable react-hooks/rules-of-hooks (1:1)
19
+[ReactForget] InvalidReact: React Forget has bailed out of optimizing this component as one or more React eslint rules were disabled. React Forget only works when your components follow all the rules of React, disabling them may result in undefined behavior. eslint-disable react-hooks/rules-of-hooks (1:1)
20
```
21
22
\ No newline at end of file
compiler/forget/packages/eslint-plugin-react-forget/__tests__/ReactForgetDiagnostics-test.ts
+30
@@ -80,6 +80,36 @@ const tests: ForgetTestCases = {
80
},
81
],
82
},
83
+ {
84
+ name: "[InvalidReact] ESlint suppression",
85
+ // Indentation is intentionally weird so it doesn't add extra whitespace
86
+ code: normalizeIndent`
87
+ function Component(props) {
88
+ 'use forget';
89
+ // eslint-disable-next-line react-hooks/rules-of-hooks
90
+ Component();
91
+ }`,
92
+ errors: [
93
+ {
94
+ message:
95
+ "[ReactForget] InvalidReact: React Forget has bailed out of optimizing this component as one or more React eslint rules were disabled. React Forget only works when your components follow all the rules of React, disabling them may result in undefined behavior. eslint-disable-next-line react-hooks/rules-of-hooks (4:4)",
96
+ suggestions: [
97
+ {
98
+ output: normalizeIndent`
99
+ function Component(props) {
100
+ 'use forget';
101
+
102
+ Component();
103
+ }`,
104
+ },
105
+ ],
106
+ },
107
+ {
108
+ message:
109
+ "Definition for rule 'react-hooks/rules-of-hooks' was not found.",
110
+ },
111
+ ],
112
+ },
113
],
114
};
115
compiler/forget/packages/eslint-plugin-react-forget/src/rules/ReactForgetDiagnostics.ts
+1
@@ -69,6 +69,7 @@ const rule: Rule.RuleModule = {
69
recommended: true,
70
},
71
fixable: "code",
72
+ hasSuggestions: true,
73
},
74
create(context: Rule.RuleContext) {
75
// Compat with older versions of eslint