Skip to content

Commit a83297e

Browse files
committed
fix: resolve shadowed callee bindings in module-facts and allow unstable_crossModuleHookFacts in rspack
Bug 1: extractModuleFacts keyed octaneLocals/importedHookOrigins lookups by callee spelling alone. A local variable or parameter that shadows an imported hook name still inherited the import's stability or forwards fact. Add calleeIsModuleImport() that walks the scope chain to verify the binding resolves to the module scope before trusting the import maps. Bug 2: unstable_crossModuleHookFacts was absent from the rspack LOADER_OPTION_KEYS allowlist and never forwarded in the plugin's loaderOptions. Add it to the allowlist, validate it as boolean, include it in the normalized output, and forward it from the plugin to the loader.
1 parent 120e00f commit a83297e

3 files changed

Lines changed: 27 additions & 2 deletions

File tree

‎packages/octane/src/compiler/module-facts.js‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,17 @@ export function extractModuleFacts(source, id) {
243243
markEmptyDepsStable(ast, analysis);
244244
const scopeOfNode = (node) => analysis.nodeScopes.get(node) ?? null;
245245

246+
function calleeIsModuleImport(node) {
247+
const scope = scopeOfNode(node);
248+
if (!scope) return false;
249+
for (let current = scope; current !== null; current = current.parent) {
250+
if (current.bindings.get(node.name) !== undefined) {
251+
return current === analysis.moduleScope;
252+
}
253+
}
254+
return false;
255+
}
256+
246257
// Which octane hooks are in lexical scope, and under what local name.
247258
const octaneLocals = new Map();
248259
const importedHookOrigins = new Map();
@@ -290,7 +301,10 @@ export function extractModuleFacts(source, id) {
290301
const only = unwrap(returns[0]);
291302
if (only?.type === 'CallExpression') {
292303
const callee = unwrap(only.callee);
293-
const origin = callee?.type === 'Identifier' ? importedHookOrigins.get(callee.name) : null;
304+
const origin =
305+
callee?.type === 'Identifier' && calleeIsModuleImport(callee)
306+
? importedHookOrigins.get(callee.name)
307+
: null;
294308
if (origin) {
295309
facts.set(name, {
296310
kind: 'hook',
@@ -301,7 +315,10 @@ export function extractModuleFacts(source, id) {
301315
continue;
302316
}
303317
// A direct octane hook result: `return useRef(null)`.
304-
const octaneName = callee?.type === 'Identifier' ? octaneLocals.get(callee.name) : null;
318+
const octaneName =
319+
callee?.type === 'Identifier' && calleeIsModuleImport(callee)
320+
? octaneLocals.get(callee.name)
321+
: null;
305322
if (octaneName && LIFETIME_STABLE_HOOKS.has(octaneName)) {
306323
facts.set(name, {
307324
kind: 'hook',

‎packages/rspack-plugin-octane/src/plugin.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,9 @@ export class OctaneRspackPlugin {
329329
...(this.options.requireDirective === undefined
330330
? null
331331
: { requireDirective: this.options.requireDirective }),
332+
...(this.options.unstable_crossModuleHookFacts === undefined
333+
? null
334+
: { unstable_crossModuleHookFacts: this.options.unstable_crossModuleHookFacts }),
332335
};
333336
compiler.options.module.rules.push({
334337
test: OCTANE_RULE,

‎packages/rspack-plugin-octane/src/shared.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ const LOADER_OPTION_KEYS = new Set([
4545
'requireDirective',
4646
'universalRuntime',
4747
'layerSpecializations',
48+
'unstable_crossModuleHookFacts',
4849
]);
4950
const PLUGIN_OPTION_KEYS = new Set([...LOADER_OPTION_KEYS, 'runtime', 'transpile']);
5051
const LAYER_SPECIALIZATION_KEYS = new Set(['runtime', 'renderers', 'universalRuntime']);
@@ -178,6 +179,7 @@ function normalizeOptions(value, plugin) {
178179
assertBooleanOption(options, 'dev');
179180
assertBooleanOption(options, 'profile');
180181
assertBooleanOption(options, 'requireDirective');
182+
assertBooleanOption(options, 'unstable_crossModuleHookFacts');
181183
if (
182184
options.exclude !== undefined &&
183185
(!Array.isArray(options.exclude) || options.exclude.some((entry) => typeof entry !== 'string'))
@@ -203,6 +205,9 @@ function normalizeOptions(value, plugin) {
203205
...(options.requireDirective === undefined
204206
? null
205207
: { requireDirective: options.requireDirective }),
208+
...(options.unstable_crossModuleHookFacts === undefined
209+
? null
210+
: { unstable_crossModuleHookFacts: options.unstable_crossModuleHookFacts }),
206211
...(plugin && options.transpile !== undefined ? { transpile: options.transpile } : null),
207212
...(plugin && options.runtime !== undefined ? { runtime: options.runtime } : null),
208213
};

0 commit comments

Comments
 (0)