|
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'; |
2 | 8 | import type { Autofixer, AutofixerState } from './index.js';
|
3 | 9 | import { left_most_id } from '../ast/utils.js';
|
4 | 10 | import type { AST } from 'svelte-eslint-parser';
|
@@ -27,9 +33,9 @@ function run_if_in_effect(
|
27 | 33 | }
|
28 | 34 | }
|
29 | 35 |
|
30 |
| -function visitor( |
| 36 | +function assign_or_update_visitor( |
31 | 37 | node: UpdateExpression | AssignmentExpression,
|
32 |
| - { state, path }: Context<Node | AST.SvelteNode, AutofixerState>, |
| 38 | + { state, path, next }: Context<Node | AST.SvelteNode, AutofixerState>, |
33 | 39 | ) {
|
34 | 40 | run_if_in_effect(path, state, () => {
|
35 | 41 | function check_if_stateful_id(id: Identifier) {
|
@@ -58,9 +64,25 @@ function visitor(
|
58 | 64 | }
|
59 | 65 | }
|
60 | 66 | });
|
| 67 | + next(); |
| 68 | +} |
| 69 | + |
| 70 | +function call_expression_visitor( |
| 71 | + node: CallExpression, |
| 72 | + { state, path, next }: Context<Node | AST.SvelteNode, AutofixerState>, |
| 73 | +) { |
| 74 | + run_if_in_effect(path, state, () => { |
| 75 | + const function_name = |
| 76 | + node.callee.type === 'Identifier' ? `the function \`${node.callee.name}\`` : 'a function'; |
| 77 | + state.output.suggestions.push( |
| 78 | + `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.`, |
| 79 | + ); |
| 80 | + }); |
| 81 | + next(); |
61 | 82 | }
|
62 | 83 |
|
63 | 84 | export const assign_in_effect: Autofixer = {
|
64 |
| - UpdateExpression: visitor, |
65 |
| - AssignmentExpression: visitor, |
| 85 | + UpdateExpression: assign_or_update_visitor, |
| 86 | + AssignmentExpression: assign_or_update_visitor, |
| 87 | + CallExpression: call_expression_visitor, |
66 | 88 | };
|
0 commit comments