Skip to content

Commit

Permalink
Mark createState getters as tracked scopes. Fixes #18.
Browse files Browse the repository at this point in the history
  • Loading branch information
joshwilsonvu committed Apr 14, 2022
1 parent eed3387 commit c4c992d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/rules/reactivity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,16 @@ const rule: TSESLint.RuleModule<MessageIds, []> = {
// Since dependencies are known, function can be async
pushTrackedScope(node.arguments[1], "called-function");
}
} else if (matchImport("createStore", callee.name) && arg0.type === "ObjectExpression") {
for (const property of arg0.properties) {
if (
property.type === "Property" &&
property.kind === "get" &&
isFunctionNode(property.value)
) {
pushTrackedScope(property.value, "function");
}
}
} else if (matchImport("runWithOwner", callee.name)) {
// runWithOwner(owner, fn) only creates a tracked scope if `owner =
// getOwner()` runs in a tracked scope. If owner is a variable,
Expand Down
17 changes: 17 additions & 0 deletions test/rules/reactivity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,14 @@ export const cases = run("reactivity", rule, {
const [count, setCount] = useSignal(props.initialCount);
return <div>{count()}</div>;
}`,
// Store getters
`const [state, setState] = createStore({
firstName: 'Will',
lastName: 'Smith',
get fullName() {
return state.firstName + " " + state.lastName;
}
})`,
],
invalid: [
// Untracked signals
Expand Down Expand Up @@ -372,6 +380,15 @@ export const cases = run("reactivity", rule, {
errors: [{ messageId: "badUnnamedDerivedSignal", line: 5 }],
},
// Async tracking scopes
{
code: `
const [count, setCount] = createSignal(0);
createEffect(async () => {
await Promise.resolve();
console.log(count());
});`,
errors: [{ messageId: "noAsyncTrackedScope", line: 3 }],
},
{
code: `
const [photos, setPhotos] = createSignal([]);
Expand Down

0 comments on commit c4c992d

Please sign in to comment.