2 * @fileoverview Rule to flag use of unary increment and decrement operators.
3 * @author Ian Christian Myers
4 * @author Brody McKee (github.com/mrmckeb)
9 //------------------------------------------------------------------------------
11 //------------------------------------------------------------------------------
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.
18 function isForStatementUpdate(node
) {
19 const parent
= node
.parent
;
21 return parent
.type
=== "ForStatement" && parent
.update
=== node
;
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.
34 function isForLoopAfterthought(node
) {
35 const parent
= node
.parent
;
37 if (parent
.type
=== "SequenceExpression") {
38 return isForLoopAfterthought(parent
);
41 return isForStatementUpdate(node
);
44 //------------------------------------------------------------------------------
46 //------------------------------------------------------------------------------
53 description
: "disallow the unary operators `++` and `--`",
54 category
: "Stylistic Issues",
56 url
: "https://eslint.org/docs/rules/no-plusplus"
63 allowForLoopAfterthoughts
: {
68 additionalProperties
: false
73 unexpectedUnaryOp
: "Unary operator '{{operator}}' used."
79 const config
= context
.options
[0];
80 let allowForLoopAfterthoughts
= false;
82 if (typeof config
=== "object") {
83 allowForLoopAfterthoughts
= config
.allowForLoopAfterthoughts
=== true;
88 UpdateExpression(node
) {
89 if (allowForLoopAfterthoughts
&& isForLoopAfterthought(node
)) {
95 messageId
: "unexpectedUnaryOp",
97 operator
: node
.operator