Skip to content

Commit

Permalink
Fix: autofix of no-unneeded-ternary made syntax error (fixes #11579) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea committed Apr 19, 2019
1 parent bebd079 commit 1dfe077
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 9 deletions.
20 changes: 11 additions & 9 deletions lib/rules/no-unneeded-ternary.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}`);
}
});
}
Expand Down
84 changes: 84 additions & 0 deletions tests/lib/rules/no-unneeded-ternary.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.