Skip to content

Commit

Permalink
fix prefer-lodash-typecheck to warn on undefined checks of declared v…
Browse files Browse the repository at this point in the history
…ariables (fixes #21)
  • Loading branch information
ganimomer committed Oct 9, 2016
1 parent ef228f0 commit 8ee071a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/rules/prefer-lodash-typecheck.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

module.exports = {
create(context) {
const some = require('lodash/some')
const {getIsTypeMethod} = require('../util/lodashUtil')

const otherSides = {
Expand All @@ -27,9 +28,14 @@ module.exports = {
return node.operator === '===' || node.operator === '!=='
}

function isDeclaredVariable(node) {
const definedVariables = context.getScope().variables
return some(definedVariables, {name: node.name})
}

function getValueForSide(node, side) {
const otherSide = otherSides[side]
if (isTypeOf(node[side]) && (node[otherSide].value !== 'undefined' || node[side].argument.type !== 'Identifier')) {
if (isTypeOf(node[side]) && (node[otherSide].value !== 'undefined' || node[side].argument.type !== 'Identifier' || isDeclaredVariable(node[side].argument))) {
return node[otherSide].value
}
}
Expand Down
3 changes: 3 additions & 0 deletions tests/lib/rules/prefer-lodash-typecheck.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,8 @@ ruleTester.run('prefer-lodash-typecheck', rule, {
}, {
code: 'var x = typeof a.b === "undefined"',
errors: errors.undefined
}, {
code: 'var y; var x = typeof y === "undefined"',
errors: errors.undefined
}]
})

0 comments on commit 8ee071a

Please sign in to comment.