Skip to content

Commit d6fc94a

Browse files
committed
Assume custom hook arguments are tracking scopes.
1 parent d94a40c commit d6fc94a

File tree

3 files changed

+24
-22
lines changed

3 files changed

+24
-22
lines changed

docs/reactivity.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,6 @@ const Component = () => {
9595
bar(); // not ok
9696
};
9797

98-
const Component = () => {
99-
const [signal] = createSignal();
100-
const memo = createMomo(() => signal());
101-
};
102-
103-
const [signal] = createSignal();
104-
const memo = createMomo(() => signal());
105-
10698
const Component = () => {
10799
createSignal();
108100
};
@@ -285,6 +277,14 @@ on(b, async () => {
285277
setA(a() + 1);
286278
});
287279

280+
const Component = (props) => {
281+
const localRef = () => props.ref;
282+
// custom hooks
283+
const composedRef1 = useComposedRefs(localRef);
284+
const composedRef2 = useComposedRefs(() => props.ref);
285+
const composedRef3 = createComposedRefs(localRef);
286+
};
287+
288288
```
289289
<!-- AUTO-GENERATED-CONTENT:END -->
290290

src/rules/reactivity.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,15 @@ const rule: TSESLint.RuleModule<MessageIds, []> = {
755755
pushTrackedScope(node.arguments[1], "function");
756756
}
757757
}
758+
} else if (/^(?:use|create)[A-Z]/.test(callee.name)) {
759+
// Custom hooks parameters may or may not be tracking scopes, no way to know.
760+
// Assume all identifier/function arguments are tracked scopes, and use "called-function"
761+
// to allow async handlers (permissive)
762+
node.arguments
763+
.filter((arg) => isFunctionNode(arg) || arg.type === "Identifier")
764+
.forEach((arg) => {
765+
pushTrackedScope(arg, "called-function");
766+
});
758767
}
759768
} else if (node.type === "VariableDeclarator") {
760769
// Solid 1.3 createReactive (renamed createReaction?) returns a track

test/rules/reactivity.test.ts

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,13 @@ export const cases = run("reactivity", rule, {
110110
`const [a, setA] = createSignal(1);
111111
const [b] = createSignal(2);
112112
on(b, async () => { await delay(1000); setA(a() + 1) });`,
113+
`const Component = (props) => {
114+
const localRef = () => props.ref;
115+
// custom hooks
116+
const composedRef1 = useComposedRefs(localRef);
117+
const composedRef2 = useComposedRefs(() => props.ref);
118+
const composedRef3 = createComposedRefs(localRef);
119+
}`,
113120
],
114121
invalid: [
115122
// Untracked signals
@@ -245,20 +252,6 @@ export const cases = run("reactivity", rule, {
245252
},
246253
],
247254
},
248-
{
249-
code: `
250-
const Component = () => {
251-
const [signal] = createSignal();
252-
const memo = createMomo(() => signal());
253-
}`,
254-
errors: [{ messageId: "badUnnamedDerivedSignal" }],
255-
},
256-
{
257-
code: `
258-
const [signal] = createSignal();
259-
const memo = createMomo(() => signal());`,
260-
errors: [{ messageId: "badUnnamedDerivedSignal", line: 3, column: 34, endColumn: 36 }],
261-
},
262255
// Unused reactives
263256
{
264257
code: `

0 commit comments

Comments
 (0)