2 * @fileoverview Rule to flag no-unneeded-ternary
3 * @author Gyandeep Singh
8 const astUtils = require("./utils/ast-utils");
10 // Operators that always result in a boolean value
11 const BOOLEAN_OPERATORS = new Set(["==", "===", "!=", "!==", ">", ">=", "<", "<=", "in", "instanceof"]);
12 const OPERATOR_INVERSES = {
18 // Operators like < and >= are not true inverses, since both will return false with NaN.
20 const OR_PRECEDENCE = astUtils.getPrecedence({ type: "LogicalExpression", operator: "||" });
22 //------------------------------------------------------------------------------
24 //------------------------------------------------------------------------------
31 description: "disallow ternary operators when simpler alternatives exist",
32 category: "Stylistic Issues",
34 url: "https://eslint.org/docs/rules/no-unneeded-ternary"
46 additionalProperties: false
53 unnecessaryConditionalExpression: "Unnecessary use of boolean literals in conditional expression.",
54 unnecessaryConditionalAssignment: "Unnecessary use of conditional expression for default assignment."
59 const options = context.options[0] || {};
60 const defaultAssignment = options.defaultAssignment !== false;
61 const sourceCode = context.getSourceCode();
64 * Test if the node is a boolean literal
65 * @param {ASTNode} node The node to report.
66 * @returns {boolean} True if the its a boolean literal
69 function isBooleanLiteral(node) {
70 return node.type === "Literal" && typeof node.value === "boolean";
74 * Creates an expression that represents the boolean inverse of the expression represented by the original node
75 * @param {ASTNode} node A node representing an expression
76 * @returns {string} A string representing an inverted expression
78 function invertExpression(node) {
79 if (node.type === "BinaryExpression" && Object.prototype.hasOwnProperty.call(OPERATOR_INVERSES, node.operator)) {
80 const operatorToken = sourceCode.getFirstTokenBetween(
83 token => token.value === node.operator
85 const text = sourceCode.getText();
87 return text.slice(node.range[0],
88 operatorToken.range[0]) + OPERATOR_INVERSES[node.operator] + text.slice(operatorToken.range[1], node.range[1]);
91 if (astUtils.getPrecedence(node) < astUtils.getPrecedence({ type: "UnaryExpression" })) {
92 return `!(${astUtils.getParenthesisedText(sourceCode, node)})`;
94 return `!${astUtils.getParenthesisedText(sourceCode, node)}`;
98 * Tests if a given node always evaluates to a boolean value
99 * @param {ASTNode} node An expression node
100 * @returns {boolean} True if it is determined that the node will always evaluate to a boolean value
102 function isBooleanExpression(node) {
103 return node.type === "BinaryExpression" && BOOLEAN_OPERATORS.has(node.operator) ||
104 node.type === "UnaryExpression" && node.operator === "!";
108 * Test if the node matches the pattern id ? id : expression
109 * @param {ASTNode} node The ConditionalExpression to check.
110 * @returns {boolean} True if the pattern is matched, and false otherwise
113 function matchesDefaultAssignment(node) {
114 return node.test.type === "Identifier" &&
115 node.consequent.type === "Identifier" &&
116 node.test.name === node.consequent.name;
121 ConditionalExpression(node) {
122 if (isBooleanLiteral(node.alternate) && isBooleanLiteral(node.consequent)) {
125 messageId: "unnecessaryConditionalExpression",
127 if (node.consequent.value === node.alternate.value) {
129 // Replace `foo ? true : true` with just `true`, but don't replace `foo() ? true : true`
130 return node.test.type === "Identifier" ? fixer.replaceText(node, node.consequent.value.toString()) : null;
132 if (node.alternate.value) {
134 // Replace `foo() ? false : true` with `!(foo())`
135 return fixer.replaceText(node, invertExpression(node.test));
138 // Replace `foo ? true : false` with `foo` if `foo` is guaranteed to be a boolean, or `!!foo` otherwise.
140 return fixer.replaceText(node, isBooleanExpression(node.test) ? astUtils.getParenthesisedText(sourceCode, node.test) : `!${invertExpression(node.test)}`);
143 } else if (!defaultAssignment && matchesDefaultAssignment(node)) {
146 messageId: "unnecessaryConditionalAssignment",
148 const shouldParenthesizeAlternate =
150 astUtils.getPrecedence(node.alternate) < OR_PRECEDENCE ||
151 astUtils.isCoalesceExpression(node.alternate)
153 !astUtils.isParenthesised(sourceCode, node.alternate);
154 const alternateText = shouldParenthesizeAlternate
155 ? `(${sourceCode.getText(node.alternate)})`
156 : astUtils.getParenthesisedText(sourceCode, node.alternate);
157 const testText = astUtils.getParenthesisedText(sourceCode, node.test);
159 return fixer.replaceText(node, `${testText} || ${alternateText}`);