Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions packages/eslint-plugin-solid/docs/reactivity.md
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,36 @@ const useStale = () => {
return () => value + 1;
};

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

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

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

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();
Expand Down Expand Up @@ -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);
});

```
<!-- end-doc-gen -->

Expand Down
42 changes: 42 additions & 0 deletions packages/eslint-plugin-solid/src/rules/reactivity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,13 @@ export default createRule<Options, MessageIds>({
// 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" &&
Expand All @@ -594,6 +601,41 @@ export default createRule<Options, MessageIds>({
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,
Expand Down
50 changes: 50 additions & 0 deletions packages/eslint-plugin-solid/test/rules/reactivity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 <button onClick={onClick}>x</button>;
};`,
errors: [{ messageId: "staleCapture", line: 4 }],
},
{
code: `
const [n] = createSignal(1);
const Component = () => {
const current = n();
function onClick() {
console.log(current);
}
return <button onClick={onClick}>x</button>;
};`,
errors: [{ messageId: "staleCapture", line: 4 }],
},
{
code: `
const [n] = createSignal(1);
function Component() {
const current = n();
const onClick = () => console.log(current);
return <button onClick={onClick}>x</button>;
}`,
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: `
Expand Down
Loading