Skip to content

Commit

Permalink
Fix: add parens for yield statement (fixes #10432) (#10468)
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane authored and platinumazure committed Jun 15, 2018
1 parent d477c5e commit 05343fd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/rules/no-unneeded-ternary.js
Expand Up @@ -140,7 +140,7 @@ module.exports = {
fix: fixer => {
let nodeAlternate = astUtils.getParenthesisedText(sourceCode, node.alternate);

if (node.alternate.type === "ConditionalExpression") {
if (node.alternate.type === "ConditionalExpression" || node.alternate.type === "YieldExpression") {
const isAlternateParenthesised = astUtils.isParenthesised(sourceCode, node.alternate);

nodeAlternate = isAlternateParenthesised ? nodeAlternate : `(${nodeAlternate})`;
Expand Down
12 changes: 12 additions & 0 deletions tests/lib/rules/no-unneeded-ternary.js
Expand Up @@ -197,6 +197,18 @@ ruleTester.run("no-unneeded-ternary", rule, {
column: 7
}]
},
{
code: "function* fn() { foo ? foo : yield bar }",
output: "function* fn() { foo || (yield bar) }",
options: [{ defaultAssignment: false }],
parserOptions: { ecmaVersion: 6 },
errors: [{
message: "Unnecessary use of conditional expression for default assignment.",
type: "ConditionalExpression",
line: 1,
column: 24
}]
},
{
code: "var a = foo ? foo : 'No';",
output: "var a = foo || 'No';",
Expand Down

0 comments on commit 05343fd

Please sign in to comment.