blob: d7b6c730562670deae186004537f2e0f9e86cc54 [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
48module.exports = {
49 meta: {
50 type: "suggestion",
51
52 docs: {
53 description: "disallow the unary operators `++` and `--`",
Yang Guo4fd355c2019-09-19 10:59:03 +020054 recommended: false,
55 url: "https://eslint.org/docs/rules/no-plusplus"
56 },
57
58 schema: [
59 {
60 type: "object",
61 properties: {
62 allowForLoopAfterthoughts: {
63 type: "boolean",
64 default: false
65 }
66 },
67 additionalProperties: false
68 }
Tim van der Lippe16aca392020-11-13 11:37:13 +000069 ],
70
71 messages: {
72 unexpectedUnaryOp: "Unary operator '{{operator}}' used."
73 }
Yang Guo4fd355c2019-09-19 10:59:03 +020074 },
75
76 create(context) {
77
78 const config = context.options[0];
Tim van der Lippe16aca392020-11-13 11:37:13 +000079 let allowForLoopAfterthoughts = false;
Yang Guo4fd355c2019-09-19 10:59:03 +020080
81 if (typeof config === "object") {
Tim van der Lippe16aca392020-11-13 11:37:13 +000082 allowForLoopAfterthoughts = config.allowForLoopAfterthoughts === true;
Yang Guo4fd355c2019-09-19 10:59:03 +020083 }
84
85 return {
86
87 UpdateExpression(node) {
Tim van der Lippe16aca392020-11-13 11:37:13 +000088 if (allowForLoopAfterthoughts && isForLoopAfterthought(node)) {
Yang Guo4fd355c2019-09-19 10:59:03 +020089 return;
90 }
Tim van der Lippe16aca392020-11-13 11:37:13 +000091
Yang Guo4fd355c2019-09-19 10:59:03 +020092 context.report({
93 node,
Tim van der Lippe16aca392020-11-13 11:37:13 +000094 messageId: "unexpectedUnaryOp",
Yang Guo4fd355c2019-09-19 10:59:03 +020095 data: {
96 operator: node.operator
97 }
98 });
99 }
100
101 };
102
103 }
104};