Repository navigation
Inline requires: Treat optional chains, logical assignments and defaults as branches when memoizing - #2280
Conversation
…lts as branches when memoizing With memoized inline requires, the first reference to a module in a scope assigns the memo variable and later references in that scope read it directly. A reference inside an optional call or member, the right-hand side of `||=`, `&&=` or `??=`, or a default value may never be evaluated, so the later references read `undefined`. Treat these like the other branches.
Metro review: Looks goodRisk: low · Commit: With Findings
Verified
Not run: No benchmark: the change adds a few node-type checks to the ancestor walk of each memoized reference, which doesn't plausibly change transform cost. I didn't reproduce the end-to-end dev server run with Hermes from the description. The unit tests and the base/head comparison cover the same behavior. I didn't run the typecheck or lint, since no Flow types change beyond node-type checks. Automated review by Claude. Advisory only: it never approves or blocks, and a maintainer makes the call. |
Summary
With
unstable_memoizeInlineRequires, the inline-requires plugin rewrites the first reference to a module in a scope as(_m || (_m = require(...)))and later references in that scope as plain_m.isDirectlyEnclosedByBlockdecides whether a reference always runs, butisBranchonly knowsif,?:,&&/||/??,switchand loops. These can skip their operand too:cb?.(greet),cb?.[key],cb?.x.y(greet)x ||= greet,x &&= greet,x ??= greetconst {x = greet} = objWhen the first reference is inside one of these and gets skipped, the later references read
undefined. The Hermes profile (unstable_transformProfile=hermes-stable) keeps?.and??=, so this appis bundled as
and throws
TypeError: Cannot read properties of undefined (reading 'greet').The fix adds these node types to
isBranch, so a reference inside them never marks its scope as having loaded the module. A reference in the part that always runs (the callee ofa?.(), the left ofx ||= y) is now also treated as a branch. That is conservative, as for the left side of&&today: it keeps the full(_m || (_m = require(...)))form.flowchart TD A[First use of an import is in a skipped optional call] --> B{Later use in the same function} B -->|Before| C[Reads the memo variable while it is still undefined] B -->|After| D[Loads the module itself]Changelog: [Fix] Memoized inline requires: a module first referenced in a skipped optional chain, logical assignment or default value is no longer
undefinedat later referencesTest plan
Added
memoizes a require whose first reference, in %s, is skippedtoinline-requires-plugin-test.js: it transforms a module withmemoizeCalls: true, runs it withvm, and checks that both an aliased (const foo = require(...)) and a destructured (const {a} = require(...)) module are loaded after a skipped first reference. Six cases: an optional call, an optional member, a later link of an optional chain, a logical assignment, a default value, and an optional call with a destructured require. All six fail onmainand pass with this change.I did not run the full
yarn test.End to end with the Metro dev server from this branch, a config with
transform: {inlineRequires: true, unstable_memoizeInlineRequires: true}, and a localgreeterpackage (exports.greet = name => 'Hello, ' + name):Narrated recording, before then after (sound on):
evidence.mp4
Edge cases and unchanged behaviour checked in the recording (the dev server from this branch):
format ??= greet(a formatter is passed) printsHi Metro | Hello, Metro; onmainit throws the sameTypeErrorHello, Metrocallback: Hello, youandHello, Metro, as onmainmetro buildwith the default profile, which lowers?.before inline requires runs, printsHello, Metro, as onmainThe optional member, later chain link and default value cases are covered by the unit tests only.
AI assistance (Claude Code) was used to find this bug, write the change and run the checks above.
🤖 Generated with Claude Code