[compiler][eslint] Add donotuse flag for bailouts
--- Adding an experimental / donotuse flag for small Meta internal usecase ghstack-source-id: 908ef1e150c9fef1347616c9c4dc6bf3316900b0 Pull Request resolved: https://github.com/facebook/react/pull/30342
Mofei Zhang committed
Jul 15, 2024 at 17:44 UTC
1f60a41801bde7fa6970c8cf36019248c2ee8996
2 files changed
+50
-1
compiler/packages/eslint-plugin-react-compiler/__tests__/ReactCompilerRule-test.ts
+20
@@ -179,6 +179,26 @@ const tests: CompilerTestCases = {
179
},
180
],
181
},
182
+ {
183
+ name: "Test experimental/unstable report all bailouts mode",
184
+ options: [
185
+ {
186
+ reportableLevels: new Set([ErrorSeverity.InvalidReact]),
187
+ __unstable_donotuse_reportAllBailouts: true,
188
+ },
189
+ ],
190
+ code: normalizeIndent`
191
+ function Foo(x) {
192
+ var y = 1;
193
+ return <div>{y * x}</div>;
194
+ }`,
195
+ errors: [
196
+ {
197
+ message:
198
+ "[ReactCompilerBailout] (BuildHIR::lowerStatement) Handle var kinds in VariableDeclaration (@:3:2)",
199
+ },
200
+ ],
201
+ },
202
],
203
};
204
compiler/packages/eslint-plugin-react-compiler/src/rules/ReactCompilerRule.ts
+30
-1
@@ -129,6 +129,22 @@ const rule: Rule.RuleModule = {
129
} else {
130
reportableLevels = DEFAULT_REPORTABLE_LEVELS;
131
}
132
+ /**
133
+ * Experimental setting to report all compilation bailouts on the compilation
134
+ * unit (e.g. function or hook) instead of the offensive line.
135
+ * Intended to be used when a codebase is 100% reliant on the compiler for
136
+ * memoization (i.e. deleted all manual memo) and needs compilation success
137
+ * signals for perf debugging.
138
+ */
139
+ let __unstable_donotuse_reportAllBailouts: boolean = false;
140
+ if (
141
+ userOpts["__unstable_donotuse_reportAllBailouts"] != null &&
142
+ typeof userOpts["__unstable_donotuse_reportAllBailouts"] === "boolean"
143
+ ) {
144
+ __unstable_donotuse_reportAllBailouts =
145
+ userOpts["__unstable_donotuse_reportAllBailouts"];
146
+ }
147
+
148
const options: PluginOptions = {
149
...parsePluginOptions(userOpts),
150
...COMPILER_OPTIONS,
@@ -139,6 +155,19 @@ const rule: Rule.RuleModule = {
155
userLogger?.logEvent(filename, event);
156
if (event.kind === "CompileError") {
157
const detail = event.detail;
158
+ const suggest = makeSuggestions(detail);
159
+ if (__unstable_donotuse_reportAllBailouts && event.fnLoc != null) {
160
+ const locStr =
161
+ detail.loc != null && typeof detail.loc !== "symbol"
162
+ ? ` (@:${detail.loc.start.line}:${detail.loc.start.column})`
163
+ : "";
164
+ context.report({
165
+ message: `[ReactCompilerBailout] ${detail.reason}${locStr}`,
166
+ loc: event.fnLoc,
167
+ suggest,
168
+ });
169
+ }
170
+
171
if (!isReportableDiagnostic(detail)) {
172
return;
173
}
@@ -154,7 +183,7 @@ const rule: Rule.RuleModule = {
183
context.report({
184
message: detail.reason,
185
loc,
157
- suggest: makeSuggestions(detail),
186
+ suggest,
187
});
188
}
189
}