UNPKG

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