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 left string fix for exceptRange (fixes #12883) #13052

Merged
merged 13 commits into from Apr 24, 2020
Merged
4 changes: 2 additions & 2 deletions lib/rules/yoda.js
Expand Up @@ -248,7 +248,7 @@ module.exports = {
return (node.operator === "&&" &&
(leftLiteral = getNormalizedLiteral(left.left)) &&
(rightLiteral = getNormalizedLiteral(right.right, Number.POSITIVE_INFINITY)) &&
leftLiteral.value <= rightLiteral.value &&
(typeof leftLiteral.value === "string" || leftLiteral.value <= rightLiteral.value) &&
anikethsaha marked this conversation as resolved.
Show resolved Hide resolved
same(left.right, right.left));
}

Expand All @@ -262,7 +262,7 @@ module.exports = {
return (node.operator === "||" &&
(leftLiteral = getNormalizedLiteral(left.right, Number.NEGATIVE_INFINITY)) &&
(rightLiteral = getNormalizedLiteral(right.left)) &&
leftLiteral.value <= rightLiteral.value &&
(typeof leftLiteral.value === "string" || leftLiteral.value <= rightLiteral.value) &&
same(left.left, right.right));
}

Expand Down
31 changes: 18 additions & 13 deletions tests/lib/rules/yoda.js
Expand Up @@ -49,6 +49,24 @@ ruleTester.run("yoda", rule, {
{ code: "if (`b` > `a` && \"b\" > \"a\") {}", options: ["always"], parserOptions: { ecmaVersion: 2015 } },

// Range exception
{
code: "if (\"a\" < x && x < MAX ) {}",
options: ["never", { exceptRange: true }]
},
{
code: "if (1 < x && x < MAX ) {}",
options: ["never", { exceptRange: true }]
},
{
code: "if (x < `x` || `x` <= x) {}",
options: ["never", { exceptRange: true }],
parserOptions: { ecmaVersion: 2015 }
},
{
code: "if (`green` < x.y && x.y < `blue`) {}",
options: ["never", { exceptRange: true }],
parserOptions: { ecmaVersion: 2015 }
},
{
code: "if (0 < x && x <= 1) {}",
options: ["never", { exceptRange: true }]
Expand Down Expand Up @@ -641,19 +659,6 @@ ruleTester.run("yoda", rule, {
}
]
},
{
code: "if (`green` < x.y && x.y < `blue`) {}",
output: "if (x.y > `green` && x.y < `blue`) {}",
options: ["never", { exceptRange: true }],
anikethsaha marked this conversation as resolved.
Show resolved Hide resolved
parserOptions: { ecmaVersion: 2015 },
errors: [
{
messageId: "expected",
data: { expectedSide: "right", operator: "<" },
type: "BinaryExpression"
}
]
},
{
code: "if (3 == a) {}",
output: "if (a == 3) {}",
Expand Down