From 16e64b5572d0a87142365d7c28a756a75420e548 Mon Sep 17 00:00:00 2001 From: Shobhit Chittora Date: Wed, 1 Nov 2017 02:12:56 +0530 Subject: [PATCH 1/2] tools: auto fix custom eslint rule for prefer-assert-methods.js 1. Extends tests 2. Refactors code 3. Adds fixer Refs: #16636 --- .../test-eslint-prefer-assert-methods.js | 37 +++++++++----- tools/eslint-rules/prefer-assert-methods.js | 48 ++++++++++++++----- 2 files changed, 63 insertions(+), 22 deletions(-) diff --git a/test/parallel/test-eslint-prefer-assert-methods.js b/test/parallel/test-eslint-prefer-assert-methods.js index 2129c083224f0e..d9f935b1694cf2 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..18946617d2f620 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"]' + @@ -8,20 +12,42 @@ function parseError(method, op) { return `'assert.${method}' should be used instead of '${op}'`; } -const preferedAssertMethod = { - '===': 'strictEqual', - '!==': 'notStrictEqual', - '==': 'equal', - '!=': 'notEqual' -}; +function checkExpression(arg) { + const type = arg.type; + return arg && (type === 'BinaryExpression'); +} + +function preferedAssertMethod(op) { + switch (op) { + case '===': return 'strictEqual'; + case '!==': return 'notStrictEqual'; + case '==': return 'equal'; + case '!=': return 'notEqual'; + } +} module.exports = function(context) { return { - [astSelector]: function(node) { - const arg = node.expression.arguments[0]; - const assertMethod = preferedAssertMethod[arg.operator]; - if (assertMethod) { - context.report(node, parseError(assertMethod, arg.operator)); + ExpressionStatement(node) { + if (isAssert(node)) { + const arg = getFirstArg(node.expression); + if (checkExpression(arg)) { + const assertMethod = preferedAssertMethod(arg.operator); + if (assertMethod) { + context.report({ + node, + message: parseError(assertMethod, arg.operator), + fix: (fixer) => { + const sourceCode = context.getSourceCode(); + const args = `${sourceCode.getText(arg)}`; + return fixer.replaceText( + node, + `assert.${assertMethod}(${args});` + ); + } + }); + } + } } } }; From 0ee62da04d1dc01d5f0ec99b31e84f310b5f6e80 Mon Sep 17 00:00:00 2001 From: Shobhit Chittora Date: Fri, 15 Dec 2017 15:54:52 +0530 Subject: [PATCH 2/2] updated as per new implementation --- tools/eslint-rules/prefer-assert-methods.js | 52 +++++++++------------ 1 file changed, 21 insertions(+), 31 deletions(-) diff --git a/tools/eslint-rules/prefer-assert-methods.js b/tools/eslint-rules/prefer-assert-methods.js index 18946617d2f620..2917d40de40810 100644 --- a/tools/eslint-rules/prefer-assert-methods.js +++ b/tools/eslint-rules/prefer-assert-methods.js @@ -12,42 +12,32 @@ function parseError(method, op) { return `'assert.${method}' should be used instead of '${op}'`; } -function checkExpression(arg) { - const type = arg.type; - return arg && (type === 'BinaryExpression'); -} - -function preferedAssertMethod(op) { - switch (op) { - case '===': return 'strictEqual'; - case '!==': return 'notStrictEqual'; - case '==': return 'equal'; - case '!=': return 'notEqual'; - } -} +const preferedAssertMethod = { + '===': 'strictEqual', + '!==': 'notStrictEqual', + '==': 'equal', + '!=': 'notEqual' +}; module.exports = function(context) { return { - ExpressionStatement(node) { - if (isAssert(node)) { - const arg = getFirstArg(node.expression); - if (checkExpression(arg)) { - const assertMethod = preferedAssertMethod(arg.operator); - if (assertMethod) { - context.report({ + [astSelector]: function(node) { + const arg = node.expression.arguments[0]; + const assertMethod = preferedAssertMethod[arg.operator]; + if (assertMethod) { + 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, - message: parseError(assertMethod, arg.operator), - fix: (fixer) => { - const sourceCode = context.getSourceCode(); - const args = `${sourceCode.getText(arg)}`; - return fixer.replaceText( - node, - `assert.${assertMethod}(${args});` - ); - } - }); + `assert.${assertMethod}(${left}, ${right});` + ); } - } + }); } } };