Skip to content

Commit

Permalink
refactor prefer-is-nil
Browse files Browse the repository at this point in the history
  • Loading branch information
ganimomer committed Feb 25, 2016
1 parent 8417432 commit 00e35b0
Showing 1 changed file with 24 additions and 25 deletions.
49 changes: 24 additions & 25 deletions lib/rules/prefer-is-nil.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand All @@ -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) {
Expand Down

0 comments on commit 00e35b0

Please sign in to comment.