Skip to content

Commit 60ee303

Browse files
shobhitchittoraMylesBorins
authored andcommitted
tools: auto fix custom eslint rule
1. Extends tests 2. Refactors code 3. Adds fixer Refs: #16636 PR-URL: #16652 Refs: #16636 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent 874361d commit 60ee303

File tree

2 files changed

+43
-12
lines changed

2 files changed

+43
-12
lines changed

test/parallel/test-eslint-prefer-assert-methods.js

+26-11
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,46 @@ const rule = require('../../tools/eslint-rules/prefer-assert-methods');
77

88
new RuleTester().run('prefer-assert-methods', rule, {
99
valid: [
10-
'assert.strictEqual(foo, bar)',
11-
'assert(foo === bar && baz)'
10+
'assert.strictEqual(foo, bar);',
11+
'assert(foo === bar && baz);',
12+
'assert.notStrictEqual(foo, bar);',
13+
'assert(foo !== bar && baz);',
14+
'assert.equal(foo, bar);',
15+
'assert(foo == bar && baz);',
16+
'assert.notEqual(foo, bar);',
17+
'assert(foo != bar && baz);',
18+
'assert.ok(foo);',
19+
'assert.ok(foo != bar);',
20+
'assert.ok(foo === bar && baz);'
1221
],
1322
invalid: [
1423
{
15-
code: 'assert(foo == bar)',
16-
errors: [{ message: "'assert.equal' should be used instead of '=='" }]
24+
code: 'assert(foo == bar);',
25+
errors: [{
26+
message: "'assert.equal' should be used instead of '=='"
27+
}],
28+
output: 'assert.equal(foo, bar);'
1729
},
1830
{
19-
code: 'assert(foo === bar)',
31+
code: 'assert(foo === bar);',
2032
errors: [{
2133
message: "'assert.strictEqual' should be used instead of '==='"
22-
}]
34+
}],
35+
output: 'assert.strictEqual(foo, bar);'
2336
},
2437
{
25-
code: 'assert(foo != bar)',
38+
code: 'assert(foo != bar);',
2639
errors: [{
2740
message: "'assert.notEqual' should be used instead of '!='"
28-
}]
41+
}],
42+
output: 'assert.notEqual(foo, bar);'
2943
},
3044
{
31-
code: 'assert(foo !== bar)',
45+
code: 'assert(foo !== bar);',
3246
errors: [{
3347
message: "'assert.notStrictEqual' should be used instead of '!=='"
34-
}]
35-
},
48+
}],
49+
output: 'assert.notStrictEqual(foo, bar);'
50+
}
3651
]
3752
});

tools/eslint-rules/prefer-assert-methods.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/**
2+
* @fileoverview Prohibit the use of assert operators ( ===, !==, ==, != )
3+
*/
4+
15
'use strict';
26

37
const astSelector = 'ExpressionStatement[expression.type="CallExpression"]' +
@@ -21,7 +25,19 @@ module.exports = function(context) {
2125
const arg = node.expression.arguments[0];
2226
const assertMethod = preferedAssertMethod[arg.operator];
2327
if (assertMethod) {
24-
context.report(node, parseError(assertMethod, arg.operator));
28+
context.report({
29+
node,
30+
message: parseError(assertMethod, arg.operator),
31+
fix: (fixer) => {
32+
const sourceCode = context.getSourceCode();
33+
const left = sourceCode.getText(arg.left);
34+
const right = sourceCode.getText(arg.right);
35+
return fixer.replaceText(
36+
node,
37+
`assert.${assertMethod}(${left}, ${right});`
38+
);
39+
}
40+
});
2541
}
2642
}
2743
};

0 commit comments

Comments
 (0)