Skip to content

Commit a391dea

Browse files
committed
fix: reset updatedStates between exclusive switch cases
SwitchStatement visited every case on a shared switchScope, so recordUpdate marked setter calls on that scope and later sibling cases saw them via checkReadAfterUpdate even when break made the update unreachable. This emitted a false OCTANE_STRONG_READ_AFTER_SET hint for mutually exclusive cases—the same control flow if/else already excludes because each arm creates its own nested block scope. Reset switchScope.updatedStates to undefined after each terminated case (break/return/throw/continue) so the next case starts without inherited updates. Fallthrough cases still accumulate correctly because the reset only fires when the case exits.
1 parent 02a8491 commit a391dea

2 files changed

Lines changed: 10 additions & 0 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5557,6 +5557,7 @@ export function analyzeStrongMode(ast, source, filename, options = {}) {
55575557
last?.type === 'ThrowStatement'
55585558
? null
55595559
: executionPhase;
5560+
if (fallthroughPhase === null) switchScope.updatedStates = undefined;
55605561
// return, throw, and continue never reach the statement after the switch.
55615562
if (
55625563
before !== undefined &&

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,11 @@ describe('Strong reads after a synchronous state update', () => {
893893
`export function A({ save }) { const [n, setN] = useState(0); return <button onClick={async () => { await save(); setN(0); save(n); }}>{n}</button>; }`,
894894
'n); }',
895895
],
896+
[
897+
'a switch fallthrough into a read',
898+
`export function A({ action, log }) { const [count, setCount] = useState(0); return <button onClick={() => { switch (action) { case 'reset': setCount(0); case 'log': log(count); break; } }}>{count}</button>; }`,
899+
'count); break',
900+
],
896901
])('hints at %s', (_name, body, needles) => {
897902
const expected = (Array.isArray(needles) ? needles : [needles]).map((needle) =>
898903
at(body, needle),
@@ -945,6 +950,10 @@ describe('Strong reads after a synchronous state update', () => {
945950
'a nested function after the update',
946951
`export function A({ later }) { const [n, setN] = useState(0); return <button onClick={() => { setN(1); later(() => n); }}>{n}</button>; }`,
947952
],
953+
[
954+
'a read in an exclusive switch case',
955+
`export function A({ action, log }) { const [count, setCount] = useState(0); return <button onClick={() => { switch (action) { case 'reset': setCount(0); break; default: log(count); break; } }}>{count}</button>; }`,
956+
],
948957
])('does not hint at %s', (_name, body) => {
949958
expect(hintsAt(body)).toEqual([]);
950959
});

0 commit comments

Comments
 (0)