UNPKG

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