Repository navigation
Keep side effects in sequence operands when folding constants - #2278
Conversation
Babel evaluates a sequence expression to its last operand, so constantFoldingPlugin treated a test like `(n++, false)` as a constant and dropped `n++`. Minifiers join statements into an `if` test this way, so `if(n++,"production"!==process.env.NODE_ENV)` in a prebuilt package lost its increment in production builds. Treat update, delete, new, await, yield and tagged template expressions as unsafe to fold, like calls and assignments already are.
Metro review: Looks goodRisk: low · Commit: The constant-folding plugin's side-effect check now also stops folding on update, Findings
Verified
Not run: No benchmark: the change adds a few visitor keys to a traversal of each folded test expression, which already ran, so per-file cost barely changes. I didn't reproduce the end-to-end Automated review by Claude. Advisory only: it never approves or blocks, and a maintainer makes the call. |
| * resulting in the expression call being skipped. | ||
| */ | ||
| OptionalCallExpression: unsafe, | ||
| // Babel evaluates a sequence to its last operand, so side effects in |
There was a problem hiding this comment.
nit · Property access in a leading sequence operand is still dropped
The check is a deny-list, so some leading operands that can still have effects are folded away. The closest to real minifier output is a member access: if ((o.x, false)) { a(); } still becomes nothing. That drops a getter call, or the TypeError thrown when o is nullish. (`${o}`, false) (which calls toString) and ("x" in p, false) (which runs a Proxy trap) are dropped too. This isn't new in this PR, and these cases are rarer than n++. If you want the sequence case closed off fully, check that the leading operands are pure, for example path.get('expressions').slice(0, -1).every(p => p.scope.isPure(p.node)) in a SequenceExpression entry. That is an allow-list, so it handles cases nobody thought to list. Otherwise, the comment could say which cases the list deliberately leaves out.
Reproduced: A probe test (packages/metro-transform-plugins/src/__tests__/review-probe-test.js, run in both checkouts) printed the plugin's output. Head and base both reduce if ((o.x, false)) { a(); }, if ((`${o}`, false)) { a(); } and if (("x" in p, false)) { a(); } to empty output. Head keeps (i++, false), !(i++, 1), typeof (i++, 1) and (i++, 1) ?? 2, all of which base folded. · id 0b01608a
|
This is the first time I've seen an AI-narrated screen recording in a PR! This is good stuff though - nice and tight diff, readable description, proven testing. Thanks! |
Summary
constantFoldingPluginfolds anif/ternary/logical test when Babel can evaluate it. Babel evaluates a sequence expression to its last operand, and the plugin's side-effect check only looked for calls and assignments. So a test like(n++, false)counted as the constantfalse, and then++was dropped with the dead branch.Minifiers produce exactly this shape: they join a statement into the following
iftest. A prebuilt package withbecomes
if (n++, false)once Metro inlinesNODE_ENVin a production build, and the wholeifwas removed, so the counter never moved. Dev builds were fine.The fix marks update (
++/--),delete,new,await,yieldand tagged template expressions as unsafe to fold, next to the existingCallExpression/AssignmentExpression/OptionalCallExpressionentries. A sequence whose leading operands have no side effects ((0, false)) still folds. Theunsafecallback's parameter type is widened toNodePath<>so it accepts the new node types.flowchart TD A[Minified test n++ then a NODE_ENV check] --> B{Production build folds the test} B -->|Before| C[Branch and n++ removed so the app prints 0 0 0] B -->|After| D[Test kept so the app prints 1 2 3]Changelog: [Fix] Keep side effects such as
i++in the leading operands of a sequence when folding constants in production buildsTest plan
Added two tests to
constant-folding-plugin-test.js:does not drop side effects from the leading operands of a sequence:if ((i++, false)),(delete o.x, true) ? 1 : 2,(new Foo(), null) || 3,(--j, 1) + 2,(tag`x`, 0) || 4, andawait/yieldin aniftest are all left alone. It fails onmain(every one of them is folded and the side effect is gone) and passes with this change.folds a sequence whose leading operands have no side effects:(0, 'production' !== 'production') ? 1 : 2still becomes2.I did not run the full
yarn test.End to end with the Metro CLI from this branch (
metro build, a localnode_modules/counter-libholding the minified line above, andconsole.log(nextId(), nextId(), nextId())as the entry):Narrated recording, before then after (sound on):
evidence.mp4
Edge cases and unchanged behaviour checked in the recording (the CLI from this branch): an unminified production build (
--minify false) also prints1 2 3; the minified production bundle still has nodev onlycode in it (grep -cprints0, the minifier removes the dead branch); a dev build is unchanged (warns three times, prints1 2 3). The other side-effect kinds (delete,new,--, tagged templates,await,yield) are covered by the unit test only.AI assistance (Claude Code) was used to find this bug, write the change and run the checks above.
🤖 Generated with Claude Code