Skip to content

Commit

Permalink
Fix weirdness with parsing undefined as an identifier.
Browse files Browse the repository at this point in the history
  • Loading branch information
joshwilsonvu committed Dec 30, 2023
1 parent 5bdce80 commit 41e9008
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 1 deletion.
5 changes: 5 additions & 0 deletions docs/no-react-deps.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ createEffect((prev) => {
return (prev || 0) + 1;
});

createEffect((prev) => {
console.log(signal());
return prev ? prev + 1 : 1;
}, undefined);

const value = createMemo(() => computeExpensiveValue(a(), b()));

const sum = createMemo((prev) => input() + prev, 0);
Expand Down
5 changes: 5 additions & 0 deletions docs/reactivity.md
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,11 @@ createEffect(() => {
runWithOwner(owner, () => console.log(signal()));
});

const [signal] = createSignal();
createEffect(() => {
runWithOwner(undefined, () => console.log(signal()));
});

const [signal] = createSignal();
createEffect(() => {
[1, 2].forEach(() => console.log(signal()));
Expand Down
4 changes: 3 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ export function trace(node: T.Node, initialScope: TSESLint.Scope.Scope): T.Node
if (!variable) return node;

const def = variable.defs[0];
switch (def.type) {

// def is `undefined` for Identifier `undefined`
switch (def?.type) {
case "FunctionName":
case "ClassName":
case "ImportBinding":
Expand Down
4 changes: 4 additions & 0 deletions test/rules/no-react-deps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export const cases = run("no-react-deps", rule, {
console.log(signal());
return (prev || 0) + 1;
});`,
`createEffect((prev) => {
console.log(signal());
return prev ? prev + 1 : 1;
}, undefined);`,
`const value = createMemo(() => computeExpensiveValue(a(), b()));`,
`const sum = createMemo((prev) => input() + prev, 0);`,
`const args = [() => { console.log(signal()); }, [signal()]];
Expand Down
4 changes: 4 additions & 0 deletions test/rules/reactivity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ export const cases = run("reactivity", rule, {
const owner = getOwner();
runWithOwner(owner, () => console.log(signal()));
});`,
`const [signal] = createSignal();
createEffect(() => {
runWithOwner(undefined, () => console.log(signal()));
});`,
// Sync callbacks
`const [signal] = createSignal();
createEffect(() => {
Expand Down

0 comments on commit 41e9008

Please sign in to comment.