Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitdcd50f1

Browse files
committed
feat(prefer-predictable-action-arguments): change auto-fix into a suggestion for switching this on
Additionally, make this rule work within createMachine even if there is aeslint-plugin-xstate-include comment directive.
1 parent02ef191 commitdcd50f1

File tree

2 files changed

+160
-34
lines changed

2 files changed

+160
-34
lines changed

‎lib/rules/prefer-predictable-action-arguments.js‎

Lines changed: 60 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ module.exports = {
2222
'It is advised to configure predictableActionArguments: true at the top-level of your machine config',
2323
deprecatedPredictableActionArguments:
2424
'The predictableActionArguments prop was removed in XState v5 so it has no effect. Please remove it.',
25+
changeToTrue:'Change to true',
26+
27+
insertPredictableActionArguments:
28+
'Insert predictableActionArguments: true',
2529
},
30+
hasSuggestions:true,
2631
},
2732

2833
create:function(context){
@@ -43,34 +48,61 @@ module.exports = {
4348
node:predictableActionArgumentsProperty,
4449
messageId:'deprecatedPredictableActionArguments',
4550
fix(fixer){
51+
constnextToken=context.sourceCode.getTokenAfter(
52+
predictableActionArgumentsProperty
53+
)
54+
if(nextToken.type==='Punctuator'&&nextToken.value===','){
55+
returnfixer.removeRange([
56+
predictableActionArgumentsProperty.range[0],
57+
nextToken.range[1],
58+
])
59+
}
4660
returnfixer.remove(predictableActionArgumentsProperty)
4761
},
4862
})
4963
return
5064
}
5165

