diff --git a/packages/eslint-plugin-solid/docs/reactivity.md b/packages/eslint-plugin-solid/docs/reactivity.md
index cfb8573..a11ac95 100644
--- a/packages/eslint-plugin-solid/docs/reactivity.md
+++ b/packages/eslint-plugin-solid/docs/reactivity.md
@@ -672,6 +672,36 @@ const useStale = () => {
return () => value + 1;
};
+const [n] = createSignal(1);
+const Component = () => {
+ const current = n();
+ const onClick = () => console.log(current);
+ return ;
+};
+
+const [n] = createSignal(1);
+const Component = () => {
+ const current = n();
+ function onClick() {
+ console.log(current);
+ }
+ return ;
+};
+
+const [n] = createSignal(1);
+function Component() {
+ const current = n();
+ const onClick = () => console.log(current);
+ return ;
+}
+
+const [n] = createSignal(1);
+const total = createMemo(() => {
+ const value = n();
+ const read = () => value;
+ return read;
+});
+
const [count, setCount] = createSignal(0);
function useStale() {
const value = count();
@@ -1538,6 +1568,15 @@ const fromLength = createMemo(() =>
Array.from({ length: 3 }, (_, i) => i * n())
);
+import { createMemo, createSignal } from "solid-js";
+const [n] = createSignal(1);
+const items = [1, 2, 3];
+const scaled = createMemo(() => {
+ const total = n();
+ const scale = (v) => v * total;
+ return items.map(scale);
+});
+
```
diff --git a/packages/eslint-plugin-solid/src/rules/reactivity.ts b/packages/eslint-plugin-solid/src/rules/reactivity.ts
index 07cb633..0cde526 100644
--- a/packages/eslint-plugin-solid/src/rules/reactivity.ts
+++ b/packages/eslint-plugin-solid/src/rules/reactivity.ts
@@ -579,6 +579,13 @@ export default createRule({
// same goes for a called function itself (IIFEs).
if (parent.type === "CallExpression" || parent.type === "NewExpression") return false;
if (isFunctionNode(parent)) {
+ if (
+ parent === boundary &&
+ child.type === "BlockStatement" &&
+ isDeclaredClosureReturned(outermost, boundary)
+ ) {
+ return true;
+ }
return (
parent === boundary &&
parent.type === "ArrowFunctionExpression" &&
@@ -594,6 +601,41 @@ export default createRule({
return false;
};
+ const isDeclaredClosureReturned = (
+ closure: FunctionNode,
+ boundary: ProgramOrFunctionNode
+ ): boolean => {
+ const declaration =
+ closure.type === "FunctionDeclaration"
+ ? closure
+ : closure.parent?.type === "VariableDeclarator" && closure.parent.init === closure
+ ? closure.parent
+ : null;
+ if (!declaration) return false;
+ const variables = sourceCode.scopeManager?.getDeclaredVariables(declaration) ?? [];
+ return variables.some((variable) =>
+ variable.references.some(
+ (reference) => !reference.init && isReferenceReturned(reference.identifier, boundary)
+ )
+ );
+ };
+
+ const isReferenceReturned = (node: T.Node, boundary: ProgramOrFunctionNode): boolean => {
+ let cursor: T.Node | null = node.parent ?? null;
+ while (cursor && cursor !== boundary) {
+ if (cursor.type === "ReturnStatement") return true;
+ if (
+ cursor.type === "CallExpression" ||
+ cursor.type === "NewExpression" ||
+ isFunctionNode(cursor)
+ ) {
+ return false;
+ }
+ cursor = cursor.parent ?? null;
+ }
+ return false;
+ };
+
/** Inspects a specific reference of a reactive variable for correct handling. */
const handleTrackedScopes = (
identifier: T.Identifier,
diff --git a/packages/eslint-plugin-solid/test/rules/reactivity.test.ts b/packages/eslint-plugin-solid/test/rules/reactivity.test.ts
index 44b46a5..cc691c0 100644
--- a/packages/eslint-plugin-solid/test/rules/reactivity.test.ts
+++ b/packages/eslint-plugin-solid/test/rules/reactivity.test.ts
@@ -762,6 +762,14 @@ export const cases = run("reactivity", rule, {
`import { createMemo, createSignal } from "solid-js";
const [n] = createSignal(1);
const fromLength = createMemo(() => Array.from({ length: 3 }, (_, i) => i * n()));`,
+ `import { createMemo, createSignal } from "solid-js";
+ const [n] = createSignal(1);
+ const items = [1, 2, 3];
+ const scaled = createMemo(() => {
+ const total = n();
+ const scale = (v) => v * total;
+ return items.map(scale);
+ });`,
],
invalid: [
// Untracked signals
@@ -1481,6 +1489,48 @@ export const cases = run("reactivity", rule, {
};`,
errors: [{ messageId: "staleCapture", line: 4 }],
},
+ {
+ code: `
+ const [n] = createSignal(1);
+ const Component = () => {
+ const current = n();
+ const onClick = () => console.log(current);
+ return ;
+ };`,
+ errors: [{ messageId: "staleCapture", line: 4 }],
+ },
+ {
+ code: `
+ const [n] = createSignal(1);
+ const Component = () => {
+ const current = n();
+ function onClick() {
+ console.log(current);
+ }
+ return ;
+ };`,
+ errors: [{ messageId: "staleCapture", line: 4 }],
+ },
+ {
+ code: `
+ const [n] = createSignal(1);
+ function Component() {
+ const current = n();
+ const onClick = () => console.log(current);
+ return ;
+ }`,
+ errors: [{ messageId: "staleCapture", line: 4 }],
+ },
+ {
+ code: `
+ const [n] = createSignal(1);
+ const total = createMemo(() => {
+ const value = n();
+ const read = () => value;
+ return read;
+ });`,
+ errors: [{ messageId: "staleCapture", line: 4 }],
+ },
// Implicit arrow return escapes too
{
code: `