@@ -12,99 +12,106 @@ function run_autofixers_on_code(code: string, desired_svelte_version = 5) {
12
12
13
13
describe ( 'add_autofixers_issues' , ( ) => {
14
14
describe ( 'assign_in_effect' , ( ) => {
15
- it ( `should add suggestions when assigning to a stateful variable inside an effect` , ( ) => {
16
- const content = run_autofixers_on_code ( `
17
- <script>
18
- const count = $state(0);
19
- $effect(() => {
20
- count = 43;
21
- });
22
- </script>` ) ;
15
+ describe . each ( [
16
+ { init : '$state' } ,
17
+ { init : '$state.raw' } ,
18
+ { init : '$derived' } ,
19
+ { init : '$derived.by' } ,
20
+ ] ) ( '($init)' , ( { init } ) => {
21
+ it ( `should add suggestions when assigning to a stateful variable inside an effect` , ( ) => {
22
+ const content = run_autofixers_on_code ( `
23
+ <script>
24
+ const count = ${ init } (0);
25
+ $effect(() => {
26
+ count = 43;
27
+ });
28
+ </script>` ) ;
23
29
24
- expect ( content . suggestions . length ) . toBeGreaterThanOrEqual ( 1 ) ;
25
- expect ( content . suggestions ) . toContain (
26
- 'The stateful variable "count" is assigned inside an $effect which is generally consider a malpractice. Consider using $derived if possible.' ,
27
- ) ;
28
- } ) ;
30
+ expect ( content . suggestions . length ) . toBeGreaterThanOrEqual ( 1 ) ;
31
+ expect ( content . suggestions ) . toContain (
32
+ 'The stateful variable "count" is assigned inside an $effect which is generally consider a malpractice. Consider using $derived if possible.' ,
33
+ ) ;
34
+ } ) ;
29
35
30
- it ( `should add a suggestion for each variable assigned within an effect` , ( ) => {
31
- const content = run_autofixers_on_code ( `
32
- <script>
33
- const count = $state(0);
34
- const count2 = $state(0);
35
- $effect(() => {
36
- count = 43;
37
- count2 = 44;
38
- });
39
- </script>` ) ;
36
+ it ( `should add a suggestion for each variable assigned within an effect` , ( ) => {
37
+ const content = run_autofixers_on_code ( `
38
+ <script>
39
+ const count = $state(0);
40
+ const count2 = $state(0);
41
+ $effect(() => {
42
+ count = 43;
43
+ count2 = 44;
44
+ });
45
+ </script>` ) ;
40
46
41
- expect ( content . suggestions . length ) . toBeGreaterThanOrEqual ( 2 ) ;
42
- expect ( content . suggestions ) . toContain (
43
- 'The stateful variable "count" is assigned inside an $effect which is generally consider a malpractice. Consider using $derived if possible.' ,
44
- ) ;
45
- expect ( content . suggestions ) . toContain (
46
- 'The stateful variable "count2" is assigned inside an $effect which is generally consider a malpractice. Consider using $derived if possible.' ,
47
- ) ;
48
- } ) ;
49
- it ( `should not add a suggestion for variables that are not assigned within an effect` , ( ) => {
50
- const content = run_autofixers_on_code ( `
51
- <script>
52
- const count = $state (0);
53
- </script>
54
-
55
- <button onclick={() => count = 43}>Increment</button>
56
- ` ) ;
47
+ expect ( content . suggestions . length ) . toBeGreaterThanOrEqual ( 2 ) ;
48
+ expect ( content . suggestions ) . toContain (
49
+ 'The stateful variable "count" is assigned inside an $effect which is generally consider a malpractice. Consider using $derived if possible.' ,
50
+ ) ;
51
+ expect ( content . suggestions ) . toContain (
52
+ 'The stateful variable "count2" is assigned inside an $effect which is generally consider a malpractice. Consider using $derived if possible.' ,
53
+ ) ;
54
+ } ) ;
55
+ it ( `should not add a suggestion for variables that are not assigned within an effect` , ( ) => {
56
+ const content = run_autofixers_on_code ( `
57
+ <script>
58
+ const count = ${ init } (0);
59
+ </script>
60
+
61
+ <button onclick={() => count = 43}>Increment</button>
62
+ ` ) ;
57
63
58
- expect ( content . suggestions ) . not . toContain (
59
- 'The stateful variable "count" is assigned inside an $effect which is generally consider a malpractice. Consider using $derived if possible.' ,
60
- ) ;
61
- } ) ;
64
+ expect ( content . suggestions ) . not . toContain (
65
+ 'The stateful variable "count" is assigned inside an $effect which is generally consider a malpractice. Consider using $derived if possible.' ,
66
+ ) ;
67
+ } ) ;
62
68
63
- it ( "should not add a suggestions for variables that are assigned within an effect but aren't stateful" , ( ) => {
64
- const content = run_autofixers_on_code ( `
65
- <script>
66
- const count = 0;
67
-
68
- $effect(() => {
69
- count = 43;
70
- });
71
- </script>` ) ;
69
+ it ( "should not add a suggestions for variables that are assigned within an effect but aren't stateful" , ( ) => {
70
+ const content = run_autofixers_on_code ( `
71
+ <script>
72
+ const count = 0;
73
+
74
+ $effect(() => {
75
+ count = 43;
76
+ });
77
+ </script>` ) ;
72
78
73
- expect ( content . suggestions ) . not . toContain (
74
- 'The stateful variable "count" is assigned inside an $effect which is generally consider a malpractice. Consider using $derived if possible.' ,
75
- ) ;
76
- } ) ;
79
+ expect ( content . suggestions ) . not . toContain (
80
+ 'The stateful variable "count" is assigned inside an $effect which is generally consider a malpractice. Consider using $derived if possible.' ,
81
+ ) ;
82
+ } ) ;
77
83
78
- it ( `should add a suggestion for variables that are assigned within an effect with an update` , ( ) => {
79
- const content = run_autofixers_on_code ( `
80
- <script>
81
- let count = $state (0);
82
-
83
- $effect(() => {
84
- count++;
85
- });
86
- </script>
87
- ` ) ;
84
+ it ( `should add a suggestion for variables that are assigned within an effect with an update` , ( ) => {
85
+ const content = run_autofixers_on_code ( `
86
+ <script>
87
+ let count = ${ init } (0);
88
+
89
+ $effect(() => {
90
+ count++;
91
+ });
92
+ </script>
93
+ ` ) ;
88
94
89
- expect ( content . suggestions ) . toContain (
90
- 'The stateful variable "count" is assigned inside an $effect which is generally consider a malpractice. Consider using $derived if possible.' ,
91
- ) ;
92
- } ) ;
95
+ expect ( content . suggestions ) . toContain (
96
+ 'The stateful variable "count" is assigned inside an $effect which is generally consider a malpractice. Consider using $derived if possible.' ,
97
+ ) ;
98
+ } ) ;
93
99
94
- it ( `should add a suggestion for variables that are mutated within an effect` , ( ) => {
95
- const content = run_autofixers_on_code ( `
96
- <script>
97
- let count = $state ({ value: 0 });
98
-
99
- $effect(() => {
100
- count.value = 42;
101
- });
102
- </script>
103
- ` ) ;
100
+ it ( `should add a suggestion for variables that are mutated within an effect` , ( ) => {
101
+ const content = run_autofixers_on_code ( `
102
+ <script>
103
+ let count = ${ init } ({ value: 0 });
104
+
105
+ $effect(() => {
106
+ count.value = 42;
107
+ });
108
+ </script>
109
+ ` ) ;
104
110
105
- expect ( content . suggestions ) . toContain (
106
- 'The stateful variable "count" is assigned inside an $effect which is generally consider a malpractice. Consider using $derived if possible.' ,
107
- ) ;
111
+ expect ( content . suggestions ) . toContain (
112
+ 'The stateful variable "count" is assigned inside an $effect which is generally consider a malpractice. Consider using $derived if possible.' ,
113
+ ) ;
114
+ } ) ;
108
115
} ) ;
109
116
} ) ;
110
117
0 commit comments