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 | //------------------------------------------------------------------------------ |
| 10 | // Rule Definition |
| 11 | //------------------------------------------------------------------------------ |
| 12 | |
| 13 | module.exports = { |
| 14 | meta: { |
| 15 | type: "suggestion", |
| 16 | |
| 17 | docs: { |
| 18 | description: "disallow the unary operators `++` and `--`", |
| 19 | category: "Stylistic Issues", |
| 20 | recommended: false, |
| 21 | url: "https://eslint.org/docs/rules/no-plusplus" |
| 22 | }, |
| 23 | |
| 24 | schema: [ |
| 25 | { |
| 26 | type: "object", |
| 27 | properties: { |
| 28 | allowForLoopAfterthoughts: { |
| 29 | type: "boolean", |
| 30 | default: false |
| 31 | } |
| 32 | }, |
| 33 | additionalProperties: false |
| 34 | } |
| 35 | ] |
| 36 | }, |
| 37 | |
| 38 | create(context) { |
| 39 | |
| 40 | const config = context.options[0]; |
| 41 | let allowInForAfterthought = false; |
| 42 | |
| 43 | if (typeof config === "object") { |
| 44 | allowInForAfterthought = config.allowForLoopAfterthoughts === true; |
| 45 | } |
| 46 | |
| 47 | return { |
| 48 | |
| 49 | UpdateExpression(node) { |
| 50 | if (allowInForAfterthought && node.parent.type === "ForStatement") { |
| 51 | return; |
| 52 | } |
| 53 | context.report({ |
| 54 | node, |
| 55 | message: "Unary operator '{{operator}}' used.", |
| 56 | data: { |
| 57 | operator: node.operator |
| 58 | } |
| 59 | }); |
| 60 | } |
| 61 | |
| 62 | }; |
| 63 | |
| 64 | } |
| 65 | }; |