Permalink
Browse files

tools: simplify prefer-assert-methods rule

PR-URL: #17572
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
  • Loading branch information...
cjihrig authored and gibfahn committed Dec 9, 2017
1 parent 3e70ee8 commit 53834226723821b67736ec6b4805d75732d05af5
Showing with 8 additions and 19 deletions.
  1. +8 −19 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));
}
}
};

0 comments on commit 5383422

Please sign in to comment.