react-server675fbba4
react-servercommit0d059c73b187

fix: use cache plugin (#387)

The use-cache-inline plugin had several bugs in how it tracks closure variables and handles exported cached functions. The root cause was a flat locals array that accumulated variable names from all function scopes without any scoping — variables declared inside one function would leak into the closure capture list of completely unrelated sibling functions. This caused the plugin to emit invalid .bind() calls on module-level exported cached functions, producing parse errors like export __cache_name__.bind(null, src, selector, isolate) during production builds. The fix replaces the flat locals array with a proper scope stack. Each non-cached function pushes a scope entry on enter and pops it on leave, so only variables from actual enclosing lexical scopes are considered for closure capture. Module-level cached functions now correctly see an empty scope stack and skip .bind() entirely.

Additional fixes included in this change: destructured binding patterns (ObjectPattern, ArrayPattern, AssignmentPattern, RestElement) are now properly walked when collecting local variable names. Function parameters in enclosing scopes are tracked for nested cached function closure capture. Identifiers in non-reference positions (object property keys, member expression properties, declaration ids, function names) are excluded from closure matching — this was necessary because the JSX transform runs before the cache plugin, turning JSX attribute names into regular Property key Identifiers that would otherwise be falsely captured. Exported cached functions are now wrapped in __react_cache__() at module level and get correct CACHE_KEY/CACHE_PROVIDER assignments using scoping-aware target resolution.

Author
Viktor Lázár <lazarv1982@gmail.com>
Date
Commit
0d059c73b187dbce946f830409d6bf84f3e5c6db
6 files changed+396 -43
Mpackages/react-server/lib/plugins/use-cache-inline.mjs+195 -43
Atest/__test__/use-cache-locals.spec.mjs+34 -0