Skip to content

reactivity: report stale captures in handlers declared as statements (fixes #258) - #260

Merged
ryansolid merged 1 commit into
solidjs:mainfrom
anubhavlal07:fix/reactivity-declared-handler-capture
Nov 17, 2026
Merged

ryansolid merged 1 commit into
solidjs:mainfrom
anubhavlal07:fix/reactivity-declared-handler-capture

Conversation

@anubhavlal07

Copy link
Copy Markdown
Contributor

Fixes #258

Bug

Since 0.19.0, staleCapture no longer reports a component handler that is declared as a statement and then passed into the returned JSX:

const Component = () => {
  const current = n();
  const onClick = () => console.log(current);
  return <button onClick={onClick}>x</button>;
};

The same happens with function onClick() { ... }. The inline version, onClick={() => console.log(current)}, is still reported.

Root cause

#251 added child.type !== "BlockStatement" to isCapturedInReturnedFunction so that a helper declared in a block-bodied memo (const share = ...; return share(2)) is no longer treated as returned (#250). Before that change every closure declared as a statement counted as returned. After it, none do, even when the closure is referenced from the return value.

Fix

When the walk from the outermost closure reaches the boundary through its block body, and that closure is a FunctionDeclaration or the init of a VariableDeclarator, the rule now looks up its declared variables and checks each non-init reference. A reference escapes if it reaches a ReturnStatement before it hits a CallExpression/NewExpression or another function. So:

Tests

New invalid cases in test/rules/reactivity.test.ts: an arrow handler, a function-declaration handler, a handler in a function-declaration component, and a closure returned directly from a block-bodied memo. New valid case: return items.map(scale) in a memo. The existing #250 and #223 cases still pass. The four invalid cases fail without the change. PARSER=all vitest --run passes for the plugin package (3122 tests), and docs/reactivity.md has been regenerated.

Declared closures referenced from a returned expression escape again (fixes solidjs#258).
@ryansolid
ryansolid merged commit 9325082 into solidjs:main Nov 17, 2026
3 checks passed
@ryansolid ryansolid mentioned this pull request Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0.19.0 (reactivity): staleCapture no longer reports a component handler declared as a statement (regression from #251)

2 participants