Skip to content

Commit ddd1d4b

Browse files
committed
Fix #85, allow reactivity in use: directive function.
1 parent a4a4135 commit ddd1d4b

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

docs/reactivity.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,19 @@ function Component() {
746746
const value = staticValue();
747747
}
748748

749+
function Component(props) {
750+
const count$ = observable(() => props.count);
751+
return <div />;
752+
}
753+
754+
const [signal, setSignal] = createSignal(0);
755+
const value$ = observable(signal);
756+
757+
let someHook;
758+
function Component(props) {
759+
return <div use:someHook={() => props.count} />;
760+
}
761+
749762
```
750763
<!-- AUTO-GENERATED-CONTENT:END -->
751764

src/rules/reactivity.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,17 @@ export default createRule({
806806
// if desired.
807807
// What this means here is we actually do consider an event handler a tracked scope
808808
// expecting a function, i.e. it's okay to use changing props/signals in the body of the
809-
// function, even though the changes don't affect when the handler will run.
809+
// function, even though the changes don't affect when the handler will run. This is what
810+
// "called-function" represents—not quite a tracked scope, but a place where it's okay to
811+
// read reactive values.
812+
pushTrackedScope(node.expression, "called-function");
813+
} else if (
814+
node.parent?.type === "JSXAttribute" &&
815+
node.parent.name.type === "JSXNamespacedName" &&
816+
node.parent.name.namespace.name === "use" &&
817+
isFunctionNode(node.expression)
818+
) {
819+
// With a `use:` hook, assume that a function passed is a called function.
810820
pushTrackedScope(node.expression, "called-function");
811821
} else if (
812822
node.parent?.type === "JSXAttribute" &&

test/rules/reactivity.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,13 @@ export const cases = run("reactivity", rule, {
298298
const count$ = observable(() => props.count);
299299
return <div />;
300300
}`,
301+
`const [signal, setSignal] = createSignal(0);
302+
const value$ = observable(signal);`,
303+
// use: functions
304+
`let someHook;
305+
function Component(props) {
306+
return <div use:someHook={() => props.count} />;
307+
}`,
301308
],
302309
invalid: [
303310
// Untracked signals

0 commit comments

Comments
 (0)