Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 1 | /** |
| 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 Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 10 | // 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 | */ |
| 18 | function 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 | */ |
| 34 | function 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 Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 45 | // Rule Definition |
| 46 | //------------------------------------------------------------------------------ |
| 47 | |
| 48 | module.exports = { |
| 49 | meta: { |
| 50 | type: "suggestion", |
| 51 | |
| 52 | docs: { |
| 53 | description: "disallow the unary operators `++` and `--`", |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 54 | 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 Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 69 | ], |
| 70 | |
| 71 | messages: { |
| 72 | unexpectedUnaryOp: "Unary operator '{{operator}}' used." |
| 73 | } |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 74 | }, |
| 75 | |
| 76 | create(context) { |
| 77 | |
| 78 | const config = context.options[0]; |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 79 | let allowForLoopAfterthoughts = false; |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 80 | |
| 81 | if (typeof config === "object") { |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 82 | allowForLoopAfterthoughts = config.allowForLoopAfterthoughts === true; |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | return { |
| 86 | |
| 87 | UpdateExpression(node) { |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 88 | if (allowForLoopAfterthoughts && isForLoopAfterthought(node)) { |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 89 | return; |
| 90 | } |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 91 | |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 92 | context.report({ |
| 93 | node, |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 94 | messageId: "unexpectedUnaryOp", |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 95 | data: { |
| 96 | operator: node.operator |
| 97 | } |
| 98 | }); |
| 99 | } |
| 100 | |
| 101 | }; |
| 102 | |
| 103 | } |
| 104 | }; |