diff --git a/lib/rules/prefer-is-nil.js b/lib/rules/prefer-is-nil.js index fc2d505..d780f2e 100644 --- a/lib/rules/prefer-is-nil.js +++ b/lib/rules/prefer-is-nil.js @@ -11,37 +11,37 @@ module.exports = function (context) { var astUtil = require('../util/astUtil'); var lodashUtil = require('../util/lodashUtil'); var settings = require('../util/settingsUtil').getSettings(context); + var _ = require('lodash'); var nilChecks = { null: { - isValue: function isNullLiteral(node) { - return node.type === 'Literal' && node.value === null; - }, - expressionChecks: [getLodashTypeChecked.bind(null, 'isNull'), getValueComparedToNil.bind(null, 'null')] + isValue: _.matches({type: 'Literal', value: null}), + expressionChecks: [getLodashTypeCheckedBy('isNull'), getValueComparedTo('null')] }, undefined: { - isValue: function isUndefinedIdentifier(node) { - return node.type === 'Identifier' && node.name === 'undefined'; - }, - expressionChecks: [getLodashTypeChecked.bind(null, 'isUndefined'), getValueComparedToNil.bind(null, 'undefined'), getValueWithTypeofUndefinedComparison] + isValue: _.matches({type: 'Identifier', name: 'undefined'}), + expressionChecks: [getLodashTypeCheckedBy('isUndefined'), getValueComparedTo('undefined'), getValueWithTypeofUndefinedComparison] } }; - function getLodashTypeChecked(typecheck, node) { - return lodashUtil.isLodashCall(node, settings.pragma) && lodashUtil.isCallToMethod(node, settings.version, typecheck) && node.arguments[0]; + function getLodashTypeCheckedBy(typecheck) { + return function (node) { + return lodashUtil.isLodashCallToMethod(node, settings, typecheck) && node.arguments[0]; + }; } - function getValueComparedToNil(nil, node, operator) { - return node.type === 'BinaryExpression' && node.operator === operator && - ((nilChecks[nil].isValue(node.right) && node.left) || (nilChecks[nil].isValue(node.left) && node.right)); + function getValueComparedTo(nil) { + return function (node, operator) { + return node.type === 'BinaryExpression' && node.operator === operator && + ((nilChecks[nil].isValue(node.right) && node.left) || (nilChecks[nil].isValue(node.left) && node.right)); + }; } - function getTypeofArgument(node) { - return node.type === 'UnaryExpression' && node.operator === 'typeof' && node.argument; - } - function isUndefinedString(node) { - return node.type === 'Literal' && node.value === 'undefined'; - } + var getTypeofArgument = _.cond([ + [_.matches({type: 'UnaryExpression', operator: 'typeof'}), _.property('argument')] + ]); + + var isUndefinedString = _.matches({type: 'Literal', value: 'undefined'}); function getValueWithTypeofUndefinedComparison(node, operator) { return node.type === 'BinaryExpression' && node.operator === operator && @@ -50,12 +50,11 @@ module.exports = function (context) { } function checkExpression(nil, operator, node) { - var valueCompared; - nilChecks[nil].expressionChecks.some(function (check) { - valueCompared = check(node, operator); - return valueCompared; - }); - return valueCompared; + return _(nilChecks[nil].expressionChecks) + .map(function (check) { + return check(node, operator); + }) + .find(); } function checkNegatedExpression(nil, node) {