You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
|[event-names](docs/rules/event-names.md)| Suggest consistent formatting of event names||
106
140
|[state-names](docs/rules/state-names.md)| Suggest consistent formatting of state names and prevent confusing names||
141
+
142
+
##Comment Directives
143
+
144
+
By default, the plugin lints only code within the`createMachine` or`Machine` calls. However, if your machine configuration is imported from another file, you will need to enable this plugin's rules by adding a comment directive to the top of the file:
Copy file name to clipboardExpand all lines: docs/rules/no-async-guard.md
+33-13Lines changed: 33 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,12 +4,12 @@
4
4
5
5
##Rule Details
6
6
7
-
Async functions return a promise which is a truthy value. Therefore, async guard functions always pass. Transitions guarded by such functions will always be taken as if no`cond` was specified.
7
+
Async functions return a promise which is a truthy value. Therefore, async guard functions always pass. Transitions guarded by such functions will always be taken as if no`cond`(XState v4) or`guard` (XState v5)was specified.
8
8
9
9
Examples of**incorrect** code for this rule:
10
10
11
11
```javascript
12
-
// ❌ async guard in an event transition
12
+
// ❌ async guard in an event transition (XState v4)
13
13
createMachine({
14
14
on: {
15
15
EVENT: {
@@ -19,37 +19,47 @@ createMachine({
19
19
},
20
20
})
21
21
22
-
// ❌ async guard in an onDone transition
22
+
// ❌ async guard in an event transition (XState v5)
23
+
createMachine({
24
+
on: {
25
+
EVENT: {
26
+
guard:async ()=> {},
27
+
target:'active',
28
+
},
29
+
},
30
+
})
31
+
32
+
// ❌ async guard in an onDone transition (XState v5)
23
33
createMachine({
24
34
states: {
25
35
active: {
26
36
invoke: {
27
37
src:'myService',
28
38
onDone: {
29
-
cond:asyncfunction () {},
39
+
guard:asyncfunction () {},
30
40
target:'finished',
31
41
},
32
42
},
33
43
},
34
44
},
35
45
})
36
46
37
-
// ❌ async guard in the choose action creator
47
+
// ❌ async guard in the choose action creator (XState v5)
38
48
createMachine({
39
49
entry:choose([
40
50
{
41
-
cond:async ()=> {},
51
+
guard:async ()=> {},
42
52
actions:'myAction',
43
53
},
44
54
]),
45
55
})
46
56
47
-
// ❌ async guards in machine options
57
+
// ❌ async guards in machine options (XState v5)
48
58
createMachine(
49
59
{
50
60
on: {
51
61
EVENT: {
52
-
cond:'myGuard',
62
+
guard:'myGuard',
53
63
target:'active',
54
64
},
55
65
},
@@ -67,7 +77,7 @@ createMachine(
67
77
Examples of**correct** code for this rule:
68
78
69
79
```javascript
70
-
// ✅ guard is synchronous
80
+
// ✅ guard is synchronous (XState v4)
71
81
createMachine({
72
82
on: {
73
83
EVENT: {
@@ -77,27 +87,37 @@ createMachine({
77
87
},
78
88
})
79
89
80
-
// ✅ guard is synchronous
90
+
// ✅ guard is synchronous (XState v5)
91
+
createMachine({
92
+
on: {
93
+
EVENT: {
94
+
guard: ()=> {},
95
+
target:'active',
96
+
},
97
+
},
98
+
})
99
+
100
+
// ✅ guard is synchronous (XState v5)
81
101
createMachine({
82
102
states: {
83
103
active: {
84
104
invoke: {
85
105
src:'myService',
86
106
onDone: {
87
-
cond:function () {},
107
+
guard:function () {},
88
108
target:'finished',
89
109
},
90
110
},
91
111
},
92
112
},
93
113
})
94
114
95
-
// ✅ all guards in machine options are synchronous
115
+
// ✅ all guards in machine options are synchronous (XState v5)
Avoid blindly forwarding all events to invoked services or spawned actors - it may lead to unexpected behavior or infinite loops. The official documentation[suggests sending events explicitly](https://xstate.js.org/docs/guides/communication.html#the-invoke-property) with the[`forwardTo`](https://xstate.js.org/docs/guides/actions.html#forward-to-action) or`send` action creators.
8
8
9
+
**XState v5 removed the`autoForward` option. This rule will report errors if`autoForward` is used with XState v5.**
10
+
9
11
Examples of**incorrect** code for this rule:
10
12
11
13
```javascript
@@ -33,7 +35,7 @@ createMachine({
33
35
})
34
36
```
35
37
36
-
Examples of**correct** code for this rule:
38
+
Examples of**correct** code for this rule (XState v4 only):