Skip to content

Commit

Permalink
refactor parts of prefer-wrapper-method into lodashUtil and aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
ganimomer committed Sep 9, 2015
1 parent bc25cad commit a91efe1
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
7 changes: 1 addition & 6 deletions lib/rules/prefer-wrapper-method.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,10 @@

module.exports = function (context) {
var lodashUtil = require('../util/lodashUtil');
var _ = require('lodash');
var WRAPPER_METHODS = ['concat', 'join', 'pop', 'push', 'reverse', 'shift', 'slice', 'sort', 'splice', 'unshift', 'replace', 'split'];
function isCallToWrapperMethod(node) {
return node && node.type === 'CallExpression' && _.includes(WRAPPER_METHODS, _.get(node, 'callee.property.name'));
}

return {
CallExpression: function (node) {
if (lodashUtil.isLodashChainStart(node) && isCallToWrapperMethod(node.arguments[0])) {
if (lodashUtil.isLodashChainStart(node) && lodashUtil.isLodashWrapperMethod(node.arguments[0])) {
context.report(node, 'Prefer {{name}} with wrapper method over inside the chain start.', {name: node.arguments[0].callee.property.name});
}
}
Expand Down
4 changes: 3 additions & 1 deletion lib/util/aliases.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ function expandAliases(methods) {
var CHAINABLE_ALIASES = expandAliases(CHAINABLE);

var supportsProp = expandAliases(property);
var WRAPPER_METHODS = ['concat', 'join', 'pop', 'push', 'reverse', 'shift', 'slice', 'sort', 'splice', 'unshift', 'replace', 'split'];

function isAliasOfMethod(method, suspect) {
return _.includes(expandAlias(method), suspect);
Expand All @@ -62,5 +63,6 @@ module.exports = {
isAliasOfMethod: isAliasOfMethod,
ALIASES: ALIASES,
CHAINABLE_ALIASES: CHAINABLE_ALIASES,
supportsProp: supportsProp
supportsProp: supportsProp,
WRAPPER_METHODS: WRAPPER_METHODS
};
7 changes: 6 additions & 1 deletion lib/util/lodashUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ function isCallToMethod(node, method) {
return aliasMap.isAliasOfMethod(method, astUtil.getMethodName(node));
}

function isLodashWrapperMethod(node) {
return _.includes(aliasMap.WRAPPER_METHODS, astUtil.getMethodName(node)) && node.type === 'CallExpression';
}

module.exports = {
isLodashCall: isLodashCall,
isLodashChainStart: isLodashChainStart,
Expand All @@ -60,5 +64,6 @@ module.exports = {
isEndOfChain: isEndOfChain,
isChainBreaker: isChainBreaker,
isExplicitMethodChaining: isExplicitMethodChaining,
isCallToMethod: isCallToMethod
isCallToMethod: isCallToMethod,
isLodashWrapperMethod: isLodashWrapperMethod
};

0 comments on commit a91efe1

Please sign in to comment.