diff --git a/lib/rules/no-unneeded-ternary.js b/lib/rules/no-unneeded-ternary.js index 7f82c8ee281..b56ad038a49 100644 --- a/lib/rules/no-unneeded-ternary.js +++ b/lib/rules/no-unneeded-ternary.js @@ -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})`; diff --git a/tests/lib/rules/no-unneeded-ternary.js b/tests/lib/rules/no-unneeded-ternary.js index 05251ccbda6..badf529e66d 100644 --- a/tests/lib/rules/no-unneeded-ternary.js +++ b/tests/lib/rules/no-unneeded-ternary.js @@ -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';",