Skip to content

Commit

Permalink
refactor with methods in astUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
ganimomer committed Aug 24, 2015
1 parent ea14068 commit 919cb2d
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 18 deletions.
8 changes: 1 addition & 7 deletions lib/rules/matches-prop-shorthand.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,9 @@ module.exports = function (context) {

var SUPPORT_MATCHES = ['find', 'detect', 'filter', 'select', 'reject'];

function isParamEqEqEq(exp, paramName) {
return exp.type === 'BinaryExpression' && exp.operator === '===' &&
(astUtil.isMemberExpOfArg(exp.left, paramName) ||
astUtil.isMemberExpOfArg(exp.right, paramName));
}

function shouldPreferMatches(func) {
var firstLine = astUtil.getFirstFunctionLine(func);
return astUtil.isReturnStatement(firstLine) && isParamEqEqEq(firstLine.argument, astUtil.getFirstParamName(func));
return astUtil.isReturnStatement(firstLine) && astUtil.isEqEqEqToParamMember(firstLine.argument, astUtil.getFirstParamName(func));
}

function methodSupportsShorthand(node) {
Expand Down
13 changes: 2 additions & 11 deletions lib/rules/prefer-reject.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,12 @@ module.exports = function (context) {
return aliases.isAliasOfMethod('filter', astUtil.getMethodName(node));
}

function isParamNegation(exp, firstParamName) {
return exp && exp.type === 'UnaryExpression' && exp.operator === '!' && astUtil.isMemberExpOfArg(exp.argument, firstParamName);
}

function isParamNotEqEq(exp, firstParamName) {
return exp && exp.type === 'BinaryExpression' && exp.operator === '!==' &&
(astUtil.isMemberExpOfArg(exp.left, firstParamName) || astUtil.isMemberExpOfArg(exp.right, firstParamName));
}

function isNegativeExpressionFunction(func) {
var firstLine = astUtil.getFirstFunctionLine(func);
var firstParamName = astUtil.getFirstParamName(func);
return astUtil.isReturnStatement(firstLine) &&
(isParamNegation(firstLine.argument, firstParamName) ||
isParamNotEqEq(firstLine.argument, firstParamName));
(astUtil.isNegationOfParamMember(firstLine.argument, firstParamName) ||
astUtil.isNotEqEqToParamMember(firstLine.argument, firstParamName));
}

return {
Expand Down

0 comments on commit 919cb2d

Please sign in to comment.