Skip to content

react-compiler: decide an eslint suppression by the place of the comment - #45943

Open
robobun wants to merge 4 commits into
robobun/8af83e6b/react-compiler-param-default-candidatefrom
robobun/0e4be85e/react-compiler-suppression-in-params
Open

robobun wants to merge 4 commits into
robobun/8af83e6b/react-compiler-param-default-candidatefrom
robobun/0e4be85e/react-compiler-suppression-in-params

Conversation

@robobun

@robobun robobun commented Oct 18, 2026 •

Copy link
Copy Markdown
Collaborator

Depends on #45550: its branch is the base of this PR. Related to #36190.

Problem

  • With bun build --react-compiler, an eslint-disable-next-line react-hooks/... comment marks the wrong functions: all later ones with cjs or iife output, the one right above it, and none from inside a JSX tag.
  • The parser marks each function that closes while a lexer flag is set. The first close into top-level code clears the flag (parse_fn.rs:421), and only an esm parse has top-level code.

Fix

  • The lexer records where the line after the comment ends. A function that starts ahead of that position is marked: the comment is inside it or right above it.
  • The parser tests this ahead of the closing brace and writes no suppression state, so take_react_hooks_suppression of react-compiler: do not let a function in a parameter list take the component's candidate name or suppression #45550 is removed. The JSX tag lexer runs the same scan.
  • A bare eslint-disable opts the file out, as upstream does.
  • Verified: SuppressionFollowsThePlaceOfTheComment-{esm,cjs,iife} in react-compiler.test.ts, and the two fixture suites. Self-reviewed: 9 concerns raised, 9 addressed.

Background

  • Upstream 1.0.0 counts a comment only for a function it is inside. It compiles a one-line hook below the comment: useMemo(() => value, []) becomes value.
  • Considered: upstream's list of comment ranges. It needs a Vec and the end of each function.

Downsides

  • Left as written now: a function right below the comment (upstream compiles it), and a function above a bare eslint-disable (main compiled it).
  • A comment right below an expression-bodied arrow still counts for that arrow.
  • None found for speed and size: with the compiler off, each close reads the lexer once (main: twice), and .text of bun_js_parser is 157 bytes smaller than on main.
Notes

The rule. A next-line comment for react-hooks/rules-of-hooks or react-hooks/exhaustive-deps counts for a function when:

  • the comment is inside the function: its parameter list, its body, a function nested in either, or a JSX tag
  • or the function starts on the line that the comment is for (or after the comment, on the line of the comment)

A bare eslint-disable for one of these rules counts for every function of the file.

Two choices for a maintainer.

  1. The line. Upstream (babel-plugin-react-compiler 1.0.0) and the lint rule react-hooks/rule-suppression count a next-line comment only for a function it is inside. The second case above keeps what main does under esm for a comment above a function. Without it the build changes what this hook returns:

    // eslint-disable-next-line react-hooks/exhaustive-deps
    export const useInitial = value => useMemo(() => value, []);

    Upstream prints value => { return value; }. Main, react-compiler: do not let a function in a parameter list take the component's candidate name or suppression #45550 and this PR print the hook as written. The four Above forms of the test of react-compiler: do not let a function in a parameter list take the component's candidate name or suppression #45550 keep their value. To follow upstream in full, store the start of the comment in scan_react_hooks_suppression: one expression.

  2. The bare eslint-disable. Upstream and the text of the lint rule count it for every function of the file. Main counts it for the functions after it, and for the one right above it when no token is between. This PR follows upstream: the comment sets the module-scope opt-out that "use no memo" sets, where the per-file state is made (parse_entry.rs:981). Nothing is added to the parse path.

History. No issue reports this. The review of #45550 found two of the layouts (#45550 (comment)). #45550 keeps the consume, and a function that closes in a parameter list records where it closed (react_hooks_suppression_taken_at), so that the owner of the list still gets the suppression. This PR has no consume, so that field and take_react_hooks_suppression go away.

What changes on the base. C is // eslint-disable-next-line react-hooks/rules-of-hooks.

