Skip to content

Commit

Permalink
prevent prefer-get from warning on computed expressions with variables
Browse files Browse the repository at this point in the history
  • Loading branch information
ganimomer committed Oct 8, 2015
1 parent 67f86c1 commit 2c2ea5c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
7 changes: 7 additions & 0 deletions lib/util/astUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,19 @@ function isCallFromObject(node, objName) {
return node && node.type === 'CallExpression' && _.get(node, 'callee.object.name') === objName;
}

function isComputed(node) {
return _.get(node, 'computed') && node.property.type !== 'Literal';
}

function isEquivalentExp(a, b) {

return _.isEqual(a, b, function(left, right, key) {
if (_.includes(['loc', 'range', 'computed'], key)) {
return true;
}
if (isComputed(left) || isComputed(right)) {
return false;
}
if (key === 'property') {
var leftValue = left.name || left.value;
var rightValue = right.name || right.value;
Expand Down
3 changes: 2 additions & 1 deletion tests/lib/rules/prefer-get.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ ruleTester.run('prefer-get', rule, {
'var x = _.get(a, "b.c");',
'var x = _.has(a, "b.c");',
'var x = a && a.b',
'a && a.b && f()'
'a && a.b && f()',
'a && a[v] && a[v].c'
],
invalid: [{
code: 'x = a && a.b && a.b.c === 8',
Expand Down

0 comments on commit 2c2ea5c

Please sign in to comment.