Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Aug 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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