blob: 2565da43231a254c1182c4b48aa040f5fe6a347b [file] [log] [blame]
Yang Guo4fd355c2019-09-19 10:59:03 +02001/**
2 * @fileoverview Rule to flag use of unary increment and decrement operators.
3 * @author Ian Christian Myers
4 * @author Brody McKee (github.com/mrmckeb)
5 */
6
7"use strict";
8
9//------------------------------------------------------------------------------
Tim van der Lippe16aca392020-11-13 11:37:13 +000010// Helpers
11//------------------------------------------------------------------------------
12
13/**
14 * Determines whether the given node is the update node of a `ForStatement`.
15 * @param {ASTNode} node The node to check.
16 * @returns {boolean} `true` if the node is `ForStatement` update.
17 */
18function isForStatementUpdate(node) {
19 const parent = node.parent;
20
21 return parent.type === "ForStatement" && parent.update === node;
22}
23
24/**
25 * Determines whether the given node is considered to be a for loop "afterthought" by the logic of this rule.
26 * In particular, it returns `true` if the given node is either:
27 * - The update node of a `ForStatement`: for (;; i++) {}
28 * - An operand of a sequence expression that is the update node: for (;; foo(), i++) {}
29 * - An operand of a sequence expression that is child of another sequence expression, etc.,
30 * up to the sequence expression that is the update node: for (;; foo(), (bar(), (baz(), i++))) {}
31 * @param {ASTNode} node The node to check.
32 * @returns {boolean} `true` if the node is a for loop afterthought.
33 */
34function isForLoopAfterthought(node) {
35 const parent = node.parent;
36
37 if (parent.type === "SequenceExpression") {
38 return isForLoopAfterthought(parent);
39 }
40
41 return isForStatementUpdate(node);
42}
43
44//------------------------------------------------------------------------------
Yang Guo4fd355c2019-09-19 10:59:03 +020045// Rule Definition
46//------------------------------------------------------------------------------
47
Tim van der Lippe0ceb4652022-01-06 14:23:36 +010048/** @type {import('../shared/types').Rule} */
Yang Guo4fd355c2019-09-19 10:59:03 +020049module.exports = {
50 meta: {
51 type: "suggestion",
52
53 docs: {
54 description: "disallow the unary operators `++` and `--`",
Yang Guo4fd355c2019-09-19 10:59:03 +020055 recommended: false,
56 url: "https://eslint.org/docs/rules/no-plusplus"
57 },
58
59 schema: [
60 {
61 type: "object",
62 properties: {
63 allowForLoopAfterthoughts: {
64 type: "boolean",
65 default: false
66 }
67 },
68 additionalProperties: false
69 }
Tim van der Lippe16aca392020-11-13 11:37:13 +000070 ],
71
72 messages: {
73 unexpectedUnaryOp: "Unary operator '{{operator}}' used."
74 }
Yang Guo4fd355c2019-09-19 10:59:03 +020075 },
76
77 create(context) {
78
79 const config = context.options[0];
Tim van der Lippe16aca392020-11-13 11:37:13 +000080 let allowForLoopAfterthoughts = false;
Yang Guo4fd355c2019-09-19 10:59:03 +020081
82 if (typeof config === "object") {
Tim van der Lippe16aca392020-11-13 11:37:13 +000083 allowForLoopAfterthoughts = config.allowForLoopAfterthoughts === true;
Yang Guo4fd355c2019-09-19 10:59:03 +020084 }
85
86 return {
87
88 UpdateExpression(node) {
Tim van der Lippe16aca392020-11-13 11:37:13 +000089 if (allowForLoopAfterthoughts && isForLoopAfterthought(node)) {
Yang Guo4fd355c2019-09-19 10:59:03 +020090 return;
91 }
Tim van der Lippe16aca392020-11-13 11:37:13 +000092
Yang Guo4fd355c2019-09-19 10:59:03 +020093 context.report({
94 node,
Tim van der Lippe16aca392020-11-13 11:37:13 +000095 messageId: "unexpectedUnaryOp",
Yang Guo4fd355c2019-09-19 10:59:03 +020096 data: {
97 operator: node.operator
98 }
99 });
100 }
101
102 };
103
104 }
105};