@samitouri / QOS-React / commits / d0a51e7dfc

Allow eslint rule reportable severity to be set

During the demo I might show an example of fixing a CannotPreserveMemoization error. But I don't want to make that reportable by default, so this PR allows configuration like so ```js module.exports = { root: true, plugins: [ 'eslint-plugin-react-compiler', ], rules: { 'react-compiler/react-compiler': [ 'error', { reportableLevels: new Set([ 'InvalidJs', 'InvalidReact', 'CannotPreserveMemoization' ]) } ] } } ``` ghstack-source-id: 984c6d3cb7e19c8fea2bb88108dd26335c031573 Pull Request resolved: https://github.com/facebook/react-forget/pull/2936

Lauren Tan committed May 6, 2024 at 20:07 UTC d0a51e7dfc0e40b7c5304adcd9b2b8ad687b37f9
2 files changed +33 -20
compiler/packages/eslint-plugin-react-compiler/__tests__/ReactCompilerRule-test.ts
+16
@@ -5,6 +5,7 @@
5 * LICENSE file in the root directory of this source tree.
6 */
7
8 +import { ErrorSeverity } from "babel-plugin-react-compiler/src";
9 import { RuleTester as ESLintTester } from "eslint";
10 import ReactCompilerRule from "../src/rules/ReactCompilerRule";
11
@@ -105,6 +106,21 @@ const tests: CompilerTestCases = {
106 },
107 ],
108 invalid: [
109 + {
110 + name: "Reportable levels can be configured",
111 + options: [{ reportableLevels: new Set([ErrorSeverity.Todo]) }],
112 + code: normalizeIndent`
113 + function Foo(x) {
114 + var y = 1;
115 + return <div>{y * x}</div>;
116 + }`,
117 + errors: [
118 + {
119 + message:
120 + "(BuildHIR::lowerStatement) Handle var kinds in VariableDeclaration",
121 + },
122 + ],
123 + },
124 {
125 name: "[InvalidReact] ESlint suppression",
126 // Indentation is intentionally weird so it doesn't add extra whitespace
compiler/packages/eslint-plugin-react-compiler/src/rules/ReactCompilerRule.ts
+17 -20
@@ -33,29 +33,17 @@ function isReactCompilerError(err: Error): err is CompilerError {
33 return err.name === "ReactCompilerError";
34 }
35
36 +const DEFAULT_REPORTABLE_LEVELS = new Set([
37 + ErrorSeverity.InvalidReact,
38 + ErrorSeverity.InvalidJS,
39 +]);
40 +let reportableLevels = DEFAULT_REPORTABLE_LEVELS;
41 +
42 function isReportableDiagnostic(
43 detail: CompilerErrorDetail
44 ): detail is CompilerErrorDetailWithLoc {
39 - let isReportable = false;
40 - switch (detail.severity) {
41 - case ErrorSeverity.InvalidReact:
42 - case ErrorSeverity.InvalidJS:
43 - isReportable = true;
44 - break;
45 - case ErrorSeverity.InvalidConfig:
46 - case ErrorSeverity.Invariant:
47 - case ErrorSeverity.CannotPreserveMemoization:
48 - case ErrorSeverity.Todo:
49 - break;
50 - default:
51 - assertExhaustive(
52 - detail.severity,
53 - `Unhandled error severity \`${detail.severity}\``
54 - );
55 - }
56 -
45 return (
58 - isReportable === true &&
46 + reportableLevels.has(detail.severity) &&
47 detail.loc != null &&
48 typeof detail.loc !== "symbol"
49 );
@@ -83,8 +71,17 @@ const rule: Rule.RuleModule = {
71 // Compat with older versions of eslint
72 const sourceCode = context.sourceCode?.text ?? context.getSourceCode().text;
73 const filename = context.filename ?? context.getFilename();
74 + const userOpts = context.options[0] ?? {};
75 + if (
76 + userOpts["reportableLevels"] != null &&
77 + userOpts["reportableLevels"] instanceof Set
78 + ) {
79 + reportableLevels = userOpts["reportableLevels"];
80 + } else {
81 + reportableLevels = DEFAULT_REPORTABLE_LEVELS;
82 + }
83 const options: PluginOptions = {
87 - ...parsePluginOptions(context.options[0] ?? {}),
84 + ...parsePluginOptions(userOpts),
85 ...COMPILER_OPTIONS,
86 };
87