Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tools: auto fix custom eslint rule for prefer-assert-methods.js #16652

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -7,31 +7,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);',
Copy link
Contributor

@apapirovski apapirovski Dec 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could be wrong but I think the idea was that assert(foo != bar) should yield the same as assert.ok(foo != bar). Since the former is an alias for the latter. That is, they should both report an error.

(I realize that might be modifying the current rule and is somewhat outside of the scope of the original work.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would indeed be nice but I guess it is best to keep that for a separate PR and I am actually about to improve the assert message for cases like that in #17581

'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