Skip to content

Commit 3a7a60a

Browse files
committed
fix(compiler): peel TS wrappers around dependency arrays in ref classification
The ArrayExpression case in classify() took parents[index - 1] as the hook call, but when the dependency array is wrapped in as const, satisfies, !, or extra parentheses, that parent is the TypeScript or grouping node rather than the CallExpression. This caused the ref to be marked as escaped, preventing isValueRef from returning true and silencing the OCTANE_STRONG_EFFECT_HIDDEN_DEPENDENCY diagnostic. Walk up through transparent wrappers above the ArrayExpression before looking up the call in callNames, mirroring the existing identifier peeling at the top of classify().
1 parent 041edbd commit 3a7a60a

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

‎packages/octane/src/compiler/strong-effects.js‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -611,9 +611,16 @@ export function createStrongEffectPolicy({ ast, analysis, callNames, report }) {
611611
return;
612612
case 'ArrayExpression': {
613613
// A dependency list is not a use of the ref's identity.
614-
const call = parents[index - 1];
614+
// Peel transparent wrappers (as const, satisfies, !, parens)
615+
// around the array to reach the enclosing call.
616+
let arg = parent;
617+
let callIndex = index - 1;
618+
while (callIndex >= 0 && TRANSPARENT.has(parents[callIndex].type)) {
619+
arg = parents[callIndex--];
620+
}
621+
const call = parents[callIndex];
615622
const position = DEPENDENCY_ARGUMENTS.get(callNames.get(call));
616-
if (position !== undefined && call.arguments?.[position] === parent) return;
623+
if (position !== undefined && call.arguments?.[position] === arg) return;
617624
break;
618625
}
619626
}

‎packages/octane/tests/compiler/strong-effects.test.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,15 @@ export function App(props) @{
986986
).toContain(HIDDEN);
987987
});
988988

989+
it.each([
990+
['as const', `useEffect(() => { props.log(last.current); }, [last] as const);`],
991+
['satisfies', `useEffect(() => { props.log(last.current); }, [last] satisfies any);`],
992+
['non-null assertion', `useEffect(() => { props.log(last.current); }, [last]!);`],
993+
['parenthesized', `useEffect(() => { props.log(last.current); }, ([last]));`],
994+
])('does not treat a dependency list wrapped with %s as attaching a ref', (_label, setup) => {
995+
expect(errors(app(`const last = useRef(0); ${setup}`))).toContain(HIDDEN);
996+
});
997+
989998
it.each([
990999
['ref writes', `const last = useRef(0); useEffect(() => { last.current = count; });`],
9911000
[

0 commit comments

Comments
 (0)