[compiler][eslint] Report bailout diagnostics with correct column # (#30977)
Compiler bailout diagnostics should now highlight only the first line of the source location span. (Resubmission of #30423 which was reverted due to invalid column number.)
mofeiZ committed
Sep 16, 2024 at 15:56 UTC
a99d8e8d97055127a8ad7b01835d2660154689ed
1 file changed
+25
-1
compiler/packages/eslint-plugin-react-compiler/src/rules/ReactCompilerRule.ts
+25
-1
@@ -166,9 +166,33 @@ const rule: Rule.RuleModule = {
166
detail.loc != null && typeof detail.loc !== 'symbol'
167
? ` (@:${detail.loc.start.line}:${detail.loc.start.column})`
168
: '';
169
+ /**
170
+ * Report bailouts with a smaller span (just the first line).
171
+ * Compiler bailout lints only serve to flag that a react function
172
+ * has not been optimized by the compiler for codebases which depend
173
+ * on compiler memo heavily for perf. These lints are also often not
174
+ * actionable.
175
+ */
176
+ let endLoc;
177
+ if (event.fnLoc.end.line === event.fnLoc.start.line) {
178
+ endLoc = event.fnLoc.end;
179
+ } else {
180
+ endLoc = {
181
+ line: event.fnLoc.start.line,
182
+ // Babel loc line numbers are 1-indexed
183
+ column: sourceCode.split(
184
+ /\r?\n|\r|\n/g,
185
+ event.fnLoc.start.line,
186
+ )[event.fnLoc.start.line - 1].length,
187
+ };
188
+ }
189
+ const firstLineLoc = {
190
+ start: event.fnLoc.start,
191
+ end: endLoc,
192
+ };
193
context.report({
194
message: `[ReactCompilerBailout] ${detail.reason}${locStr}`,
171
- loc: event.fnLoc,
195
+ loc: firstLineLoc,
196
suggest,
197
});
198
}