53be62e68a3626a05cb0b5ad7f98a68c150f3468
[dotfiles/.git] / id-blacklist.js
1 /**
2  * @fileoverview Rule that warns when identifier names that are
3  * blacklisted in the configuration are used.
4  * @author Keith Cirkel (http://keithcirkel.co.uk)
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 specified identifiers",
19             category: "Stylistic Issues",
20             recommended: false,
21             url: "https://eslint.org/docs/rules/id-blacklist"
22         },
23
24         schema: {
25             type: "array",
26             items: {
27                 type: "string"
28             },
29             uniqueItems: true
30         },
31         messages: {
32             blacklisted: "Identifier '{{name}}' is blacklisted."
33         }
34     },
35
36     create(context) {
37
38
39         //--------------------------------------------------------------------------
40         // Helpers
41         //--------------------------------------------------------------------------
42
43         const blacklist = context.options;
44
45
46         /**
47          * Checks if a string matches the provided pattern
48          * @param {string} name The string to check.
49          * @returns {boolean} if the string is a match
50          * @private
51          */
52         function isInvalid(name) {
53             return blacklist.indexOf(name) !== -1;
54         }
55
56         /**
57          * Verifies if we should report an error or not based on the effective
58          * parent node and the identifier name.
59          * @param {ASTNode} effectiveParent The effective parent node of the node to be reported
60          * @param {string} name The identifier name of the identifier node
61          * @returns {boolean} whether an error should be reported or not
62          */
63         function shouldReport(effectiveParent, name) {
64             return effectiveParent.type !== "CallExpression" &&
65                 effectiveParent.type !== "NewExpression" &&
66                 isInvalid(name);
67         }
68
69         /**
70          * Reports an AST node as a rule violation.
71          * @param {ASTNode} node The node to report.
72          * @returns {void}
73          * @private
74          */
75         function report(node) {
76             context.report({
77                 node,
78                 messageId: "blacklisted",
79                 data: {
80                     name: node.name
81                 }
82             });
83         }
84
85         return {
86
87             Identifier(node) {
88                 const name = node.name,
89                     effectiveParent = (node.parent.type === "MemberExpression") ? node.parent.parent : node.parent;
90
91                 // MemberExpressions get special rules
92                 if (node.parent.type === "MemberExpression") {
93
94                     // Always check object names
95                     if (node.parent.object.type === "Identifier" &&
96                         node.parent.object.name === node.name) {
97                         if (isInvalid(name)) {
98                             report(node);
99                         }
100
101                         // Report AssignmentExpressions only if they are the left side of the assignment
102                     } else if (effectiveParent.type === "AssignmentExpression" &&
103                         (effectiveParent.right.type !== "MemberExpression" ||
104                         effectiveParent.left.type === "MemberExpression" &&
105                         effectiveParent.left.property.name === node.name)) {
106                         if (isInvalid(name)) {
107                             report(node);
108                         }
109                     }
110
111                 // Properties have their own rules
112                 } else if (node.parent.type === "Property") {
113
114                     if (shouldReport(effectiveParent, name)) {
115                         report(node);
116                     }
117
118                 // Report anything that is a match and not a CallExpression
119                 } else if (shouldReport(effectiveParent, name)) {
120                     report(node);
121                 }
122             }
123
124         };
125
126     }
127 };