Skip to content

Commit ef2d569

Browse files
Merge pull request #26 from sveltejs/function-call-in-effect
2 parents 7ba57b4 + 8483bd6 commit ef2d569

File tree

2 files changed

+57
-5
lines changed

2 files changed

+57
-5
lines changed

packages/mcp-server/src/mcp/autofixers/add-autofixers-issues.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,36 @@ describe('add_autofixers_issues', () => {
133133
);
134134
});
135135
});
136+
137+
it('should add a suggestion when calling a function inside an effect', () => {
138+
const content = run_autofixers_on_code(`
139+
<script>
140+
import { fetch_data } from './data.js';
141+
$effect(() => {
142+
fetch_data();
143+
});
144+
</script>`);
145+
146+
expect(content.suggestions.length).toBeGreaterThanOrEqual(1);
147+
expect(content.suggestions).toContain(
148+
`You are calling the function \`fetch_data\` inside an $effect. Please check if the function is reassigning a stateful variable because that's considered malpractice and check if it could use \`$derived\` instead. Ignore this suggestion if you are sure this function is not assigning any stateful variable or if you can't check if it does.`,
149+
);
150+
});
151+
152+
it('should add a suggestion when calling a function inside an effect (with non identifier callee)', () => {
153+
const content = run_autofixers_on_code(`
154+
<script>
155+
import { fetch_data } from './data.js';
156+
$effect(() => {
157+
fetch_data.fetch();
158+
});
159+
</script>`);
160+
161+
expect(content.suggestions.length).toBeGreaterThanOrEqual(1);
162+
expect(content.suggestions).toContain(
163+
`You are calling a function inside an $effect. Please check if the function is reassigning a stateful variable because that's considered malpractice and check if it could use \`$derived\` instead. Ignore this suggestion if you are sure this function is not assigning any stateful variable or if you can't check if it does.`,
164+
);
165+
});
136166
});
137167

138168
with_possible_inits('($init)', ({ init }) => {

packages/mcp-server/src/mcp/autofixers/visitors/assign-in-effect.ts

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import type { AssignmentExpression, Identifier, Node, UpdateExpression } from 'estree';
1+
import type {
2+
AssignmentExpression,
3+
CallExpression,
4+
Identifier,
5+
Node,
6+
UpdateExpression,
7+
} from 'estree';
28
import type { Autofixer, AutofixerState } from './index.js';
39
import { left_most_id } from '../ast/utils.js';
410
import type { AST } from 'svelte-eslint-parser';
@@ -19,9 +25,9 @@ function run_if_in_effect(
1925
}
2026
}
2127

22-
function visitor(
28+
function assign_or_update_visitor(
2329
node: UpdateExpression | AssignmentExpression,
24-
{ state, path }: Context<Node | AST.SvelteNode, AutofixerState>,
30+
{ state, path, next }: Context<Node | AST.SvelteNode, AutofixerState>,
2531
) {
2632
run_if_in_effect(path, state, () => {
2733
function check_if_stateful_id(id: Identifier) {
@@ -50,9 +56,25 @@ function visitor(
5056
}
5157
}
5258
});
59+
next();
60+
}
61+
62+
function call_expression_visitor(
63+
node: CallExpression,
64+
{ state, path, next }: Context<Node | AST.SvelteNode, AutofixerState>,
65+
) {
66+
run_if_in_effect(path, state, () => {
67+
const function_name =
68+
node.callee.type === 'Identifier' ? `the function \`${node.callee.name}\`` : 'a function';
69+
state.output.suggestions.push(
70+
`You are calling ${function_name} inside an $effect. Please check if the function is reassigning a stateful variable because that's considered malpractice and check if it could use \`$derived\` instead. Ignore this suggestion if you are sure this function is not assigning any stateful variable or if you can't check if it does.`,
71+
);
72+
});
73+
next();
5374
}
5475

5576
export const assign_in_effect: Autofixer = {
56-
UpdateExpression: visitor,
57-
AssignmentExpression: visitor,
77+
UpdateExpression: assign_or_update_visitor,
78+
AssignmentExpression: assign_or_update_visitor,
79+
CallExpression: call_expression_visitor,
5880
};

0 commit comments

Comments
 (0)