layout esm before cjs and iife before after, each format
C inside A (body, parameter list, default) A as written A as written same
a function after A compiled as written compiled
C between the JSX attributes of A A compiled A compiled A as written
function B() {}, C, function A() {} B as written, A compiled both as written B compiled, A as written
function T() {} // C, then N on the next line T as written, N compiled both as written T compiled, N as written
a statement that ends in ;, C, A A as written A as written same
C, a statement that is not a function, A A as written A as written A compiled
C, a doc comment, A A as written A as written A compiled
C, const A = memo(, the function on the next line A as written A as written A compiled
C in a class static block, then A A as written A as written A compiled
C, an empty line, A A as written A as written A compiled
a bare eslint-disable, then A A as written A as written same
A, then a bare eslint-disable right below A as written A as written same
A, a statement, then a bare eslint-disable A compiled A compiled A as written

A comment that is inside T, on a line of T that is right above N, also counts for N: it is for the line that N starts on.

Reference. 125 probe files. Each was built with its comments and with the comments made inert, by babel-plugin-react-compiler 1.0.0 and by a debug build of the base (f9b900b, the head of #45550 at that time) and of this branch (ae354c6). 215 functions show a decision in both tools.

base, esm base, cjs and iife this PR, each format
decisions that differ from the reference 37 151 30
of those: compiled, where the reference leaves the function as written 8 6 0

The 30 of this PR are all left as written:

  • 28 functions that start after the comment, on its line or on the line that it is for.
  • 1 arrow with an expression body right above a comment (see below).
  • 1 directive that names another rule first (eslint-disable-next-line no-console, react-hooks/rules-of-hooks): bun and ESLint honour it.

Under esm, 25 decisions move to the reference and 18 move away from it. Each of the 18 is a function that starts on the line that a comment is for. On the base another function took that comment first. Under cjs and iife, 121 move to the reference and none away.

Not changed. An arrow with an expression body closes at the token after the expression, and the lexer scans a comment on the way to that token. So a comment right below such an arrow, with no ; between, counts for the arrow. The test has this form with its value. An exact test needs the end of the previous token, which the lexer does not keep.

react-hooks/exhaustive-deps. It stays in the matcher. With default options, babel-plugin-react-compiler 1.0.0 skips a component that has this comment inside (CompileError: Suppression). Upstream compiles the fixture exhaustive-deps/compile-files-with-exhaustive-deps-violation-in-effects.js because of its @validateExhaustiveMemoizationDependencies. bun has that validation off (src/react_compiler/hir/environment_config.rs:155).

The text of the comment. The match is as before: eslint-disable anywhere in the comment, then the name of the rule anywhere after it. So a comment that only mentions the directive counts. The pattern of babel-plugin-react-compiler 1.0.0 is not anchored either, and ESLint and the lint rule anchor theirs. To anchor it is a separate choice.

Lines. The end of a line is a line break of JavaScript: \n, \r\n, \r, U+2028 or U+2029 (strings::find_js_line_break). Each matching comment searches for two of them. Once a comment reaches the end of the file, no later comment searches.

#36190. It also ends the cjs and iife leak, by another route: each bundler parse becomes a module parse, so the flag is cleared. With this PR the suppression does not read is_top_level. The two change different source files. The react-compiler tests of the one that lands second pass before it.

Cost with the compiler off. From cargo build --release -p bun_js_parser, then objdump, nm -S and size on the rlib member, and rustc -Zprint-type-sizes. The counts are the instructions that serve the suppression on the path with no comment.

main (91e7e3b) #45550 (626f0b7) this PR
parse_fn, per function 4 instructions, 2 loads, 2 branches 4, 2, 2 4, 1, 1
parse_arrow_body, per arrow 5 instructions, 2 loads, 2 branches 3, 2, 1 5, 1, 1
.text of the rlib 1,625,324 1,626,455 1,625,167
parse_fn (JS / TS) 2,467 / 4,034 2,593 / 4,210 2,636 / 4,188
parse_fn_body 552 552 497
parse_arrow_body 1,279 1,292 1,291
take_react_hooks_suppression 159
scan_comment_text 1,319 1,335 1,034, and 602 for the scan it calls
next_inside_jsx_element 5,277 5,277 5,411
Lexer / LexerSnapshot / ParserSnapshot 336 / 240 / 416 336 / 240 / 424 336 / 240 / 416

For this PR, one of the 4 instructions of parse_fn and one of the 5 of parse_arrow_body keep the start argument. A parse of typescript.js (9.1 MB) calls parse_fn 12,597 times, parse_arrow_body 9,120 times and parse_paren_expr 11,427 times (gdb breakpoint counts). No allocation on any path.

The start of a function. parse_fn and parse_arrow_body get it as an argument: the function or async token, the first token of an arrow, or the key of a method. Each caller had it already.

TS namespace and enum bodies. #34250 gave them a fresh FnOrArrowDataParse, as esbuild does, and kept is_top_level only for the consume. Each other reader of is_top_level is behind allow_await == AllowExpr, which these bodies do not have.

Tests. On a debug build of the base (626f0b7), SuppressionFollowsThePlaceOfTheComment-esm differs in 10 of 56 lines, -cjs in 29 and -iife in 29. SuppressionAheadOfAFunctionInAParameterList and SuppressionInsideTSNamespaceDoesNotLeak pass before and after.

Suites run on a debug build of this branch. react-compiler.test.ts (124 pass, 1 skip, 1 todo), react-compiler-fixtures.test.ts (3,293 pass, with a longer hook timeout), react-compiler-fixtures-run.test.ts (849 pass), transpiler.test.js, test/bundler/esbuild/ts.test.ts, test/bundler/bundler_jsx.test.ts. No failure. In my container the debug build is slow to start, and some runs hit time limits on the base build too: the 120 s hook of react-compiler-fixtures.test.ts, and the 5 s limit of tests in test/cli/lint/react-compiler.test.ts and jsx-production.test.ts.


no test proof · iteration 0 · platform-specific test(s) that do not run on this machine, deferring to CI, which covers all platforms: test/bundler/transpiler/react-compiler.test.ts

The lexer records how far the last react-hooks `eslint-disable-next-line`
comment reaches: the end of the line that the comment is for. A function is
marked when it starts at or ahead of that position, tested before the parser
steps over the closing brace of its body. So a comment counts for each
function it is inside, and for a function that starts on the line it is for.
The JSX tag lexer passes its comments to the same scan.

The parser no longer writes suppression state: no function consumes the
suppression, and no parameter list gives it back. With cjs and iife output,
the functions after a suppressed function are compiled again.
@robobun

robobun commented Oct 18, 2026 •

Copy link
Copy Markdown
Collaborator Author
Updated 9:45 AM PT - Oct 21st, 2026

❌ @robobun, your commit a1d9f06 has 1 failures in Build #130230 (All Failures):

  • step failed outside runner - exit 1 on ⚙️

🧪   To try this PR locally:

bunx bun-pr 45943

That installs a local version of the PR into your bun-45943 executable, so you can run:

bun-45943 --bun

@robobun

robobun commented Oct 18, 2026

Copy link
Copy Markdown
Collaborator Author

Status: ready for review. It depends on #45550, whose branch is the base of this PR.

Reproduction on the base, with a.jsx:

export function First({ label }) {
  // eslint-disable-next-line react-hooks/rules-of-hooks
  const style = { color: label };
  return <button style={style}>{label}</button>;
}
export function Second({ label }) {
  const style = { color: label };
  return <button style={style}>{label}</button>;
}
  • bun build ./a.jsx --react-compiler --format=cjs --external react --external 'react/*' prints Second as written. With --format=esm it starts with let $ = _c(5).
  • Put the comment between the attributes of the <button> tag of First: First is compiled in each format.

With this PR, First is printed as written and Second is compiled, in each format and for both places of the comment.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.

Beyond the inline findings, I also checked the move of the closing-brace next() out of parse_fn_body — its only two callers (parse_fn, parse_arrow_body) now consume the } themselves — and the new react_hooks_suppressed_until field, which is carried through LexerSnapshot save/restore so TypeScript backtracking does not lose it.

Extended reasoning...

The change rewrites how the JS lexer and parser attribute eslint react-hooks suppression comments to functions for the React Compiler, threading a start Loc through every parse_fn/parse_arrow_body caller; it touches no security-sensitive surface. Four confirmed findings are posted inline, so a human look is already signalled; this note records only the two adjacent concerns examined and ruled out.

Comment thread src/js_parser/parse/parse_fn.rs Resolved
Comment thread src/js_parser/lexer.rs Resolved
Comment thread src/js_parser/lexer.rs Resolved
Comment thread src/js_parser/parse/parse_fn.rs Resolved
…nd lines as JavaScript does

A bare `eslint-disable` for a react-hooks rule sets the module-scope opt-out,
so it counts for each function of the file, as upstream does. The function
right above such a comment is left as written again.

The line that a next-line comment is for ends at a line break of JavaScript:
\n, \r\n, \r, U+2028 or U+2029. Once a comment reaches the end of the file,
no later comment searches for a line break.
@robobun

robobun commented Oct 18, 2026

Copy link
Copy Markdown
Collaborator Author

Reply to the review, for ae354c6:

  • Bare eslint-disable below a function: changed. The comment now counts for every function of the file, as upstream does, through the module-scope opt-out. The parse path has no new read.
  • Line ends: fixed. The search knows \r, U+2028 and U+2029, and stops once a comment reaches the end of the file.
  • react-hooks/exhaustive-deps: not changed. With default options, babel-plugin-react-compiler 1.0.0 also leaves that component as written. The cited fixture has @validateExhaustiveMemoizationDependencies.
  • Expression-bodied arrow right above a comment: not changed. An exact test needs the end of the previous token, which the lexer does not keep.

Each thread has the details. The PR body has the new tables.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.

Beyond the inline finding, I checked the follow-up commit against the two concerns from the previous round: the next-line end is now found with strings::find_js_line_break (CR and U+2028 covered, with a fixture for each), and a bare eslint-disable now opts the whole module out via parse_entry.rs, so a function closing before it is no longer compiled. I also traced the p.lexer.next() moved out of parse_fn_body: both callers (parse_fn and block-bodied parse_arrow_body, including the TypeScript backtrack path) call it after the decision, and LexerSnapshot saves and restores the new react_hooks_suppressed_until field so backtracking rescans correctly.

Extended reasoning...

The change replaces the lexer's two sticky suppression booleans with a single react_hooks_suppressed_until position and decides each function's React Compiler opt-out from its start location, removing the consume/give-back counter and is_top_level plumbing; it touches the JS parser and lexer only, with no security-sensitive surface. The one surviving finding is pre-existing (prose mentions of the directive match), and the new commit addressed both earlier inline concerns, but the parser hot path and the per-format behavior table warrant a human look.

Comment thread src/js_parser/lexer.rs Resolved
@robobun

robobun commented Oct 19, 2026

Copy link
Copy Markdown
Collaborator Author

An independent branch fixes the same bug: robobun/cc56a91e/react-compiler-suppression-scope. It has no PR, because this PR already covers the report.

The diff in one line. The lexer records the next-line comments. The parser records where functions end, only in a file that has such a comment. The visit pass decides per candidate with the containment rule of filterSuppressionsThatAffectFunction, and bun lint calls the same predicate.

It is choice 1 of "Two choices for a maintainer", taken the other way. It follows babel-plugin-react-compiler 1.0.0 in full, so it prints useInitial as value => { return value; }. It needs the same decision as this PR. Do not merge both.

Two parts of it are of use here, whatever the decision is:

  • The end of an expression-bodied arrow ("Not changed" above), with no new lexer field and nothing per token. When the arrow closes, test that only whitespace and comments lie between the recorded comment and the current token. If so, the comment is after the arrow. See Lexer::is_trivia and end_before_trailing_react_hooks_suppressions and the call in parse_arrow_body. This PR keeps the end of the suppressed line. The test also needs the end of the comment: one more position.
  • A table of 56 comment placements with the verdicts of 1.0.0 on @babel/core 7.29.7: react-compiler/SuppressionCommentPlacement-{esm,cjs}. A release build of this PR at ae354c6 gives the same result under esm and cjs and differs from the table on 17 rows. Each is a function left as written. 14 come from the line rule and 3 from the end of an expression-bodied arrow.
The 17 rows
row 1.0.0 compiles this PR compiles
next-line after function B none
next-line after async B none
next-line between the parameters and => B none
next-line after => B none
next-line in the type parameters of an arrow B none
next-line between async and function B none
next-line between export and function A B none
next-line between export and async function B useA none
next-line above a function A B B
next-line above export default function B default B
next-line between = and an arrow A B B
next-line after an overload A B B
next-line after the closing brace A B C A C
next-line above the first and in the second of three functions A C C
next-line after an arrow with an expression body A B none
two next-lines after an arrow with an expression body A B none
next-line and other comments after an arrow with an expression body A B B

In the first eight rows the comment is a /* */ on the line of A, and B starts on the next line.

The branch also takes the candidate before the parameters are visited, as #45550 does. That part is not needed here.

…param-default-candidate' into robobun/0e4be85e/react-compiler-suppression-in-params
@robobun

robobun commented Oct 20, 2026

Copy link
Copy Markdown
Collaborator Author

Reply to the second review:

  • Prose that mentions the directive: not changed. The pattern of babel-plugin-react-compiler 1.0.0 is not anchored either, so an anchored match would compile a function that the plugin leaves as written. The thread has the details, and the PR notes state the current match.

678352d merges the new head of #45550 (626f0b7). Its take_react_hooks_suppression and react_hooks_suppression_taken_at are removed here, because no function consumes a suppression. The tests of both PRs pass on the merge, and the tests of this PR differ on the new base in the same 10, 29 and 29 of 56 lines.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reviewed the latest push (ae354c6 plus the merge) and found no new bugs; since it changes the parse_fn_body contract (callers now consume the closing }) on every function parse and adds a scan to the JSX-mode lexer, a human look at the parser side is still worthwhile.

What was reviewed:

  • The two earlier inline points are addressed in code: a bare eslint-disable now feeds the module-scope opt-out in parse_entry.rs wherever it sits in the file, and the next-line end comes from strings::find_js_line_break (CR, CRLF, U+2028/2029, EOF fallback), with tests for both.
  • parse_fn_body has exactly two callers (parse_fn, parse_arrow_body); both decide the flag and then call lexer.next(), and no removed lexer/parser field is still referenced anywhere in src/.
  • Dropping the is_top_level propagation in namespace/enum bodies is safe: the three remaining readers are all gated on allow_await == AllowExpr, which those bodies reset to AllowIdent.
  • JSX-mode comment slices: self.end is on the line terminator after a // comment and just past */ after a block comment, so the end - 2 strip and the next-line search origin are right; react_hooks_suppressed_until is saved and restored in LexerSnapshot for backtracking.
Extended reasoning...

The change touches the JS lexer and parser (src/js_parser/lexer.rs, parse_fn.rs, parse_entry.rs and the call sites of parse_fn/parse_arrow_body) to replace sticky react-hooks suppression booleans with a single react_hooks_suppressed_until position, plus a 470-line test covering esm/cjs/iife, CR and U+2028 line ends, JSX-tag comments and TypeScript arrow paths. It touches no security-sensitive surface. The prior inline findings from this bot were addressed by commit ae354c6 and the bug hunt ran dry this round. Deferral rather than approval because the parse_fn_body lexer-advance contract changed for every function parse and the new scan runs inside the JSX lexer loop, which merits a maintainer's eye; no CODEOWNER covers the changed paths and no third-party objection is outstanding.

@robobun

robobun commented Oct 21, 2026

Copy link
Copy Markdown
Collaborator Author

A count of where these comments are in published code. It is for choice 1 of "Two choices for a maintainer" (the line) and for the second bullet of Downsides.

Sample, taken on 2026-10-19.

  • 3,088 npm packages: 2,306 from the npm search for five React keywords (react, react-native, react-hooks, hooks, react-component), and 782 picked by hand (React libraries and application packages).
  • The latest tarball of 3,039 of them. The other 49 were skipped (over 80 MB unpacked, or no tarball).
  • 384,901 JS and TS files scanned. 2,457 of them name one of the two rules.
  • The files with an eslint-disable comment were parsed with @babel/parser. A file that does not parse is not counted.
  • Each comment was tested with the matcher of scan_react_hooks_suppression.

Result. 3,399 next-line comments match, in 2,114 files of 349 packages. 3,388 of them also match the pattern of babel-plugin-react-compiler.

place of the comment comments
inside a module-scope function that is a compile candidate 2,803
inside another function 592
above a module-scope function, not inside a function 2
module scope, no function on the next line 2
  • The 2 comments above a function are one doc comment in two build flavors of @mui/x-data-grid (utils/utils.js and utils/utils.mjs). It names the directive in prose, above function eslintUseValue(_) {}, which is not a component or a hook.
  • Per candidate: the line rule and the containment rule of upstream stop the same 2,122 functions. The line rule stops 2 more, the two copies of eslintUseValue. The compiler takes neither copy, with or without the comment.
  • No comment is right below an arrow with an expression body at module scope. Of the 4 comments at module scope, 2 are that doc comment (it follows the closing brace of a function declaration) and 2 are inside an object literal (react-native and react-native-tvos, Libraries/Animated/NativeAnimatedAllowlist.js).
  • Counted for one build flavor per package: 1,714 comments, and 1 of them is above a function.

So in this sample choice 1 changes the output of no function, and the limit for an expression-bodied arrow changes none. Limit of the sample: these are npm packages, and application code can differ.

This comes from a separate look at the fault that this PR fixes (a comment right below a function declaration marks that declaration). This PR covers it, so there is no second PR.

…param-default-candidate' into robobun/0e4be85e/react-compiler-suppression-in-params
@robobun

robobun commented Oct 21, 2026

Copy link
Copy Markdown
Collaborator Author

a1d9f06 merges the new head of #45550 (a363e4b). The files of this PR did not change: the merge has the same tree as 678352d.

On the third review: no finding is open. The two failures of the last build are not from this diff: an artifact download timed out on the darwin aarch64 test lane, and test/js/node/http/node-http-uaf.test.ts timed out on darwin x64, as it does on main.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review found no issues

No high-confidence issues detected in this change.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant