diff --git a/test/parallel/test-eslint-prefer-assert-methods.js b/test/parallel/test-eslint-prefer-assert-methods.js index 57596132d88ec3..2d05a4851bf781 100644 --- a/test/parallel/test-eslint-prefer-assert-methods.js +++ b/test/parallel/test-eslint-prefer-assert-methods.js @@ -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);', + '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);' + } ] }); diff --git a/tools/eslint-rules/prefer-assert-methods.js b/tools/eslint-rules/prefer-assert-methods.js index 0604fd3ed99046..2917d40de40810 100644 --- a/tools/eslint-rules/prefer-assert-methods.js +++ b/tools/eslint-rules/prefer-assert-methods.js @@ -1,3 +1,7 @@ +/** + * @fileoverview Prohibit the use of assert operators ( ===, !==, ==, != ) + */ + 'use strict'; const astSelector = 'ExpressionStatement[expression.type="CallExpression"]' + @@ -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});` + ); + } + }); } } };