66+
// version 4
5267
if(!predictableActionArgumentsProperty){
5368
if(node.properties.length===0){
5469
context.report({
5570
node,
5671
messageId:'preferPredictableActionArguments',
57-
fix(fixer){
58-
returnfixer.replaceText(
59-
node,
60-
'{ predictableActionArguments: true }'
61-
)
62-
},
72+
suggest:[
73+
{
74+
messageId:'insertPredictableActionArguments',
75+
fix(fixer){
76+
returnfixer.replaceText(
77+
node,
78+
'{ predictableActionArguments: true }'
79+
)
80+
},
81+
},
82+
],
6383
})
6484
}else{
85+
// find the indentation of the first property
86+
const{ loc}=node.properties[0]
87+
constoffset=loc.start.column
88+
6589
context.report({
6690
node,
6791
messageId:'preferPredictableActionArguments',
68-
fix(fixer){
69-
returnfixer.insertTextBefore(
70-
node.properties[0],
71-
'predictableActionArguments: true,\n'
72-
)
73-
},
92+
suggest:[
93+
{
94+
messageId:'insertPredictableActionArguments',
95+
fix(fixer){
96+
returnfixer.insertTextBefore(
97+
node.properties[0],
98+
`predictableActionArguments: true,\n${''.padStart(
99+
offset,
100+
' '
101+
)}`
102+
)
103+
},
104+
},
105+
],
74106
})
75107
}
76108
return
@@ -83,12 +115,17 @@ module.exports = {
83115
context.report({
84116
node:predictableActionArgumentsProperty,
85117
messageId:'preferPredictableActionArguments',
86-
fix(fixer){
87-
returnfixer.replaceText(
88-
predictableActionArgumentsProperty.value,
89-
'true'
90-
)
91-
},
118+
suggest:[
119+
{
120+
messageId:'changeToTrue',
121+
fix(fixer){
122+
returnfixer.replaceText(
123+
predictableActionArgumentsProperty.value,
124+
'true'
125+
)
126+
},
127+
},
128+
],
92129
})
93130
}
94131
}
@@ -98,6 +135,11 @@ module.exports = {
98135
[`${prefix}> ObjectExpression:first-child`]:check,
99136
}
100137
:{
138+
// it should still produce errors within the createMachine call even if there is no context
139+
// to identify the root node
140+
'CallExpression[callee.name=/^createMachine$|^Machine$/] > ObjectExpression:first-child':
141+
check,
142+
101143
ObjectExpression(node){
102144
// check if it is a root state node config
103145
if(

‎tests/lib/rules/prefer-predictable-action-arguments.js‎

Lines changed: 100 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,21 @@ const tests = {
4545
predictableActionArguments: false
4646
})
4747
`,
48-
errors:[{messageId:'preferPredictableActionArguments'}],
49-
output:`
48+
errors:[
49+
{
50+
messageId:'preferPredictableActionArguments',
51+
suggestions:[
52+
{
53+
messageId:'changeToTrue',
54+
output:`
5055
createMachine({
5156
predictableActionArguments: true
5257
})
5358
`,
59+
},
60+
],
61+
},
62+
],
5463
}),
5564
withVersion(5,{
5665
code:`
@@ -65,28 +74,60 @@ const tests = {
6574
})
6675
`,
6776
}),
77+
// also removes a comma if there
78+
withVersion(5,{
79+
code:`
80+
createMachine({
81+
predictableActionArguments: false,
82+
})
83+
`,
84+
errors:[{messageId:'deprecatedPredictableActionArguments'}],
85+
output:`
86+
createMachine({
87+
88+
})
89+
`,
90+
}),
6891
withVersion(4,{
6992
code:`
7093
createMachine({})
7194
`,
72-
errors:[{messageId:'preferPredictableActionArguments'}],
73-
output:`
95+
errors:[
96+
{
97+
messageId:'preferPredictableActionArguments',
98+
suggestions:[
99+
{
100+
messageId:'insertPredictableActionArguments',
101+
output:`
74102
createMachine({ predictableActionArguments: true })
75103
`,
104+
},
105+
],
106+
},
107+
],
76108
}),
77109
withVersion(4,{
78110
code:`
79111
createMachine({
80112
initial: 'ready'
81113
})
82114
`,
83-
errors:[{messageId:'preferPredictableActionArguments'}],
84-
output:`
115+
errors:[
116+
{
117+
messageId:'preferPredictableActionArguments',
118+
suggestions:[
119+
{
120+
messageId:'insertPredictableActionArguments',
121+
output:`
85122
createMachine({
86123
predictableActionArguments: true,
87-
initial: 'ready'
124+
initial: 'ready'
88125
})
89126
`,
127+
},
128+
],
129+
},
130+
],
90131
}),
91132
withVersion(4,{
92133
code:`
@@ -95,30 +136,48 @@ initial: 'ready'
95136
predictableActionArguments: 42
96137
})
97138
`,
98-
errors:[{messageId:'preferPredictableActionArguments'}],
99-
output:`
139+
errors:[
140+
{
141+
messageId:'preferPredictableActionArguments',
142+
suggestions:[
143+
{
144+
messageId:'changeToTrue',
145+
output:`
100146
createMachine({
101147
states: {},
102148
predictableActionArguments: true
103149
})
104150
`,
151+
},
152+
],
153+
},
154+
],
105155
}),
106-
// errors reported outside of createMachine if there is the comment directive
156+
////errors reported outside of createMachine if there is the comment directive
107157
withVersion(4,{
108158
code:`
109159
/* eslint-plugin-xstate-include */
110160
const config = {
111161
context: {},
112162
}
113163
`,
114-
errors:[{messageId:'preferPredictableActionArguments'}],
115-
output:`
164+
errors:[
165+
{
166+
messageId:'preferPredictableActionArguments',
167+
suggestions:[
168+
{
169+
messageId:'insertPredictableActionArguments',
170+
output:`
116171
/* eslint-plugin-xstate-include */
117172
const config = {
118173
predictableActionArguments: true,
119-
context: {},
174+
context: {},
120175
}
121176
`,
177+
},
178+
],
179+
},
180+
],
122181
}),
123182
withVersion(4,{
124183
code:`
@@ -128,21 +187,30 @@ context: {},
128187
context: {},
129188
}
130189
`,
131-
errors:[{messageId:'preferPredictableActionArguments'}],
132-
output:`
190+
errors:[
191+
{
192+
messageId:'preferPredictableActionArguments',
193+
suggestions:[
194+
{
195+
messageId:'changeToTrue',
196+
output:`
133197
/* eslint-plugin-xstate-include */
134198
const config = {
135199
predictableActionArguments: true,
136200
context: {},
137201
}
138202
`,
203+
},
204+
],
205+
},
206+
],
139207
}),
140208
withVersion(5,{
141209
code:`
142210
/* eslint-plugin-xstate-include */
143211
const config = {
144212
context: {},
145-
predictableActionArguments: false
213+
predictableActionArguments: false,
146214
}
147215
`,
148216
errors:[{messageId:'deprecatedPredictableActionArguments'}],
@@ -154,6 +222,22 @@ context: {},
154222
}
155223
`,
156224
}),
225+
// // error reported even if there is no context property, because we are within the createMachine call
226+
withVersion(5,{
227+
code:`
228+
/* eslint-plugin-xstate-include */
229+
const machine = createMachine({
230+
predictableActionArguments: false
231+
})
232+
`,
233+
errors:[{messageId:'deprecatedPredictableActionArguments'}],
234+
output:`
235+
/* eslint-plugin-xstate-include */
236+
const machine = createMachine({
237+
238+
})
239+
`,
240+
}),
157241
],
158242
}
159243

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp