Skip to content

Commit 5f81352

Browse files
committed
fix(compiler): fix strong-effect shape merging, helper target detection, and global listener recognition
- mergeShapes: compare returned-array and returned-object shapes structurally instead of by identity, so multi-path custom hooks that return the same bindings in different wrapper objects preserve setter provenance - platformTarget: follow frameArgument for helper parameters and handle destructured properties (path.length > 0), so on(window, type, fn) helpers and const { body } = document bindings are recognized as platform targets - acquire/scan: recognize bare addEventListener and removeEventListener as global window methods (matching setInterval/clearInterval pattern), so unqualified global listener calls are tracked for leak detection
1 parent 2595220 commit 5f81352

2 files changed

Lines changed: 50 additions & 2 deletions

File tree

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,13 @@ export function createStrongEffectPolicy({ ast, analysis, callNames, report }) {
507507
) {
508508
const key = keyOf(node.arguments?.[0]);
509509
if (key !== null) result.cleared.add(key);
510+
} else if (name === 'removeEventListener') {
511+
result.removed.push({
512+
target: 'g:window',
513+
type: eventType(node.arguments?.[0]),
514+
handler: keyOf(node.arguments?.[1]),
515+
capture: captureOf(node.arguments?.[2]),
516+
});
510517
} else if (callee?.type === 'MemberExpression') {
511518
const method = memberName(callee);
512519
const object = keyOf(callee.object);
@@ -638,8 +645,18 @@ export function createStrongEffectPolicy({ ast, analysis, callNames, report }) {
638645
if (node.type === 'Identifier') {
639646
const binding = bindingOf(node);
640647
if (binding === null) return EVENT_TARGET_GLOBALS.has(node.name);
648+
const argument = frameArgument(binding);
649+
if (argument !== null) return platformTarget(argument, depth + 1);
641650
const init = stableInit(binding);
642-
return init !== null && platformTarget(init, depth + 1);
651+
if (init !== null) return platformTarget(init, depth + 1);
652+
if (!binding.reassigned) {
653+
const info = declarator(binding);
654+
if (info !== null && info.path.length !== 0) {
655+
const base = unwrap(info.decl.init);
656+
return base !== null && platformTarget(base, depth + 1);
657+
}
658+
}
659+
return false;
643660
}
644661
if (node.type === 'MemberExpression') {
645662
const object = unwrap(node.object);
@@ -675,6 +692,18 @@ export function createStrongEffectPolicy({ ast, analysis, callNames, report }) {
675692
}
676693
return;
677694
}
695+
if (name === 'addEventListener') {
696+
record.acquisitions.push({
697+
kind: 'listener',
698+
node,
699+
target: 'g:window',
700+
type: eventType(node.arguments?.[0]),
701+
handler: keyOf(node.arguments?.[1]),
702+
capture: captureOf(node.arguments?.[2]),
703+
signal: listenerSignal(node.arguments?.[2]),
704+
});
705+
return;
706+
}
678707
if (callee?.type !== 'MemberExpression') return;
679708
const method = memberName(callee);
680709
if (method === 'addEventListener' && platformTarget(callee.object)) {

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2727,9 +2727,28 @@ export function analyzeStrongMode(ast, source, filename, options = {}) {
27272727
return shapeMember(node, scope);
27282728
}
27292729

2730+
function shapesEqual(a, b) {
2731+
if (a === b) return true;
2732+
if (a == null || b == null || a.kind !== b.kind) return false;
2733+
if (a.kind === 'returned-array') {
2734+
return (
2735+
a.elements.length === b.elements.length &&
2736+
a.elements.every((element, i) => element === b.elements[i])
2737+
);
2738+
}
2739+
if (a.kind === 'returned-object') {
2740+
if (a.properties.size !== b.properties.size) return false;
2741+
for (const [key, value] of a.properties) {
2742+
if (b.properties.get(key) !== value) return false;
2743+
}
2744+
return true;
2745+
}
2746+
return false;
2747+
}
2748+
27302749
function mergeShapes(shapes) {
27312750
const [first] = shapes;
2732-
return first != null && shapes.every((shape) => shape === first) ? first : null;
2751+
return first != null && shapes.every((shape) => shapesEqual(shape, first)) ? first : null;
27332752
}
27342753

27352754
function bindReturnedShape(pattern, shape, bind) {

0 commit comments

Comments
 (0)