7
7
isFunctionExpression,
8
8
isCallExpression,
9
9
} = require ( '../utils/predicates' )
10
+ const getSelectorPrefix = require ( '../utils/getSelectorPrefix' )
11
+ const getSettings = require ( '../utils/getSettings' )
10
12
11
- function isObjectWithGuard ( node ) {
12
- return node . type === 'ObjectExpression' && hasProperty ( 'cond' , node )
13
+ function isObjectWithGuard ( node , version ) {
14
+ return (
15
+ node . type === 'ObjectExpression' &&
16
+ hasProperty ( version > 4 ? 'guard' : 'cond' , node )
17
+ )
13
18
}
14
19
15
20
function isValidAction ( node ) {
@@ -21,17 +26,25 @@ function isValidAction(node) {
21
26
)
22
27
}
23
28
24
- const entryActionDeclaration =
25
- 'CallExpression[callee.name=/^createMachine$|^Machine$/] Property[key.name!="states"] > ObjectExpression > Property[key.name="entry"]'
29
+ const entryActionDeclaration = ( prefix ) =>
30
+ prefix === ''
31
+ ? 'Property[key.name!="states"] > ObjectExpression > Property[key.name="entry"]'
32
+ : `${ prefix } Property[key.name!="states"] > ObjectExpression > Property[key.name="entry"]`
26
33
27
- const rootEntryActionDeclaration =
28
- 'CallExpression[callee.name=/^createMachine$|^Machine$/] > ObjectExpression:nth-child(1) > Property[key.name="entry"]'
34
+ const rootEntryActionDeclaration = ( prefix ) =>
35
+ prefix === ''
36
+ ? 'Property[key.name="entry"]'
37
+ : `${ prefix } > ObjectExpression:nth-child(1) > Property[key.name="entry"]`
29
38
30
- const exitActionDeclaration =
31
- 'CallExpression[callee.name=/^createMachine$|^Machine$/] Property[key.name!="states"] > ObjectExpression > Property[key.name="exit"]'
39
+ const exitActionDeclaration = ( prefix ) =>
40
+ prefix === ''
41
+ ? 'Property[key.name!="states"] > ObjectExpression > Property[key.name="exit"]'
42
+ : `${ prefix } Property[key.name!="states"] > ObjectExpression > Property[key.name="exit"]`
32
43
33
- const rootExitActionDeclaration =
34
- 'CallExpression[callee.name=/^createMachine$|^Machine$/] > ObjectExpression:nth-child(1) > Property[key.name="exit"]'
44
+ const rootExitActionDeclaration = ( prefix ) =>
45
+ prefix === ''
46
+ ? 'Property[key.name="exit"]'
47
+ : `${ prefix } > ObjectExpression:nth-child(1) > Property[key.name="exit"]`
35
48
36
49
module . exports = {
37
50
meta : {
@@ -48,7 +61,7 @@ module.exports = {
48
61
invalidGuardedEntryAction :
49
62
'Invalid declaration of an "entry" action. Use the "choose" or "pure" action creators to specify a conditional entry action.' ,
50
63
invalidGuardedExitAction :
51
- 'Invalid declaration of an "entry " action. Use the "choose" or "pure" action creators to specify a conditional entry action.' ,
64
+ 'Invalid declaration of an "exit " action. Use the "choose" or "pure" action creators to specify a conditional exit action.' ,
52
65
invalidEntryAction :
53
66
'The "entry" action has an invalid value. Specify a function, string, variable, action creator call, action object, or an array of those.' ,
54
67
invalidExitAction :
@@ -57,8 +70,10 @@ module.exports = {
57
70
} ,
58
71
59
72
create : function ( context ) {
73
+ const { version } = getSettings ( context )
74
+ const prefix = getSelectorPrefix ( context . sourceCode )
60
75
const validateAction = ( actionType ) => ( node ) => {
61
- if ( isObjectWithGuard ( node . value ) ) {
76
+ if ( isObjectWithGuard ( node . value , version ) ) {
62
77
context . report ( {
63
78
node,
64
79
messageId :
@@ -80,7 +95,7 @@ module.exports = {
80
95
81
96
if ( node . value . type === 'ArrayExpression' ) {
82
97
node . value . elements . forEach ( ( element ) => {
83
- if ( isObjectWithGuard ( element ) ) {
98
+ if ( isObjectWithGuard ( element , version ) ) {
84
99
context . report ( {
85
100
node : element ,
86
101
messageId :
@@ -101,10 +116,10 @@ module.exports = {
101
116
}
102
117
}
103
118
return {
104
- [ entryActionDeclaration ] : validateAction ( 'entry' ) ,
105
- [ rootEntryActionDeclaration ] : validateAction ( 'entry' ) ,
106
- [ exitActionDeclaration ] : validateAction ( 'exit' ) ,
107
- [ rootExitActionDeclaration ] : validateAction ( 'exit' ) ,
119
+ [ entryActionDeclaration ( prefix ) ] : validateAction ( 'entry' ) ,
120
+ [ rootEntryActionDeclaration ( prefix ) ] : validateAction ( 'entry' ) ,
121
+ [ exitActionDeclaration ( prefix ) ] : validateAction ( 'exit' ) ,
122
+ [ rootExitActionDeclaration ( prefix ) ] : validateAction ( 'exit' ) ,
108
123
}
109
124
} ,
110
125
}
0 commit comments