From 1dfe077b7e47c6090277eb984e08bd472bb5595e Mon Sep 17 00:00:00 2001 From: Toru Nagashima Date: Fri, 19 Apr 2019 15:50:23 +0900 Subject: [PATCH] Fix: autofix of no-unneeded-ternary made syntax error (fixes #11579) (#11616) --- lib/rules/no-unneeded-ternary.js | 20 +++--- tests/lib/rules/no-unneeded-ternary.js | 84 ++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 9 deletions(-) diff --git a/lib/rules/no-unneeded-ternary.js b/lib/rules/no-unneeded-ternary.js index e75a36430fe..4dbb78a3e07 100644 --- a/lib/rules/no-unneeded-ternary.js +++ b/lib/rules/no-unneeded-ternary.js @@ -17,6 +17,7 @@ const OPERATOR_INVERSES = { // Operators like < and >= are not true inverses, since both will return false with NaN. }; +const OR_PRECEDENCE = astUtils.getPrecedence({ type: "LogicalExpression", operator: "||" }); //------------------------------------------------------------------------------ // Rule Definition @@ -141,15 +142,16 @@ module.exports = { loc: node.consequent.loc.start, message: "Unnecessary use of conditional expression for default assignment.", fix: fixer => { - let nodeAlternate = astUtils.getParenthesisedText(sourceCode, node.alternate); - - if (node.alternate.type === "ConditionalExpression" || node.alternate.type === "YieldExpression") { - const isAlternateParenthesised = astUtils.isParenthesised(sourceCode, node.alternate); - - nodeAlternate = isAlternateParenthesised ? nodeAlternate : `(${nodeAlternate})`; - } - - return fixer.replaceText(node, `${astUtils.getParenthesisedText(sourceCode, node.test)} || ${nodeAlternate}`); + const shouldParenthesizeAlternate = ( + astUtils.getPrecedence(node.alternate) < OR_PRECEDENCE && + !astUtils.isParenthesised(sourceCode, node.alternate) + ); + const alternateText = shouldParenthesizeAlternate + ? `(${sourceCode.getText(node.alternate)})` + : astUtils.getParenthesisedText(sourceCode, node.alternate); + const testText = astUtils.getParenthesisedText(sourceCode, node.test); + + return fixer.replaceText(node, `${testText} || ${alternateText}`); } }); } diff --git a/tests/lib/rules/no-unneeded-ternary.js b/tests/lib/rules/no-unneeded-ternary.js index badf529e66d..5196b576bd0 100644 --- a/tests/lib/rules/no-unneeded-ternary.js +++ b/tests/lib/rules/no-unneeded-ternary.js @@ -230,6 +230,90 @@ ruleTester.run("no-unneeded-ternary", rule, { line: 1, column: 24 }] + }, + { + code: "var a = b ? b : c => c;", + output: "var a = b || (c => c);", + options: [{ defaultAssignment: false }], + parserOptions: { ecmaVersion: 2015 }, + errors: [{ + message: "Unnecessary use of conditional expression for default assignment.", + type: "ConditionalExpression", + line: 1, + column: 13 + }] + }, + { + code: "var a = b ? b : c = 0;", + output: "var a = b || (c = 0);", + options: [{ defaultAssignment: false }], + parserOptions: { ecmaVersion: 2015 }, + errors: [{ + message: "Unnecessary use of conditional expression for default assignment.", + type: "ConditionalExpression", + line: 1, + column: 13 + }] + }, + { + code: "var a = b ? b : (c => c);", + output: "var a = b || (c => c);", + options: [{ defaultAssignment: false }], + parserOptions: { ecmaVersion: 2015 }, + errors: [{ + message: "Unnecessary use of conditional expression for default assignment.", + type: "ConditionalExpression", + line: 1, + column: 13 + }] + }, + { + code: "var a = b ? b : (c = 0);", + output: "var a = b || (c = 0);", + options: [{ defaultAssignment: false }], + parserOptions: { ecmaVersion: 2015 }, + errors: [{ + message: "Unnecessary use of conditional expression for default assignment.", + type: "ConditionalExpression", + line: 1, + column: 13 + }] + }, + { + code: "var a = b ? b : (c) => (c);", + output: "var a = b || ((c) => (c));", + options: [{ defaultAssignment: false }], + parserOptions: { ecmaVersion: 2015 }, + errors: [{ + message: "Unnecessary use of conditional expression for default assignment.", + type: "ConditionalExpression", + line: 1, + column: 13 + }] + }, + { + code: "var a = b ? b : c, d; // this is ((b ? b : c), (d))", + output: "var a = b || c, d; // this is ((b ? b : c), (d))", + options: [{ defaultAssignment: false }], + parserOptions: { ecmaVersion: 2015 }, + errors: [{ + message: "Unnecessary use of conditional expression for default assignment.", + type: "ConditionalExpression", + line: 1, + column: 13 + }] + }, + { + code: "var a = b ? b : (c, d);", + output: "var a = b || (c, d);", + options: [{ defaultAssignment: false }], + parserOptions: { ecmaVersion: 2015 }, + errors: [{ + message: "Unnecessary use of conditional expression for default assignment.", + type: "ConditionalExpression", + line: 1, + column: 13 + }] } ] });