1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const astUtils = require("./utils/ast-utils");
|
9 |
|
10 |
|
11 | const BOOLEAN_OPERATORS = new Set(["==", "===", "!=", "!==", ">", ">=", "<", "<=", "in", "instanceof"]);
|
12 | const OPERATOR_INVERSES = {
|
13 | "==": "!=",
|
14 | "!=": "==",
|
15 | "===": "!==",
|
16 | "!==": "==="
|
17 |
|
18 |
|
19 | };
|
20 | const OR_PRECEDENCE = astUtils.getPrecedence({ type: "LogicalExpression", operator: "||" });
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | module.exports = {
|
27 | meta: {
|
28 | type: "suggestion",
|
29 |
|
30 | docs: {
|
31 | description: "disallow ternary operators when simpler alternatives exist",
|
32 | category: "Stylistic Issues",
|
33 | recommended: false,
|
34 | url: "https://eslint.org/docs/rules/no-unneeded-ternary"
|
35 | },
|
36 |
|
37 | schema: [
|
38 | {
|
39 | type: "object",
|
40 | properties: {
|
41 | defaultAssignment: {
|
42 | type: "boolean",
|
43 | default: true
|
44 | }
|
45 | },
|
46 | additionalProperties: false
|
47 | }
|
48 | ],
|
49 |
|
50 | fixable: "code",
|
51 |
|
52 | messages: {
|
53 | unnecessaryConditionalExpression: "Unnecessary use of boolean literals in conditional expression.",
|
54 | unnecessaryConditionalAssignment: "Unnecessary use of conditional expression for default assignment."
|
55 | }
|
56 | },
|
57 |
|
58 | create(context) {
|
59 | const options = context.options[0] || {};
|
60 | const defaultAssignment = options.defaultAssignment !== false;
|
61 | const sourceCode = context.getSourceCode();
|
62 |
|
63 | |
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 | function isBooleanLiteral(node) {
|
70 | return node.type === "Literal" && typeof node.value === "boolean";
|
71 | }
|
72 |
|
73 | |
74 |
|
75 |
|
76 |
|
77 |
|
78 | function invertExpression(node) {
|
79 | if (node.type === "BinaryExpression" && Object.prototype.hasOwnProperty.call(OPERATOR_INVERSES, node.operator)) {
|
80 | const operatorToken = sourceCode.getFirstTokenBetween(
|
81 | node.left,
|
82 | node.right,
|
83 | token => token.value === node.operator
|
84 | );
|
85 | const text = sourceCode.getText();
|
86 |
|
87 | return text.slice(node.range[0],
|
88 | operatorToken.range[0]) + OPERATOR_INVERSES[node.operator] + text.slice(operatorToken.range[1], node.range[1]);
|
89 | }
|
90 |
|
91 | if (astUtils.getPrecedence(node) < astUtils.getPrecedence({ type: "UnaryExpression" })) {
|
92 | return `!(${astUtils.getParenthesisedText(sourceCode, node)})`;
|
93 | }
|
94 | return `!${astUtils.getParenthesisedText(sourceCode, node)}`;
|
95 | }
|
96 |
|
97 | |
98 |
|
99 |
|
100 |
|
101 |
|
102 | function isBooleanExpression(node) {
|
103 | return node.type === "BinaryExpression" && BOOLEAN_OPERATORS.has(node.operator) ||
|
104 | node.type === "UnaryExpression" && node.operator === "!";
|
105 | }
|
106 |
|
107 | |
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 | function matchesDefaultAssignment(node) {
|
114 | return node.test.type === "Identifier" &&
|
115 | node.consequent.type === "Identifier" &&
|
116 | node.test.name === node.consequent.name;
|
117 | }
|
118 |
|
119 | return {
|
120 |
|
121 | ConditionalExpression(node) {
|
122 | if (isBooleanLiteral(node.alternate) && isBooleanLiteral(node.consequent)) {
|
123 | context.report({
|
124 | node,
|
125 | messageId: "unnecessaryConditionalExpression",
|
126 | fix(fixer) {
|
127 | if (node.consequent.value === node.alternate.value) {
|
128 |
|
129 |
|
130 | return node.test.type === "Identifier" ? fixer.replaceText(node, node.consequent.value.toString()) : null;
|
131 | }
|
132 | if (node.alternate.value) {
|
133 |
|
134 |
|
135 | return fixer.replaceText(node, invertExpression(node.test));
|
136 | }
|
137 |
|
138 |
|
139 |
|
140 | return fixer.replaceText(node, isBooleanExpression(node.test) ? astUtils.getParenthesisedText(sourceCode, node.test) : `!${invertExpression(node.test)}`);
|
141 | }
|
142 | });
|
143 | } else if (!defaultAssignment && matchesDefaultAssignment(node)) {
|
144 | context.report({
|
145 | node,
|
146 | messageId: "unnecessaryConditionalAssignment",
|
147 | fix: fixer => {
|
148 | const shouldParenthesizeAlternate =
|
149 | (
|
150 | astUtils.getPrecedence(node.alternate) < OR_PRECEDENCE ||
|
151 | astUtils.isCoalesceExpression(node.alternate)
|
152 | ) &&
|
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);
|
158 |
|
159 | return fixer.replaceText(node, `${testText} || ${alternateText}`);
|
160 | }
|
161 | });
|
162 | }
|
163 | }
|
164 | };
|
165 | }
|
166 | };
|