diff --git a/docs/rules/no-unneeded-ternary.md b/docs/rules/no-unneeded-ternary.md index b4caddf5855..e5d4e80112d 100644 --- a/docs/rules/no-unneeded-ternary.md +++ b/docs/rules/no-unneeded-ternary.md @@ -41,8 +41,6 @@ Examples of **incorrect** code for this rule: var a = x === 2 ? true : false; var a = x ? true : false; - -var a = f(x ? x : 1); ``` Examples of **correct** code for this rule: @@ -58,7 +56,7 @@ var a = x ? "Yes" : "No"; var a = x ? y : x; -var a = x ? x : 1; // Note that this is only allowed as it on the right hand side of an assignment; this type of ternary is disallowed everywhere else. See defaultAssignment option below for more details. +f(x ? x : 1); // default assignment - would be disallowed if defaultAssignment option set to false. See option details below. ``` ## Options @@ -70,7 +68,7 @@ This rule has an object option: ### defaultAssignment -The defaultAssignment option allows expressions of the form `x ? x : expr` (where `x` is any identifier and `expr` is any expression) as the right hand side of assignments (but nowhere else). +When set to `true`, which it is by default, The defaultAssignment option allows expressions of the form `x ? x : expr` (where `x` is any identifier and `expr` is any expression). Examples of additional **incorrect** code for this rule with the `{ "defaultAssignment": false }` option: @@ -78,8 +76,12 @@ Examples of additional **incorrect** code for this rule with the `{ "defaultAssi /*eslint no-unneeded-ternary: ["error", { "defaultAssignment": false }]*/ var a = x ? x : 1; + +f(x ? x : 1); ``` +Note that `defaultAssignment: false` still allows expressions of the form `x ? expr : x` (where the identifier is on the right hand side of the ternary). + ## When Not To Use It You can turn this rule off if you are not concerned with unnecessary complexity in conditional expressions. diff --git a/tests/lib/rules/no-unneeded-ternary.js b/tests/lib/rules/no-unneeded-ternary.js index 243526c2243..a297b211ba7 100644 --- a/tests/lib/rules/no-unneeded-ternary.js +++ b/tests/lib/rules/no-unneeded-ternary.js @@ -27,6 +27,12 @@ ruleTester.run("no-unneeded-ternary", rule, { "var a = x === 2 ? 'true' : 'false';", "var a = foo ? foo : bar;", "var value = 'a';var canSet = true;var result = value || (canSet ? 'unset' : 'can not set')", + "var a = foo ? bar : foo;", + "foo ? bar : foo;", + "var a = f(x ? x : 1)", + "f(x ? x : 1);", + "foo ? foo : bar;", + "var a = foo ? 'Yes' : foo;", { code: "var a = foo ? 'Yes' : foo;", options: [{ defaultAssignment: false }] @@ -34,6 +40,10 @@ ruleTester.run("no-unneeded-ternary", rule, { { code: "var a = foo ? bar : foo;", options: [{ defaultAssignment: false }] + }, + { + code: "foo ? bar : foo;", + options: [{ defaultAssignment: false }] } ], invalid: [ @@ -314,6 +324,39 @@ ruleTester.run("no-unneeded-ternary", rule, { line: 1, column: 13 }] + }, + { + code: "f(x ? x : 1);", + output: "f(x || 1);", + options: [{ defaultAssignment: false }], + errors: [{ + message: "Unnecessary use of conditional expression for default assignment.", + type: "ConditionalExpression", + line: 1, + column: 7 + }] + }, + { + code: "x ? x : 1;", + output: "x || 1;", + options: [{ defaultAssignment: false }], + errors: [{ + message: "Unnecessary use of conditional expression for default assignment.", + type: "ConditionalExpression", + line: 1, + column: 5 + }] + }, + { + code: "var a = foo ? foo : bar;", + output: "var a = foo || bar;", + options: [{ defaultAssignment: false }], + errors: [{ + message: "Unnecessary use of conditional expression for default assignment.", + type: "ConditionalExpression", + line: 1, + column: 15 + }] } ] });