Skip to content

Commit

Permalink
tools: simplify prefer-assert-methods rule
Browse files Browse the repository at this point in the history
PR-URL: #17572
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
  • Loading branch information
cjihrig authored and MylesBorins committed Dec 12, 2017
1 parent cbecd9a commit bc83e8b
Showing 1 changed file with 8 additions and 19 deletions.
27 changes: 8 additions & 19 deletions tools/eslint-rules/prefer-assert-methods.js
Original file line number Diff line number Diff line change
@@ -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}'`;
Expand All @@ -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));
}
}
};
Expand Down

0 comments on commit bc83e8b

Please sign in to comment.