Skip to content

Commit 227d02f

Browse files
committed
Ignore derived signals assigned to properties
1 parent 6a918af commit 227d02f

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

src/rules/reactivity.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,8 @@ const rule: TSESLint.RuleModule<MessageIds, []> = {
368368
} else {
369369
pushUnnamedDerivedSignal();
370370
}
371+
} else if (currentScopeNode.parent?.type === "Property") {
372+
// todo make this a unique props or something--for now, just ignore (unsafe)
371373
} else {
372374
pushUnnamedDerivedSignal();
373375
}
@@ -876,12 +878,14 @@ const rule: TSESLint.RuleModule<MessageIds, []> = {
876878
} else if (/^(?:use|create)[A-Z]/.test(callee.name)) {
877879
// Custom hooks parameters may or may not be tracking scopes, no way to know.
878880
// Assume all identifier/function arguments are tracked scopes, and use "called-function"
879-
// to allow async handlers (permissive)
880-
node.arguments
881-
.filter((arg) => isFunctionNode(arg) || arg.type === "Identifier")
882-
.forEach((arg) => {
881+
// to allow async handlers (permissive). Assume non-resolvable args are reactive expressions.
882+
for (const arg of node.arguments) {
883+
if (isFunctionNode(arg)) {
883884
pushTrackedScope(arg, "called-function");
884-
});
885+
} else if (arg.type === "Identifier" || arg.type === "ObjectExpression") {
886+
pushTrackedScope(arg, "expression");
887+
}
888+
}
885889
}
886890
} else if (node.type === "CallExpression" && node.callee.type === "MemberExpression") {
887891
const { property } = node.callee;

test/rules/reactivity.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,16 @@ export const cases = run("reactivity", rule, {
143143
`const [a, setA] = createSignal(1);
144144
const [b] = createSignal(2);
145145
on(b, async () => { await delay(1000); setA(a() + 1) });`,
146+
// Custom hooks
146147
`const Component = (props) => {
147148
const localRef = () => props.ref;
148-
// custom hooks
149149
const composedRef1 = useComposedRefs(localRef);
150150
const composedRef2 = useComposedRefs(() => props.ref);
151151
const composedRef3 = createComposedRefs(localRef);
152152
}`,
153+
`function createFoo(v) {}
154+
const [bar, setBar] = createSignal();
155+
createFoo({ onBar: () => bar() });`,
153156
// Event listeners
154157
`const [signal, setSignal] = createSignal(1);
155158
const element = document.getElementById("id");

0 commit comments

Comments
 (0)