Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cool-tips-explain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sveltejs/mcp": patch
---

fix: check effect.pre in assign-in-effect
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe('add_autofixers_issues', () => {
<script>
const count = ${init}(0);
</script>

<button onclick={() => count = 43}>Increment</button>
`);

Expand All @@ -74,7 +74,7 @@ describe('add_autofixers_issues', () => {
const content = run_autofixers_on_code(`
<script>
const count = 0;

$effect(() => {
count = 43;
});
Expand All @@ -89,7 +89,7 @@ describe('add_autofixers_issues', () => {
const content = run_autofixers_on_code(`
<script>
let count = ${init}(0);

$effect(() => {
count++;
});
Expand All @@ -105,7 +105,7 @@ describe('add_autofixers_issues', () => {
const content = run_autofixers_on_code(`
<script>
let count = ${init}({ value: 0 });

$effect(() => {
count.value = 42;
});
Expand All @@ -116,6 +116,22 @@ describe('add_autofixers_issues', () => {
'The stateful variable "count" is assigned inside an $effect which is generally consider a malpractice. Consider using $derived if possible.',
);
});

it(`should add a suggestion for variables that are mutated within an effect.pre`, () => {
const content = run_autofixers_on_code(`
<script>
let count = ${init}({ value: 0 });

$effect.pre(() => {
count.value = 42;
});
</script>
`);

expect(content.suggestions).toContain(
'The stateful variable "count" is assigned inside an $effect which is generally consider a malpractice. Consider using $derived if possible.',
);
});
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,11 @@ function run_if_in_effect(
) {
const in_effect = path.findLast(
(node) =>
node.type === 'CallExpression' &&
node.callee.type === 'Identifier' &&
node.callee.name === '$effect',
node.type === 'CallExpression' && state.parsed.is_rune(node, ['$effect', '$effect.pre']),
);

if (
in_effect &&
in_effect.type === 'CallExpression' &&
(in_effect.callee.type === 'Identifier' || in_effect.callee.type === 'MemberExpression')
) {
if (state.parsed.is_rune(in_effect, ['$effect', '$effect.pre'])) {
to_run();
}
if (in_effect) {
to_run();
}
}

Expand Down
Loading