1
1
/**
2
- * @fileoverview enforces always return from a fixer function
2
+ * @fileoverview Require fixer function to return a fix
3
3
* @author 薛定谔的猫<[email protected] >
4
4
*/
5
5
12
12
const rule = require ( '../../../lib/rules/fixer-return' ) ;
13
13
const RuleTester = require ( 'eslint' ) . RuleTester ;
14
14
15
- const ERROR = { messageId : 'missingFix' , type : 'FunctionExpression' } ;
16
-
17
15
// ------------------------------------------------------------------------------
18
16
// Tests
19
17
// ------------------------------------------------------------------------------
@@ -46,6 +44,38 @@ ruleTester.run('fixer-return', rule, {
46
44
}
47
45
};
48
46
` ,
47
+ // Not the right fix function.
48
+ `
49
+ module.exports = {
50
+ create: function(context) {
51
+ context.report( {
52
+ notFix: function(fixer) {
53
+ }
54
+ });
55
+ }
56
+ };
57
+ ` ,
58
+ // Not the right fix function (arrow function with implied return)
59
+ `
60
+ module.exports = {
61
+ create: function(context) {
62
+ context.report( {
63
+ notFix: fixer => undefined
64
+ });
65
+ }
66
+ };
67
+ ` ,
68
+ // Not the right fix function (arrow function)
69
+ `
70
+ module.exports = {
71
+ create: function(context) {
72
+ context.report( {
73
+ notFix: fixer => {}
74
+ });
75
+ }
76
+ };
77
+ ` ,
78
+ // Arrow function (expression)
49
79
`
50
80
module.exports = {
51
81
create: function(context) {
@@ -55,6 +85,17 @@ ruleTester.run('fixer-return', rule, {
55
85
}
56
86
};
57
87
` ,
88
+ // Arrow function (with return)
89
+ `
90
+ module.exports = {
91
+ create: function(context) {
92
+ context.report({
93
+ fix: fixer => {return fixer.foo();}
94
+ });
95
+ }
96
+ };
97
+ ` ,
98
+ // Generator
58
99
`
59
100
module.exports = {
60
101
create: function (context) {
@@ -196,7 +237,50 @@ ruleTester.run('fixer-return', rule, {
196
237
}
197
238
};
198
239
` ,
199
- errors : [ ERROR ] ,
240
+ errors : [ { messageId : 'missingFix' , type : 'FunctionExpression' , line : 5 , column : 24 } ] ,
241
+ } ,
242
+ {
243
+ // Fix but missing return (arrow function, report on arrow)
244
+ code : `
245
+ module.exports = {
246
+ create: function(context) {
247
+ context.report({
248
+ fix: (fixer) => {
249
+ fixer.foo();
250
+ }
251
+ });
252
+ }
253
+ };
254
+ ` ,
255
+ errors : [ { messageId : 'missingFix' , type : 'ArrowFunctionExpression' , line : 5 , endLine : 5 , column : 34 , endColumn : 36 } ] ,
256
+ } ,
257
+ {
258
+ // With no autofix (arrow function, explicit return, report on arrow)
259
+ code : `
260
+ module.exports = {
261
+ create: function(context) {
262
+ context.report({
263
+ fix: (fixer) => {
264
+ return undefined;
265
+ }
266
+ });
267
+ }
268
+ };
269
+ ` ,
270
+ errors : [ { messageId : 'missingFix' , type : 'ArrowFunctionExpression' , line : 5 , endLine : 5 , column : 34 , endColumn : 36 } ] ,
271
+ } ,
272
+ {
273
+ // With no autofix (arrow function, implied return, report on arrow)
274
+ code : `
275
+ module.exports = {
276
+ create: function(context) {
277
+ context.report( {
278
+ fix: fixer => undefined
279
+ });
280
+ }
281
+ };
282
+ ` ,
283
+ errors : [ { messageId : 'missingFix' , type : 'ArrowFunctionExpression' , line : 5 , endLine : 5 , column : 32 , endColumn : 34 } ] ,
200
284
} ,
201
285
{
202
286
// Fix but missing yield (generator)
@@ -211,22 +295,22 @@ ruleTester.run('fixer-return', rule, {
211
295
}
212
296
};
213
297
` ,
214
- errors : [ ERROR ] ,
298
+ errors : [ { messageId : 'missingFix' , type : 'FunctionExpression' , line : 5 , column : 25 } ] ,
215
299
} ,
216
300
{
217
301
// With no autofix (only yield undefined)
218
302
code : `
219
- module.exports = {
220
- create: function(context) {
221
- context.report({
222
- *fix(fixer) {
223
- yield undefined;
224
- }
225
- });
226
- }
227
- };
228
- ` ,
229
- errors : [ ERROR ] ,
303
+ module.exports = {
304
+ create: function(context) {
305
+ context.report({
306
+ *fix(fixer) {
307
+ yield undefined;
308
+ }
309
+ });
310
+ }
311
+ };
312
+ ` ,
313
+ errors : [ { messageId : 'missingFix' , type : 'FunctionExpression' , line : 5 , column : 25 } ] ,
230
314
} ,
231
315
{
232
316
// With no autofix (only return null)
@@ -241,7 +325,7 @@ ruleTester.run('fixer-return', rule, {
241
325
}
242
326
};
243
327
` ,
244
- errors : [ ERROR ] ,
328
+ errors : [ { messageId : 'missingFix' , type : 'FunctionExpression' , line : 5 , column : 26 } ] ,
245
329
} ,
246
330
{
247
331
// With no autofix (only return undefined)
@@ -256,23 +340,23 @@ ruleTester.run('fixer-return', rule, {
256
340
}
257
341
};
258
342
` ,
259
- errors : [ ERROR ] ,
343
+ errors : [ { messageId : 'missingFix' , type : 'FunctionExpression' , line : 5 , column : 26 } ] ,
260
344
} ,
261
345
{
262
346
// With no autofix (only return undefined, but in variable)
263
347
code : `
264
- module.exports = {
265
- create: function(context) {
266
- context.report( {
267
- fix: function(fixer) {
268
- const returnValue = undefined;
269
- return returnValue;
270
- }
271
- });
272
- }
273
- };
274
- ` ,
275
- errors : [ ERROR ] ,
348
+ module.exports = {
349
+ create: function(context) {
350
+ context.report( {
351
+ fix: function(fixer) {
352
+ const returnValue = undefined;
353
+ return returnValue;
354
+ }
355
+ });
356
+ }
357
+ };
358
+ ` ,
359
+ errors : [ { messageId : 'missingFix' , type : 'FunctionExpression' , line : 5 , column : 26 } ] ,
276
360
} ,
277
361
{
278
362
// With no autofix (only return implicit undefined)
@@ -287,7 +371,7 @@ ruleTester.run('fixer-return', rule, {
287
371
}
288
372
};
289
373
` ,
290
- errors : [ ERROR ] ,
374
+ errors : [ { messageId : 'missingFix' , type : 'FunctionExpression' , line : 5 , column : 26 } ] ,
291
375
} ,
292
376
{
293
377
// With no autofix (only return empty array)
@@ -302,7 +386,7 @@ ruleTester.run('fixer-return', rule, {
302
386
}
303
387
};
304
388
` ,
305
- errors : [ ERROR ] ,
389
+ errors : [ { messageId : 'missingFix' , type : 'FunctionExpression' , line : 5 , column : 26 } ] ,
306
390
} ,
307
391
{
308
392
// With no autofix (no return, empty function)
@@ -316,7 +400,7 @@ ruleTester.run('fixer-return', rule, {
316
400
}
317
401
};
318
402
` ,
319
- errors : [ ERROR ] ,
403
+ errors : [ { messageId : 'missingFix' , type : 'FunctionExpression' , line : 5 , column : 26 } ] ,
320
404
} ,
321
405
] ,
322
406
} ) ;
0 commit comments