Skip to content

Commit

Permalink
tools: auto fix custom eslint rule
Browse files Browse the repository at this point in the history
1. Extends tests
2. Refactors code
3. Adds fixer

Refs: #16636

PR-URL: #16652
Refs: #16636
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
  • Loading branch information
shobhitchittora authored and MylesBorins committed Mar 20, 2018
1 parent 265faf8 commit 1f2269a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
37 changes: 26 additions & 11 deletions test/parallel/test-eslint-prefer-assert-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,46 @@ const rule = require('../../tools/eslint-rules/prefer-assert-methods');

new RuleTester().run('prefer-assert-methods', rule, {
valid: [
'assert.strictEqual(foo, bar)',
'assert(foo === bar && baz)'
'assert.strictEqual(foo, bar);',
'assert(foo === bar && baz);',
'assert.notStrictEqual(foo, bar);',
'assert(foo !== bar && baz);',
'assert.equal(foo, bar);',
'assert(foo == bar && baz);',
'assert.notEqual(foo, bar);',
'assert(foo != bar && baz);',
'assert.ok(foo);',
'assert.ok(foo != bar);',
'assert.ok(foo === bar && baz);'
],
invalid: [
{
code: 'assert(foo == bar)',
errors: [{ message: "'assert.equal' should be used instead of '=='" }]
code: 'assert(foo == bar);',
errors: [{
message: "'assert.equal' should be used instead of '=='"
}],
output: 'assert.equal(foo, bar);'
},
{
code: 'assert(foo === bar)',
code: 'assert(foo === bar);',
errors: [{
message: "'assert.strictEqual' should be used instead of '==='"
}]
}],
output: 'assert.strictEqual(foo, bar);'
},
{
code: 'assert(foo != bar)',
code: 'assert(foo != bar);',
errors: [{
message: "'assert.notEqual' should be used instead of '!='"
}]
}],
output: 'assert.notEqual(foo, bar);'
},
{
code: 'assert(foo !== bar)',
code: 'assert(foo !== bar);',
errors: [{
message: "'assert.notStrictEqual' should be used instead of '!=='"
}]
},
}],
output: 'assert.notStrictEqual(foo, bar);'
}
]
});
18 changes: 17 additions & 1 deletion tools/eslint-rules/prefer-assert-methods.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/**
* @fileoverview Prohibit the use of assert operators ( ===, !==, ==, != )
*/

'use strict';

const astSelector = 'ExpressionStatement[expression.type="CallExpression"]' +
Expand All @@ -21,7 +25,19 @@ module.exports = function(context) {
const arg = node.expression.arguments[0];
const assertMethod = preferedAssertMethod[arg.operator];
if (assertMethod) {
context.report(node, parseError(assertMethod, arg.operator));
context.report({
node,
message: parseError(assertMethod, arg.operator),
fix: (fixer) => {
const sourceCode = context.getSourceCode();
const left = sourceCode.getText(arg.left);
const right = sourceCode.getText(arg.right);
return fixer.replaceText(
node,
`assert.${assertMethod}(${left}, ${right});`
);
}
});
}
}
};
Expand Down

0 comments on commit 1f2269a

Please sign in to comment.