Skip to content

Commit a8b5a96

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 c0f40be commit a8b5a96

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
@@ -9,31 +9,46 @@ const rule = require('../../tools/eslint-rules/prefer-assert-methods');
99

1010
new RuleTester().run('prefer-assert-methods', rule, {
1111
valid: [
12-
'assert.strictEqual(foo, bar)',
13-
'assert(foo === bar && baz)'
12+
'assert.strictEqual(foo, bar);',
13+
'assert(foo === bar && baz);',
14+
'assert.notStrictEqual(foo, bar);',
15+
'assert(foo !== bar && baz);',
16+
'assert.equal(foo, bar);',
17+
'assert(foo == bar && baz);',
18+
'assert.notEqual(foo, bar);',
19+
'assert(foo != bar && baz);',
20+
'assert.ok(foo);',
21+
'assert.ok(foo != bar);',
22+
'assert.ok(foo === bar && baz);'
1423
],
1524
invalid: [
1625
{
17-
code: 'assert(foo == bar)',
18-
errors: [{ message: "'assert.equal' should be used instead of '=='" }]
26+
code: 'assert(foo == bar);',
27+
errors: [{
28+
message: "'assert.equal' should be used instead of '=='"
29+
}],
30+
output: 'assert.equal(foo, bar);'
1931
},
2032
{
21-
code: 'assert(foo === bar)',
33+
code: 'assert(foo === bar);',
2234
errors: [{
2335
message: "'assert.strictEqual' should be used instead of '==='"
24-
}]
36+
}],
37+
output: 'assert.strictEqual(foo, bar);'
2538
},
2639
{
27-
code: 'assert(foo != bar)',
40+
code: 'assert(foo != bar);',
2841
errors: [{
2942
message: "'assert.notEqual' should be used instead of '!='"
30-
}]
43+
}],
44+
output: 'assert.notEqual(foo, bar);'
3145
},
3246
{
33-
code: 'assert(foo !== bar)',
47+
code: 'assert(foo !== bar);',
3448
errors: [{
3549
message: "'assert.notStrictEqual' should be used instead of '!=='"
36-
}]
37-
},
50+
}],
51+
output: 'assert.notStrictEqual(foo, bar);'
52+
}
3853
]
3954
});

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)