Skip to content
Permalink
Browse files

Fix: autofix of no-unneeded-ternary made syntax error (fixes #11579) (#…

  • Loading branch information...
mysticatea committed Apr 19, 2019
1 parent bebd079 commit 1dfe077b7e47c6090277eb984e08bd472bb5595e
Showing with 95 additions and 9 deletions.
  1. +11 −9 lib/rules/no-unneeded-ternary.js
  2. +84 −0 tests/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}`);
}
});
}
@@ -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
}]
}
]
});

0 comments on commit 1dfe077

Please sign in to comment.
You can’t perform that action at this time.