Skip to content

Commit

Permalink
Allow ignoring object names via regex pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
lavelle-tt authored and ganimomer committed Jul 20, 2016
1 parent 02b120b commit 1c01a5f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/rules/prefer-lodash-method.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
//------------------------------------------------------------------------------

module.exports = function (context) {

const {isLodashCall, isLodashWrapper, isNativeCollectionMethodCall} = require('../util/lodashUtil')
const {getMethodName, getCaller} = require('../util/astUtil')
const settings = require('../util/settingsUtil').getSettings(context)
const [get, includes, cond, matches, property] = ['get', 'includes', 'cond', 'matches', 'property'].map(m => require(`lodash/${m}`))
const [get, includes, cond, matches, property, some, map] = ['get', 'includes', 'cond', 'matches', 'property', 'some', 'map'].map(m => require(`lodash/${m}`))
const REPORT_MESSAGE = 'Prefer \'_.{{method}}\' over the native function.'
const exceptions = get(context, ['options', 0, 'except'], [])
const ignoredObjects = get(context, ['options', 0, 'ignoreObjects'], [])
const ignoredPatterns = map(get(context, ['options', 0, 'ignorePatterns'], []), pattern => new RegExp(pattern))

function isStaticNativeMethodCall(node) {
const staticMethods = {
Expand All @@ -41,7 +42,13 @@ module.exports = function (context) {

function isIgnoredObject(node) {
const callerName = getTextOfNode(getCaller(node))
return callerName && includes(ignoredObjects, callerName)
if (!callerName) { return false }

if (includes(ignoredObjects, callerName)) { return true }

if (some(ignoredPatterns, pattern => callerName.match(pattern))) { return true }

return false
}

function isRuleException(node) {
Expand Down Expand Up @@ -75,4 +82,4 @@ module.exports.schema = [
}
}
}
]
]
4 changes: 4 additions & 0 deletions tests/lib/rules/prefer-lodash-method.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ ruleTester.run('prefer-lodash-method', rule, {
code: 'var x = React.Children.map(f)',
options: [{ignoreObjects: ['React.Children']}]
},
{
code: 'var x = $el.filter(f)',
options: [{ignorePatterns: ['^\\$.+']}]
},
'_.chain(a).get(p).map(f).value()'
],
invalid: [{
Expand Down

0 comments on commit 1c01a5f

Please sign in to comment.