Skip to content
Permalink
Browse files

Fix: yoda exceptRange false positives on empty string property names (#…

  • Loading branch information
mdjermanovic authored and platinumazure committed Aug 18, 2019
1 parent d642150 commit 19ab6666e8e4142a183bdee2be96e5bafbac0e21
Showing with 69 additions and 1 deletion.
  1. +1 −1 lib/rules/yoda.js
  2. +68 −0 tests/lib/rules/yoda.js
@@ -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)
@@ -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 }]
@@ -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) {}",

0 comments on commit 19ab666

Please sign in to comment.
You can’t perform that action at this time.