]> git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/logical-assignment-operators.js
import 8.41.0 source
[pve-eslint.git] / eslint / lib / rules / logical-assignment-operators.js
1 /**
2 * @fileoverview Rule to replace assignment expressions with logical operator assignment
3 * @author Daniel Martens
4 */
5 "use strict";
6
7 //------------------------------------------------------------------------------
8 // Requirements
9 //------------------------------------------------------------------------------
10 const astUtils = require("./utils/ast-utils.js");
11
12 //------------------------------------------------------------------------------
13 // Helpers
14 //------------------------------------------------------------------------------
15
16 const baseTypes = new Set(["Identifier", "Super", "ThisExpression"]);
17
18 /**
19 * Returns true iff either "undefined" or a void expression (eg. "void 0")
20 * @param {ASTNode} expression Expression to check
21 * @param {import('eslint-scope').Scope} scope Scope of the expression
22 * @returns {boolean} True iff "undefined" or "void ..."
23 */
24 function isUndefined(expression, scope) {
25 if (expression.type === "Identifier" && expression.name === "undefined") {
26 return astUtils.isReferenceToGlobalVariable(scope, expression);
27 }
28
29 return expression.type === "UnaryExpression" &&
30 expression.operator === "void" &&
31 expression.argument.type === "Literal" &&
32 expression.argument.value === 0;
33 }
34
35 /**
36 * Returns true iff the reference is either an identifier or member expression
37 * @param {ASTNode} expression Expression to check
38 * @returns {boolean} True for identifiers and member expressions
39 */
40 function isReference(expression) {
41 return (expression.type === "Identifier" && expression.name !== "undefined") ||
42 expression.type === "MemberExpression";
43 }
44
45 /**
46 * Returns true iff the expression checks for nullish with loose equals.
47 * Examples: value == null, value == void 0
48 * @param {ASTNode} expression Test condition
49 * @param {import('eslint-scope').Scope} scope Scope of the expression
50 * @returns {boolean} True iff implicit nullish comparison
51 */
52 function isImplicitNullishComparison(expression, scope) {
53 if (expression.type !== "BinaryExpression" || expression.operator !== "==") {
54 return false;
55 }
56
57 const reference = isReference(expression.left) ? "left" : "right";
58 const nullish = reference === "left" ? "right" : "left";
59
60 return isReference(expression[reference]) &&
61 (astUtils.isNullLiteral(expression[nullish]) || isUndefined(expression[nullish], scope));
62 }
63
64 /**
65 * Condition with two equal comparisons.
66 * @param {ASTNode} expression Condition
67 * @returns {boolean} True iff matches ? === ? || ? === ?
68 */
69 function isDoubleComparison(expression) {
70 return expression.type === "LogicalExpression" &&
71 expression.operator === "||" &&
72 expression.left.type === "BinaryExpression" &&
73 expression.left.operator === "===" &&
74 expression.right.type === "BinaryExpression" &&
75 expression.right.operator === "===";
76 }
77
78 /**
79 * Returns true iff the expression checks for undefined and null.
80 * Example: value === null || value === undefined
81 * @param {ASTNode} expression Test condition
82 * @param {import('eslint-scope').Scope} scope Scope of the expression
83 * @returns {boolean} True iff explicit nullish comparison
84 */
85 function isExplicitNullishComparison(expression, scope) {
86 if (!isDoubleComparison(expression)) {
87 return false;
88 }
89 const leftReference = isReference(expression.left.left) ? "left" : "right";
90 const leftNullish = leftReference === "left" ? "right" : "left";
91 const rightReference = isReference(expression.right.left) ? "left" : "right";
92 const rightNullish = rightReference === "left" ? "right" : "left";
93
94 return astUtils.isSameReference(expression.left[leftReference], expression.right[rightReference]) &&
95 ((astUtils.isNullLiteral(expression.left[leftNullish]) && isUndefined(expression.right[rightNullish], scope)) ||
96 (isUndefined(expression.left[leftNullish], scope) && astUtils.isNullLiteral(expression.right[rightNullish])));
97 }
98
99 /**
100 * Returns true for Boolean(arg) calls
101 * @param {ASTNode} expression Test condition
102 * @param {import('eslint-scope').Scope} scope Scope of the expression
103 * @returns {boolean} Whether the expression is a boolean cast
104 */
105 function isBooleanCast(expression, scope) {
106 return expression.type === "CallExpression" &&
107 expression.callee.name === "Boolean" &&
108 expression.arguments.length === 1 &&
109 astUtils.isReferenceToGlobalVariable(scope, expression.callee);
110 }
111
112 /**
113 * Returns true for:
114 * truthiness checks: value, Boolean(value), !!value
115 * falsiness checks: !value, !Boolean(value)
116 * nullish checks: value == null, value === undefined || value === null
117 * @param {ASTNode} expression Test condition
118 * @param {import('eslint-scope').Scope} scope Scope of the expression
119 * @returns {?{ reference: ASTNode, operator: '??'|'||'|'&&'}} Null if not a known existence
120 */
121 function getExistence(expression, scope) {
122 const isNegated = expression.type === "UnaryExpression" && expression.operator === "!";
123 const base = isNegated ? expression.argument : expression;
124
125 switch (true) {
126 case isReference(base):
127 return { reference: base, operator: isNegated ? "||" : "&&" };
128 case base.type === "UnaryExpression" && base.operator === "!" && isReference(base.argument):
129 return { reference: base.argument, operator: "&&" };
130 case isBooleanCast(base, scope) && isReference(base.arguments[0]):
131 return { reference: base.arguments[0], operator: isNegated ? "||" : "&&" };
132 case isImplicitNullishComparison(expression, scope):
133 return { reference: isReference(expression.left) ? expression.left : expression.right, operator: "??" };
134 case isExplicitNullishComparison(expression, scope):
135 return { reference: isReference(expression.left.left) ? expression.left.left : expression.left.right, operator: "??" };
136 default: return null;
137 }
138 }
139
140 /**
141 * Returns true iff the node is inside a with block
142 * @param {ASTNode} node Node to check
143 * @returns {boolean} True iff passed node is inside a with block
144 */
145 function isInsideWithBlock(node) {
146 if (node.type === "Program") {
147 return false;
148 }
149
150 return node.parent.type === "WithStatement" && node.parent.body === node ? true : isInsideWithBlock(node.parent);
151 }
152
153 //------------------------------------------------------------------------------
154 // Rule Definition
155 //------------------------------------------------------------------------------
156 /** @type {import('../shared/types').Rule} */
157 module.exports = {
158 meta: {
159 type: "suggestion",
160
161 docs: {
162 description: "Require or disallow logical assignment operator shorthand",
163 recommended: false,
164 url: "https://eslint.org/docs/latest/rules/logical-assignment-operators"
165 },
166
167 schema: {
168 type: "array",
169 oneOf: [{
170 items: [
171 { const: "always" },
172 {
173 type: "object",
174 properties: {
175 enforceForIfStatements: {
176 type: "boolean"
177 }
178 },
179 additionalProperties: false
180 }
181 ],
182 minItems: 0, // 0 for allowing passing no options
183 maxItems: 2
184 }, {
185 items: [{ const: "never" }],
186 minItems: 1,
187 maxItems: 1
188 }]
189 },
190 fixable: "code",
191 // eslint-disable-next-line eslint-plugin/require-meta-has-suggestions -- Does not detect conditional suggestions
192 hasSuggestions: true,
193 messages: {
194 assignment: "Assignment (=) can be replaced with operator assignment ({{operator}}).",
195 useLogicalOperator: "Convert this assignment to use the operator {{ operator }}.",
196 logical: "Logical expression can be replaced with an assignment ({{ operator }}).",
197 convertLogical: "Replace this logical expression with an assignment with the operator {{ operator }}.",
198 if: "'if' statement can be replaced with a logical operator assignment with operator {{ operator }}.",
199 convertIf: "Replace this 'if' statement with a logical assignment with operator {{ operator }}.",
200 unexpected: "Unexpected logical operator assignment ({{operator}}) shorthand.",
201 separate: "Separate the logical assignment into an assignment with a logical operator."
202 }
203 },
204
205 create(context) {
206 const mode = context.options[0] === "never" ? "never" : "always";
207 const checkIf = mode === "always" && context.options.length > 1 && context.options[1].enforceForIfStatements;
208 const sourceCode = context.sourceCode;
209 const isStrict = sourceCode.getScope(sourceCode.ast).isStrict;
210
211 /**
212 * Returns false if the access could be a getter
213 * @param {ASTNode} node Assignment expression
214 * @returns {boolean} True iff the fix is safe
215 */
216 function cannotBeGetter(node) {
217 return node.type === "Identifier" &&
218 (isStrict || !isInsideWithBlock(node));
219 }
220
221 /**
222 * Check whether only a single property is accessed
223 * @param {ASTNode} node reference
224 * @returns {boolean} True iff a single property is accessed
225 */
226 function accessesSingleProperty(node) {
227 if (!isStrict && isInsideWithBlock(node)) {
228 return node.type === "Identifier";
229 }
230
231 return node.type === "MemberExpression" &&
232 baseTypes.has(node.object.type) &&
233 (!node.computed || (node.property.type !== "MemberExpression" && node.property.type !== "ChainExpression"));
234 }
235
236 /**
237 * Adds a fixer or suggestion whether on the fix is safe.
238 * @param {{ messageId: string, node: ASTNode }} descriptor Report descriptor without fix or suggest
239 * @param {{ messageId: string, fix: Function }} suggestion Adds the fix or the whole suggestion as only element in "suggest" to suggestion
240 * @param {boolean} shouldBeFixed Fix iff the condition is true
241 * @returns {Object} Descriptor with either an added fix or suggestion
242 */
243 function createConditionalFixer(descriptor, suggestion, shouldBeFixed) {
244 if (shouldBeFixed) {
245 return {
246 ...descriptor,
247 fix: suggestion.fix
248 };
249 }
250
251 return {
252 ...descriptor,
253 suggest: [suggestion]
254 };
255 }
256
257
258 /**
259 * Returns the operator token for assignments and binary expressions
260 * @param {ASTNode} node AssignmentExpression or BinaryExpression
261 * @returns {import('eslint').AST.Token} Operator token between the left and right expression
262 */
263 function getOperatorToken(node) {
264 return sourceCode.getFirstTokenBetween(node.left, node.right, token => token.value === node.operator);
265 }
266
267 if (mode === "never") {
268 return {
269
270 // foo ||= bar
271 "AssignmentExpression"(assignment) {
272 if (!astUtils.isLogicalAssignmentOperator(assignment.operator)) {
273 return;
274 }
275
276 const descriptor = {
277 messageId: "unexpected",
278 node: assignment,
279 data: { operator: assignment.operator }
280 };
281 const suggestion = {
282 messageId: "separate",
283 *fix(ruleFixer) {
284 if (sourceCode.getCommentsInside(assignment).length > 0) {
285 return;
286 }
287
288 const operatorToken = getOperatorToken(assignment);
289
290 // -> foo = bar
291 yield ruleFixer.replaceText(operatorToken, "=");
292
293 const assignmentText = sourceCode.getText(assignment.left);
294 const operator = assignment.operator.slice(0, -1);
295
296 // -> foo = foo || bar
297 yield ruleFixer.insertTextAfter(operatorToken, ` ${assignmentText} ${operator}`);
298
299 const precedence = astUtils.getPrecedence(assignment.right) <= astUtils.getPrecedence({ type: "LogicalExpression", operator });
300
301 // ?? and || / && cannot be mixed but have same precedence
302 const mixed = assignment.operator === "??=" && astUtils.isLogicalExpression(assignment.right);
303
304 if (!astUtils.isParenthesised(sourceCode, assignment.right) && (precedence || mixed)) {
305
306 // -> foo = foo || (bar)
307 yield ruleFixer.insertTextBefore(assignment.right, "(");
308 yield ruleFixer.insertTextAfter(assignment.right, ")");
309 }
310 }
311 };
312
313 context.report(createConditionalFixer(descriptor, suggestion, cannotBeGetter(assignment.left)));
314 }
315 };
316 }
317
318 return {
319
320 // foo = foo || bar
321 "AssignmentExpression[operator='='][right.type='LogicalExpression']"(assignment) {
322 if (!astUtils.isSameReference(assignment.left, assignment.right.left)) {
323 return;
324 }
325
326 const descriptor = {
327 messageId: "assignment",
328 node: assignment,
329 data: { operator: `${assignment.right.operator}=` }
330 };
331 const suggestion = {
332 messageId: "useLogicalOperator",
333 data: { operator: `${assignment.right.operator}=` },
334 *fix(ruleFixer) {
335 if (sourceCode.getCommentsInside(assignment).length > 0) {
336 return;
337 }
338
339 // No need for parenthesis around the assignment based on precedence as the precedence stays the same even with changed operator
340 const assignmentOperatorToken = getOperatorToken(assignment);
341
342 // -> foo ||= foo || bar
343 yield ruleFixer.insertTextBefore(assignmentOperatorToken, assignment.right.operator);
344
345 // -> foo ||= bar
346 const logicalOperatorToken = getOperatorToken(assignment.right);
347 const firstRightOperandToken = sourceCode.getTokenAfter(logicalOperatorToken);
348
349 yield ruleFixer.removeRange([assignment.right.range[0], firstRightOperandToken.range[0]]);
350 }
351 };
352
353 context.report(createConditionalFixer(descriptor, suggestion, cannotBeGetter(assignment.left)));
354 },
355
356 // foo || (foo = bar)
357 'LogicalExpression[right.type="AssignmentExpression"][right.operator="="]'(logical) {
358
359 // Right side has to be parenthesized, otherwise would be parsed as (foo || foo) = bar which is illegal
360 if (isReference(logical.left) && astUtils.isSameReference(logical.left, logical.right.left)) {
361 const descriptor = {
362 messageId: "logical",
363 node: logical,
364 data: { operator: `${logical.operator}=` }
365 };
366 const suggestion = {
367 messageId: "convertLogical",
368 data: { operator: `${logical.operator}=` },
369 *fix(ruleFixer) {
370 if (sourceCode.getCommentsInside(logical).length > 0) {
371 return;
372 }
373
374 const requiresOuterParenthesis = logical.parent.type !== "ExpressionStatement" &&
375 (astUtils.getPrecedence({ type: "AssignmentExpression" }) < astUtils.getPrecedence(logical.parent));
376
377 if (!astUtils.isParenthesised(sourceCode, logical) && requiresOuterParenthesis) {
378 yield ruleFixer.insertTextBefore(logical, "(");
379 yield ruleFixer.insertTextAfter(logical, ")");
380 }
381
382 // Also removes all opening parenthesis
383 yield ruleFixer.removeRange([logical.range[0], logical.right.range[0]]); // -> foo = bar)
384
385 // Also removes all ending parenthesis
386 yield ruleFixer.removeRange([logical.right.range[1], logical.range[1]]); // -> foo = bar
387
388 const operatorToken = getOperatorToken(logical.right);
389
390 yield ruleFixer.insertTextBefore(operatorToken, logical.operator); // -> foo ||= bar
391 }
392 };
393 const fix = cannotBeGetter(logical.left) || accessesSingleProperty(logical.left);
394
395 context.report(createConditionalFixer(descriptor, suggestion, fix));
396 }
397 },
398
399 // if (foo) foo = bar
400 "IfStatement[alternate=null]"(ifNode) {
401 if (!checkIf) {
402 return;
403 }
404
405 const hasBody = ifNode.consequent.type === "BlockStatement";
406
407 if (hasBody && ifNode.consequent.body.length !== 1) {
408 return;
409 }
410
411 const body = hasBody ? ifNode.consequent.body[0] : ifNode.consequent;
412 const scope = sourceCode.getScope(ifNode);
413 const existence = getExistence(ifNode.test, scope);
414
415 if (
416 body.type === "ExpressionStatement" &&
417 body.expression.type === "AssignmentExpression" &&
418 body.expression.operator === "=" &&
419 existence !== null &&
420 astUtils.isSameReference(existence.reference, body.expression.left)
421 ) {
422 const descriptor = {
423 messageId: "if",
424 node: ifNode,
425 data: { operator: `${existence.operator}=` }
426 };
427 const suggestion = {
428 messageId: "convertIf",
429 data: { operator: `${existence.operator}=` },
430 *fix(ruleFixer) {
431 if (sourceCode.getCommentsInside(ifNode).length > 0) {
432 return;
433 }
434
435 const firstBodyToken = sourceCode.getFirstToken(body);
436 const prevToken = sourceCode.getTokenBefore(ifNode);
437
438 if (
439 prevToken !== null &&
440 prevToken.value !== ";" &&
441 prevToken.value !== "{" &&
442 firstBodyToken.type !== "Identifier" &&
443 firstBodyToken.type !== "Keyword"
444 ) {
445
446 // Do not fix if the fixed statement could be part of the previous statement (eg. fn() if (a == null) (a) = b --> fn()(a) ??= b)
447 return;
448 }
449
450
451 const operatorToken = getOperatorToken(body.expression);
452
453 yield ruleFixer.insertTextBefore(operatorToken, existence.operator); // -> if (foo) foo ||= bar
454
455 yield ruleFixer.removeRange([ifNode.range[0], body.range[0]]); // -> foo ||= bar
456
457 yield ruleFixer.removeRange([body.range[1], ifNode.range[1]]); // -> foo ||= bar, only present if "if" had a body
458
459 const nextToken = sourceCode.getTokenAfter(body.expression);
460
461 if (hasBody && (nextToken !== null && nextToken.value !== ";")) {
462 yield ruleFixer.insertTextAfter(ifNode, ";");
463 }
464 }
465 };
466 const shouldBeFixed = cannotBeGetter(existence.reference) ||
467 (ifNode.test.type !== "LogicalExpression" && accessesSingleProperty(existence.reference));
468
469 context.report(createConditionalFixer(descriptor, suggestion, shouldBeFixed));
470 }
471 }
472 };
473 }
474 };