Skip to content

Commit

Permalink
Fix: yoda exceptRange false positives on empty string property names (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjermanovic authored and platinumazure committed Aug 18, 2019
1 parent d642150 commit 19ab666
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/rules/yoda.js
Expand Up @@ -119,7 +119,7 @@ function same(a, b) {
const nameA = astUtils.getStaticPropertyName(a);

// x.y = x["y"]
if (nameA) {
if (nameA !== null) {
return (
same(a.object, b.object) &&
nameA === astUtils.getStaticPropertyName(b)
Expand Down
68 changes: 68 additions & 0 deletions tests/lib/rules/yoda.js
Expand Up @@ -50,6 +50,14 @@ ruleTester.run("yoda", rule, {
}, {
code: "if ('blue' < x.y && x.y < 'green') {}",
options: ["never", { exceptRange: true }]
}, {
code: "if (0 < x[``] && x[``] < 100) {}",
options: ["never", { exceptRange: true }],
parserOptions: { ecmaVersion: 2015 }
}, {
code: "if (0 < x[''] && x[``] < 100) {}",
options: ["never", { exceptRange: true }],
parserOptions: { ecmaVersion: 2015 }
}, {
code: "if (0 <= x['y'] && x['y'] <= 100) {}",
options: ["never", { exceptRange: true }]
Expand Down Expand Up @@ -332,6 +340,66 @@ ruleTester.run("yoda", rule, {
}
]
},
{
code: "if (0 <= a[''] && a.b < 1) {}",
output: "if (a[''] >= 0 && a.b < 1) {}",
options: ["never", { exceptRange: true }],
errors: [
{
messageId: "expected",
data: { expectedSide: "right", operator: "<=" },
type: "BinaryExpression"
}
]
},
{
code: "if (0 <= a[''] && a[' '] < 1) {}",
output: "if (a[''] >= 0 && a[' '] < 1) {}",
options: ["never", { exceptRange: true }],
errors: [
{
messageId: "expected",
data: { expectedSide: "right", operator: "<=" },
type: "BinaryExpression"
}
]
},
{
code: "if (0 <= a[''] && a[null] < 1) {}",
output: "if (a[''] >= 0 && a[null] < 1) {}",
options: ["never", { exceptRange: true }],
errors: [
{
messageId: "expected",
data: { expectedSide: "right", operator: "<=" },
type: "BinaryExpression"
}
]
},
{
code: "if (0 <= a[''] && a[b] < 1) {}",
output: "if (a[''] >= 0 && a[b] < 1) {}",
options: ["never", { exceptRange: true }],
errors: [
{
messageId: "expected",
data: { expectedSide: "right", operator: "<=" },
type: "BinaryExpression"
}
]
},
{
code: "if (0 <= a[''] && a[b()] < 1) {}",
output: "if (a[''] >= 0 && a[b()] < 1) {}",
options: ["never", { exceptRange: true }],
errors: [
{
messageId: "expected",
data: { expectedSide: "right", operator: "<=" },
type: "BinaryExpression"
}
]
},
{
code: "if (0 <= a[b()] && a[b()] < 1) {}",
output: "if (a[b()] >= 0 && a[b()] < 1) {}",
Expand Down

0 comments on commit 19ab666

Please sign in to comment.