From bc83e8b7a378fb01931329d9e57a38a523f0cb2b Mon Sep 17 00:00:00 2001 From: cjihrig Date: Sat, 9 Dec 2017 09:54:33 -0500 Subject: [PATCH] tools: simplify prefer-assert-methods rule PR-URL: https://github.com/nodejs/node/pull/17572 Reviewed-By: Anatoli Papirovski --- tools/eslint-rules/prefer-assert-methods.js | 27 ++++++--------------- 1 file changed, 8 insertions(+), 19 deletions(-) diff --git a/tools/eslint-rules/prefer-assert-methods.js b/tools/eslint-rules/prefer-assert-methods.js index fa345eb7c3fc33..0604fd3ed99046 100644 --- a/tools/eslint-rules/prefer-assert-methods.js +++ b/tools/eslint-rules/prefer-assert-methods.js @@ -1,15 +1,8 @@ 'use strict'; -function isAssert(node) { - return node.expression && - node.expression.type === 'CallExpression' && - node.expression.callee && - node.expression.callee.name === 'assert'; -} - -function getFirstArg(expression) { - return expression.arguments && expression.arguments[0]; -} +const astSelector = 'ExpressionStatement[expression.type="CallExpression"]' + + '[expression.callee.name="assert"]' + + '[expression.arguments.0.type="BinaryExpression"]'; function parseError(method, op) { return `'assert.${method}' should be used instead of '${op}'`; @@ -24,15 +17,11 @@ const preferedAssertMethod = { module.exports = function(context) { return { - ExpressionStatement(node) { - if (isAssert(node)) { - const arg = getFirstArg(node.expression); - if (arg && arg.type === 'BinaryExpression') { - const assertMethod = preferedAssertMethod[arg.operator]; - if (assertMethod) { - context.report(node, parseError(assertMethod, arg.operator)); - } - } + [astSelector]: function(node) { + const arg = node.expression.arguments[0]; + const assertMethod = preferedAssertMethod[arg.operator]; + if (assertMethod) { + context.report(node, parseError(assertMethod, arg.operator)); } } };