Skip to content

Commit 13af307

Browse files
committed
fix(compiler): unwrap ternary, sequence, and logical expressions in abort-proof walk
sourceOf now follows SequenceExpression (last element) and passes ConditionalExpression/LogicalExpression through to requestControllers, which intersects the controller sets from each branch. This matches the unwrapping that settled and zeroDelayPromise already perform, so `await (cond ? fetch(a, { signal }) : fetch(b, { signal }))` and `await (0, fetch(url, { signal }))` correctly find their controllers instead of producing a false OCTANE_STRONG_EFFECT_DATA_FETCH.
1 parent 67a03f6 commit 13af307

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,19 +373,40 @@ export function createStrongEffectPolicy({ ast, analysis, callNames, report }) {
373373
node = unwrap(callee.object);
374374
}
375375
if (depth < 8 && node?.type === 'AwaitExpression') return sourceOf(node.argument, depth + 1);
376+
if (depth < 8 && node?.type === 'SequenceExpression')
377+
return sourceOf(node.expressions?.at(-1), depth + 1);
376378
if (node?.type === 'Identifier' && depth < 8) {
377379
const init = stableInitOf(node);
378380
if (init !== null) return sourceOf(init, depth + 1);
379381
}
382+
if (node?.type === 'ConditionalExpression' || node?.type === 'LogicalExpression') return node;
380383
return node?.type === 'CallExpression' || node?.type === 'NewExpression' ? node : null;
381384
}
382385

386+
// Intersect two controller sets: keeps only controllers present in both.
387+
function intersectControllers(left, right) {
388+
if (left === null || right === null) return null;
389+
let controllers = null;
390+
for (const c of left) if (right.has(c)) (controllers ??= new Set()).add(c);
391+
return controllers;
392+
}
393+
383394
// Controllers whose abort rejects the promise a continuation follows: the
384395
// request's own signal, or the signal of the request whose result it reads,
385396
// as in `(await fetch(url, { signal })).json()`.
386397
function requestControllers(expression, depth = 0) {
387398
const source = depth > 8 ? null : sourceOf(expression);
388399
if (source === null) return null;
400+
if (source.type === 'ConditionalExpression')
401+
return intersectControllers(
402+
requestControllers(source.consequent, depth),
403+
requestControllers(source.alternate, depth),
404+
);
405+
if (source.type === 'LogicalExpression')
406+
return intersectControllers(
407+
requestControllers(source.left, depth),
408+
requestControllers(source.right, depth),
409+
);
389410
let controllers = signalsIn(source.arguments, null);
390411
const callee = unwrap(source.callee);
391412
if (callee?.type === 'MemberExpression') {

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,15 @@ describe('Strong asynchronous effect updates', () => {
257257
],
258258
['a while test', `while (await fetch('/a', { signal })) { setData(1); }`],
259259
['a for test', `for (; await fetch('/a', { signal }); ) { setData(1); }`],
260+
[
261+
'a ternary request',
262+
`const r = await (props.fast ? fetch('/a', { signal }) : fetch('/b', { signal })); setData(r);`,
263+
],
264+
['a sequence expression request', `const r = await (0, fetch('/a', { signal })); setData(r);`],
265+
[
266+
'a logical-or request',
267+
`const r = await (fetch('/a', { signal }) || fetch('/b', { signal })); setData(r);`,
268+
],
260269
])('accepts an abort proof that covers %s', (_label, body) => {
261270
accepts(app(aborted(body)));
262271
});

0 commit comments

Comments
 (0)