Skip to content

Commit

Permalink
improve performance for shorthand rules
Browse files Browse the repository at this point in the history
  • Loading branch information
ganimomer committed Nov 8, 2016
1 parent e346159 commit 5eed1ec
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
7 changes: 5 additions & 2 deletions src/rules/identity-shorthand.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ module.exports = {
},

create(context) {
const [get, matches, overSome] = ['get', 'matches', 'overSome'].map(m => require(`lodash/${m}`))
const {methodSupportsShorthand, getShorthandVisitors} = require('../util/lodashUtil')
const get = require('lodash/get')
const matches = require('lodash/matches')
const overSome = require('lodash/overSome')
const {methodSupportsShorthand} = require('../util/methodDataUtil')
const {getShorthandVisitors} = require('../util/lodashUtil')
const {getFirstParamName, getValueReturnedInFirstStatement} = require('../util/astUtil')
const settings = require('../util/settingsUtil').getSettings(context)

Expand Down
13 changes: 1 addition & 12 deletions src/util/lodashUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,6 @@ function getLodashMethodCallExpVisitor(lodashContext, reporter) {
}
}

/**
* Returns whether the node's method call supports using shorthands in the specified version
* @param {Number} version
* @param {object} node
* @returns {boolean}
*/
function methodSupportsShorthand(version, method) {
return _.includes(methodDataUtil.getShorthandMethods(version), method)
}

function isLodashCallToMethod(node, method, lodashContext) {
return lodashContext.isLodashCall(node) && isCallToMethod(node, lodashContext.version, method)
}
Expand All @@ -133,7 +123,7 @@ function getShorthandVisitors(context, checks, messages) {
const visitors = lodashContext.getImportVisitors()
visitors.CallExpression = getLodashMethodCallExpVisitor(lodashContext, {
always(node, iteratee, {method, version}) {
if (methodSupportsShorthand(version, method) && checks.canUseShorthand(iteratee, lodashContext)) {
if (methodDataUtil.methodSupportsShorthand(version, method) && checks.canUseShorthand(iteratee, lodashContext)) {
context.report(iteratee, messages.always)
}
},
Expand Down Expand Up @@ -163,7 +153,6 @@ module.exports = {
getIsTypeMethod,
isNativeCollectionMethodCall,
getLodashMethodCallExpVisitor,
methodSupportsShorthand,
isCallToLodashMethod,
getShorthandVisitors,
getLodashMethodVisitors,
Expand Down
15 changes: 15 additions & 0 deletions src/util/methodDataUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const _ = require('lodash')

const _methodDataByVersion = {}
const _shorthandMethodsByVersion = {}
function getMethodData(version) {
_methodDataByVersion[version] = _methodDataByVersion[version] || require(`./methodDataByVersion/${version}`)
return _methodDataByVersion[version]
Expand Down Expand Up @@ -65,6 +66,19 @@ function getCollectionMethods(version) {
function getShorthandMethods(version) {
return expandAliases(version, getMethodData(version).shorthand)
}
/**
* Returns whether the node's method call supports using shorthands in the specified version
* @param {Number} version
* @param {string} method
* @returns {boolean}
*/
function methodSupportsShorthand(version, method) {
if (!_shorthandMethodsByVersion[version]) {
const methods = getShorthandMethods(version)
_shorthandMethodsByVersion[version] = _.reduce(methods, (obj, key) => _.assign(obj, {[key]: true}), {})
}
return _shorthandMethodsByVersion[version][method]
}

/**
* Gets a list of all wrapper methods for a specific version
Expand Down Expand Up @@ -140,6 +154,7 @@ module.exports = {
getAliasesByVersion,
getChainableAliases,
getShorthandMethods,
methodSupportsShorthand,
getWrapperMethods,
getCollectionMethods,
getMainAlias,
Expand Down

0 comments on commit 5eed1ec

Please sign in to comment.