Skip to content

Commit

Permalink
fix prefer-lodash-method warning on unimplemented string methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ganimomer committed May 1, 2017
1 parent 71c1533 commit 401d28a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 25 deletions.
4 changes: 3 additions & 1 deletion src/rules/prefer-lodash-method.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ module.exports = {

const {getLodashContext, isNativeCollectionMethodCall, getLodashMethodCallExpVisitor} = require('../util/lodashUtil')
const {getMethodName, getCaller} = require('../util/astUtil')
const {methodExists} = require('../util/methodDataUtil')
const keys = require('lodash/keys')
const get = require('lodash/get')
const includes = require('lodash/includes')
Expand Down Expand Up @@ -74,7 +75,8 @@ module.exports = {
}

function isNativeStringMethodCall(node) {
return includes(keys(nativeStringMap), getMethodName(node))
const lodashFunction = nativeStringMap[getMethodName(node)]
return Boolean(lodashFunction) && methodExists(lodashContext.version, lodashFunction)
}

function canUseLodash(node) {
Expand Down
13 changes: 12 additions & 1 deletion src/util/methodDataUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,16 @@ function getSideEffectIterationMethods(version) {
return expandAliases(version, sideEffectIterationMethods)
}

/**
* Returns whether the method exists in the specified version
* @param {number} version
* @param {string} method
* @returns {boolean}
*/
function methodExists(version, method) {
return Boolean(getMethodData(version)[method])
}

module.exports = {
isAliasOfMethod,
isChainable,
Expand All @@ -148,7 +158,8 @@ module.exports = {
getMainAlias,
getIterateeIndex,
getFunctionMaxArity,
getSideEffectIterationMethods
getSideEffectIterationMethods,
methodExists
}

/**
Expand Down
51 changes: 28 additions & 23 deletions tests/lib/rules/prefer-lodash-method.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,36 @@ const ruleTesterUtil = require('../testUtil/ruleTesterUtil')
// ------------------------------------------------------------------------------

const ruleTester = ruleTesterUtil.getRuleTester()
const {withDefaultPragma} = require('../testUtil/optionsUtil')
const {withDefaultPragma, fromVersion3WithDefaultPragma} = require('../testUtil/optionsUtil')
ruleTester.run('prefer-lodash-method', rule, {
valid: [
'var x = _.map(arr, f)',
'var x = _(arr).map(f).reduce(g)',
'var x = _.chain(arr).map(f).reduce(g).map(h).value()',
'var x = _.keys(obj)',
'var x = arr.indexOf(item)',
{
code: 'var x = a.map(f)',
options: [{ignoreMethods: ['map']}]
}, {
code: 'var x = fp.map(f, a)',
options: [{ignoreObjects: ['fp']}]
}, {
code: 'var x = React.Children.map(f)',
options: [{ignoreObjects: ['React.Children']}]
},
{
code: 'var x = $el.filter(f)',
options: [{ignoreObjects: ['^\\$.+']}]
},
'_.chain(a).get(p).map(f).value()',
'var x = Object.create(null)'
].map(withDefaultPragma),
...[
'var x = _.map(arr, f)',
'var x = _(arr).map(f).reduce(g)',
'var x = _.chain(arr).map(f).reduce(g).map(h).value()',
'var x = _.keys(obj)',
'var x = arr.indexOf(item)',
{
code: 'var x = a.map(f)',
options: [{ignoreMethods: ['map']}]
}, {
code: 'var x = fp.map(f, a)',
options: [{ignoreObjects: ['fp']}]
}, {
code: 'var x = React.Children.map(f)',
options: [{ignoreObjects: ['React.Children']}]
},
{
code: 'var x = $el.filter(f)',
options: [{ignoreObjects: ['^\\$.+']}]
},
'_.chain(a).get(p).map(f).value()',
'var x = Object.create(null)'
].map(withDefaultPragma),
...[
'var x = str.replace(something, withSomething)'
].map(fromVersion3WithDefaultPragma)
],
invalid: [{
code: 'var x = a.map(function(x) {return x.f()});',
errors: [{message: 'Prefer \'_.map\' over the native function.'}]
Expand Down

0 comments on commit 401d28a

Please sign in to comment.