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 rule produces invalid autofix with preceding yield #12166

Merged
merged 1 commit into from Aug 30, 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
13 changes: 11 additions & 2 deletions lib/rules/yoda.js
Expand Up @@ -274,13 +274,22 @@ module.exports = {
* @returns {string} A string representation of the node with the sides and operator flipped
*/
function getFlippedString(node) {
const tokenBefore = sourceCode.getTokenBefore(node);
const operatorToken = sourceCode.getFirstTokenBetween(node.left, node.right, token => token.value === node.operator);
const textBeforeOperator = sourceCode.getText().slice(sourceCode.getTokenBefore(operatorToken).range[1], operatorToken.range[0]);
const textAfterOperator = sourceCode.getText().slice(operatorToken.range[1], sourceCode.getTokenAfter(operatorToken).range[0]);
const leftText = sourceCode.getText().slice(node.range[0], sourceCode.getTokenBefore(operatorToken).range[1]);
const rightText = sourceCode.getText().slice(sourceCode.getTokenAfter(operatorToken).range[0], node.range[1]);
const firstRightToken = sourceCode.getTokenAfter(operatorToken);
const rightText = sourceCode.getText().slice(firstRightToken.range[0], node.range[1]);

return rightText + textBeforeOperator + OPERATOR_FLIP_MAP[operatorToken.value] + textAfterOperator + leftText;
let prefix = "";

if (tokenBefore && tokenBefore.range[1] === node.range[0] &&
!astUtils.canTokensBeAdjacent(tokenBefore, firstRightToken)) {
prefix = " ";
}

return prefix + rightText + textBeforeOperator + OPERATOR_FLIP_MAP[operatorToken.value] + textAfterOperator + leftText;
}

//--------------------------------------------------------------------------
Expand Down
169 changes: 169 additions & 0 deletions tests/lib/rules/yoda.js
Expand Up @@ -568,6 +568,175 @@ ruleTester.run("yoda", rule, {
type: "BinaryExpression"
}
]
},

// Adjacent tokens tests
{
code: "function *foo() { yield(1) < a }",
output: "function *foo() { yield a > (1) }",
options: ["never"],
parserOptions: { ecmaVersion: 2015 },
errors: [
{
messageId: "expected",
data: { expectedSide: "right", operator: "<" },
type: "BinaryExpression"
}
]
},
{
code: "function *foo() { yield((1)) < a }",
output: "function *foo() { yield a > ((1)) }",
options: ["never"],
parserOptions: { ecmaVersion: 2015 },
errors: [
{
messageId: "expected",
data: { expectedSide: "right", operator: "<" },
type: "BinaryExpression"
}
]
},
{
code: "function *foo() { yield 1 < a }",
output: "function *foo() { yield a > 1 }",
options: ["never"],
parserOptions: { ecmaVersion: 2015 },
errors: [
{
messageId: "expected",
data: { expectedSide: "right", operator: "<" },
type: "BinaryExpression"
}
]
},
{
code: "function *foo() { yield/**/1 < a }",
output: "function *foo() { yield/**/a > 1 }",
options: ["never"],
parserOptions: { ecmaVersion: 2015 },
errors: [
{
messageId: "expected",
data: { expectedSide: "right", operator: "<" },
type: "BinaryExpression"
}
]
},
{
code: "function *foo() { yield(1) < ++a }",
output: "function *foo() { yield++a > (1) }",
options: ["never"],
parserOptions: { ecmaVersion: 2015 },
errors: [
{
messageId: "expected",
data: { expectedSide: "right", operator: "<" },
type: "BinaryExpression"
}
]
},
{
code: "function *foo() { yield(1) < (a) }",
output: "function *foo() { yield(a) > (1) }",
options: ["never"],
parserOptions: { ecmaVersion: 2015 },
errors: [
{
messageId: "expected",
data: { expectedSide: "right", operator: "<" },
type: "BinaryExpression"
}
]
},
{
code: "x=1 < a",
output: "x=a > 1",
options: ["never"],
errors: [
{
messageId: "expected",
data: { expectedSide: "right", operator: "<" },
type: "BinaryExpression"
}
]
},
{
code: "function *foo() { yield++a < 1 }",
output: "function *foo() { yield 1 > ++a }",
options: ["always"],
parserOptions: { ecmaVersion: 2015 },
errors: [
{
messageId: "expected",
data: { expectedSide: "left", operator: "<" },
type: "BinaryExpression"
}
]
},
{
code: "function *foo() { yield(a) < 1 }",
output: "function *foo() { yield 1 > (a) }",
options: ["always"],
parserOptions: { ecmaVersion: 2015 },
errors: [
{
messageId: "expected",
data: { expectedSide: "left", operator: "<" },
type: "BinaryExpression"
}
]
},
{
code: "function *foo() { yield a < 1 }",
output: "function *foo() { yield 1 > a }",
options: ["always"],
parserOptions: { ecmaVersion: 2015 },
errors: [
{
messageId: "expected",
data: { expectedSide: "left", operator: "<" },
type: "BinaryExpression"
}
]
},
{
code: "function *foo() { yield/**/a < 1 }",
output: "function *foo() { yield/**/1 > a }",
options: ["always"],
parserOptions: { ecmaVersion: 2015 },
errors: [
{
messageId: "expected",
data: { expectedSide: "left", operator: "<" },
type: "BinaryExpression"
}
]
},
{
code: "function *foo() { yield++a < (1) }",
output: "function *foo() { yield(1) > ++a }",
options: ["always"],
parserOptions: { ecmaVersion: 2015 },
errors: [
{
messageId: "expected",
data: { expectedSide: "left", operator: "<" },
type: "BinaryExpression"
}
]
},
{
code: "x=a < 1",
output: "x=1 > a",
options: ["always"],
errors: [
{
messageId: "expected",
data: { expectedSide: "left", operator: "<" },
type: "BinaryExpression"
}
]
}
]
});