diff --git a/lib/rules/yoda.js b/lib/rules/yoda.js index 89c4a8afd11..b00acf82c70 100644 --- a/lib/rules/yoda.js +++ b/lib/rules/yoda.js @@ -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; } //-------------------------------------------------------------------------- diff --git a/tests/lib/rules/yoda.js b/tests/lib/rules/yoda.js index d2c18347740..7908499d88f 100644 --- a/tests/lib/rules/yoda.js +++ b/tests/lib/rules/yoda.js @@ -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" + } + ] } ] });