Skip to content

Commit e6fbed9

Browse files
committed
fix(compiler): detect resource leaks through conditional platform targets and wrapped host refs
platformTarget now walks ConditionalExpression and LogicalExpression via someResult, so `const target = enabled ? window : document` is recognized as a platform EventTarget and its addEventListener is tracked for cleanup. classify now handles ConditionalExpression/LogicalExpression wrappers in ref attributes (`ref={enabled ? element : null}`) and walks through transparent TS wrappers between an ArrayExpression and its JSXExpressionContainer (`ref={[element] as const}`), so the ref is correctly marked as a host ref and listeners on its .current are tracked.
1 parent cff777a commit e6fbed9

2 files changed

Lines changed: 78 additions & 3 deletions

File tree

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

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,9 @@ export function createStrongEffectPolicy({ ast, analysis, callNames, report }) {
895895
const callee = unwrap(node.callee);
896896
return callee?.type === 'MemberExpression' && platformTarget(callee.object, depth + 1);
897897
}
898+
if (node.type === 'ConditionalExpression' || node.type === 'LogicalExpression') {
899+
return someResult(node, platformTarget, depth);
900+
}
898901
return platformConstructor(node) !== null;
899902
}
900903

@@ -1201,6 +1204,28 @@ export function createStrongEffectPolicy({ ast, analysis, callNames, report }) {
12011204
case 'BreakStatement':
12021205
case 'ContinueStatement':
12031206
return;
1207+
case 'ConditionalExpression':
1208+
case 'LogicalExpression': {
1209+
let at = index - 1;
1210+
while (
1211+
at >= 0 &&
1212+
(TRANSPARENT.has(parents[at].type) ||
1213+
parents[at].type === 'ConditionalExpression' ||
1214+
parents[at].type === 'LogicalExpression')
1215+
)
1216+
at--;
1217+
if (parents[at]?.type === 'JSXExpressionContainer') {
1218+
const attribute = parents[at - 1];
1219+
const element = parents[at - 2];
1220+
if (attribute?.type === 'JSXAttribute' && attribute.name?.name === 'ref') {
1221+
const name = element?.type === 'JSXOpeningElement' ? element.name : null;
1222+
if (name?.type === 'JSXIdentifier' && /^[a-z]/.test(name.name)) {
1223+
references.host.add(root);
1224+
}
1225+
}
1226+
}
1227+
break;
1228+
}
12041229
case 'ArrayExpression': {
12051230
// A dependency list is not a use of the ref's identity, even when it
12061231
// is parenthesized or cast.
@@ -1216,9 +1241,14 @@ export function createStrongEffectPolicy({ ast, analysis, callNames, report }) {
12161241
let attribute = parents[index - 1];
12171242
let element = parents[index - 2];
12181243
if (parent.type === 'ArrayExpression') {
1219-
attribute = parents[index - 2];
1220-
element = parents[index - 3];
1221-
if (parents[index - 1]?.type !== 'JSXExpressionContainer') attribute = null;
1244+
let at = index - 1;
1245+
while (at >= 0 && TRANSPARENT.has(parents[at].type)) at--;
1246+
if (parents[at]?.type === 'JSXExpressionContainer') {
1247+
attribute = parents[at - 1];
1248+
element = parents[at - 2];
1249+
} else {
1250+
attribute = null;
1251+
}
12221252
}
12231253
if (attribute?.type === 'JSXAttribute' && attribute.name?.name === 'ref') {
12241254
const name = element?.type === 'JSXOpeningElement' ? element.name : null;

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,6 +1476,21 @@ export function App(props) @{
14761476
`const element = useRef(null); useEffect(() => { props.log(element.current); });`,
14771477
'<div ref={[element, props.forwarded]} />',
14781478
],
1479+
[
1480+
'a conditional element ref',
1481+
`const element = useRef(null); useEffect(() => { props.log(element.current); });`,
1482+
'<div ref={props.enabled ? element : null} />',
1483+
],
1484+
[
1485+
'a logical element ref',
1486+
`const element = useRef(null); useEffect(() => { props.log(element.current); });`,
1487+
'<div ref={props.enabled && element} />',
1488+
],
1489+
[
1490+
'a TS-cast array element ref',
1491+
`const element = useRef(null); useEffect(() => { props.log(element.current); });`,
1492+
'<div ref={[element] as const} />',
1493+
],
14791494
[
14801495
'a ref passed to a component',
14811496
`const element = useRef(null); useEffect(() => { props.log(element.current); });`,
@@ -1750,6 +1765,14 @@ export function App(props) @{
17501765
'a conditionally created interval',
17511766
`useEffect(() => { const id = props.enabled ? setInterval(() => setWidth(1), 1000) : null; });`,
17521767
],
1768+
[
1769+
'a conditional platform target listener',
1770+
`useEffect(() => { const target = props.narrow ? document : window; target.addEventListener('resize', () => setWidth(1)); });`,
1771+
],
1772+
[
1773+
'a logical platform target listener',
1774+
`useEffect(() => { const target = props.target || document; target.addEventListener('resize', () => setWidth(1)); });`,
1775+
],
17531776
[
17541777
'a stored helper remover for another handler',
17551778
`function subscribe(handler, other) { window.addEventListener('resize', handler); return () => window.removeEventListener('resize', other); } useEffect(() => { const first = () => setWidth(1); const second = () => setWidth(2); const stop = subscribe(first, second); return stop; });`,
@@ -1766,6 +1789,24 @@ export function App(props) @{
17661789
rejects(app(setup), LEAK);
17671790
});
17681791

1792+
it.each([
1793+
['a conditional ref', `<div ref={props.enabled ? element : null} />`],
1794+
['a logical ref', `<div ref={props.enabled && element} />`],
1795+
['a TS-cast array ref', `<div ref={[element] as const} />`],
1796+
])('rejects an element listener on %s without release', (_label, output) => {
1797+
rejects(
1798+
`
1799+
import { useState, useEffect, useRef } from 'octane';
1800+
export function App(props) @{
1801+
const [width, setWidth] = useState(0);
1802+
const element = useRef(null);
1803+
useEffect(() => { element.current.addEventListener('scroll', () => setWidth(1)); });
1804+
${output}
1805+
}`,
1806+
LEAK,
1807+
);
1808+
});
1809+
17691810
it.each([
17701811
[
17711812
'a removed listener',
@@ -1949,6 +1990,10 @@ export function App(props) @{
19491990
'a reset global handler property',
19501991
`useEffect(() => { onresize = () => setWidth(1); return () => { window.onresize = null; }; });`,
19511992
],
1993+
[
1994+
'a removed conditional platform target listener',
1995+
`useEffect(() => { const target = props.narrow ? document : window; const onResize = () => setWidth(1); target.addEventListener('resize', onResize); return () => target.removeEventListener('resize', onResize); });`,
1996+
],
19521997
])('accepts %s', (_label, setup) => {
19531998
accepts(app(setup));
19541999
});

0 commit comments

Comments
 (0)