Skip to content

Commit

Permalink
Fix: yoda rule produces invalid autofix with preceding yield (#12166)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjermanovic authored and kaicataldo committed Aug 30, 2019
1 parent febb660 commit 4009d39
Show file tree
Hide file tree
Showing 2 changed files with 180 additions and 2 deletions.
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"
}
]
}
]
});

0 comments on commit 4009d39

Please sign in to comment.