]> git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/id-match.js
import 8.4.0 source
[pve-eslint.git] / eslint / lib / rules / id-match.js
1 /**
2 * @fileoverview Rule to flag non-matching identifiers
3 * @author Matthieu Larcher
4 */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Rule Definition
10 //------------------------------------------------------------------------------
11
12 /** @type {import('../shared/types').Rule} */
13 module.exports = {
14 meta: {
15 type: "suggestion",
16
17 docs: {
18 description: "require identifiers to match a specified regular expression",
19 recommended: false,
20 url: "https://eslint.org/docs/rules/id-match"
21 },
22
23 schema: [
24 {
25 type: "string"
26 },
27 {
28 type: "object",
29 properties: {
30 properties: {
31 type: "boolean",
32 default: false
33 },
34 classFields: {
35 type: "boolean",
36 default: false
37 },
38 onlyDeclarations: {
39 type: "boolean",
40 default: false
41 },
42 ignoreDestructuring: {
43 type: "boolean",
44 default: false
45 }
46 },
47 additionalProperties: false
48 }
49 ],
50 messages: {
51 notMatch: "Identifier '{{name}}' does not match the pattern '{{pattern}}'.",
52 notMatchPrivate: "Identifier '#{{name}}' does not match the pattern '{{pattern}}'."
53 }
54 },
55
56 create(context) {
57
58 //--------------------------------------------------------------------------
59 // Options
60 //--------------------------------------------------------------------------
61 const pattern = context.options[0] || "^.+$",
62 regexp = new RegExp(pattern, "u");
63
64 const options = context.options[1] || {},
65 checkProperties = !!options.properties,
66 checkClassFields = !!options.classFields,
67 onlyDeclarations = !!options.onlyDeclarations,
68 ignoreDestructuring = !!options.ignoreDestructuring;
69
70 //--------------------------------------------------------------------------
71 // Helpers
72 //--------------------------------------------------------------------------
73
74 // contains reported nodes to avoid reporting twice on destructuring with shorthand notation
75 const reportedNodes = new Set();
76 const ALLOWED_PARENT_TYPES = new Set(["CallExpression", "NewExpression"]);
77 const DECLARATION_TYPES = new Set(["FunctionDeclaration", "VariableDeclarator"]);
78 const IMPORT_TYPES = new Set(["ImportSpecifier", "ImportNamespaceSpecifier", "ImportDefaultSpecifier"]);
79
80 /**
81 * Checks if a string matches the provided pattern
82 * @param {string} name The string to check.
83 * @returns {boolean} if the string is a match
84 * @private
85 */
86 function isInvalid(name) {
87 return !regexp.test(name);
88 }
89
90 /**
91 * Checks if a parent of a node is an ObjectPattern.
92 * @param {ASTNode} node The node to check.
93 * @returns {boolean} if the node is inside an ObjectPattern
94 * @private
95 */
96 function isInsideObjectPattern(node) {
97 let { parent } = node;
98
99 while (parent) {
100 if (parent.type === "ObjectPattern") {
101 return true;
102 }
103
104 parent = parent.parent;
105 }
106
107 return false;
108 }
109
110 /**
111 * Verifies if we should report an error or not based on the effective
112 * parent node and the identifier name.
113 * @param {ASTNode} effectiveParent The effective parent node of the node to be reported
114 * @param {string} name The identifier name of the identifier node
115 * @returns {boolean} whether an error should be reported or not
116 */
117 function shouldReport(effectiveParent, name) {
118 return (!onlyDeclarations || DECLARATION_TYPES.has(effectiveParent.type)) &&
119 !ALLOWED_PARENT_TYPES.has(effectiveParent.type) && isInvalid(name);
120 }
121
122 /**
123 * Reports an AST node as a rule violation.
124 * @param {ASTNode} node The node to report.
125 * @returns {void}
126 * @private
127 */
128 function report(node) {
129
130 /*
131 * We used the range instead of the node because it's possible
132 * for the same identifier to be represented by two different
133 * nodes, with the most clear example being shorthand properties:
134 * { foo }
135 * In this case, "foo" is represented by one node for the name
136 * and one for the value. The only way to know they are the same
137 * is to look at the range.
138 */
139 if (!reportedNodes.has(node.range.toString())) {
140
141 const messageId = (node.type === "PrivateIdentifier")
142 ? "notMatchPrivate" : "notMatch";
143
144 context.report({
145 node,
146 messageId,
147 data: {
148 name: node.name,
149 pattern
150 }
151 });
152 reportedNodes.add(node.range.toString());
153 }
154 }
155
156 return {
157
158 Identifier(node) {
159 const name = node.name,
160 parent = node.parent,
161 effectiveParent = (parent.type === "MemberExpression") ? parent.parent : parent;
162
163 if (parent.type === "MemberExpression") {
164
165 if (!checkProperties) {
166 return;
167 }
168
169 // Always check object names
170 if (parent.object.type === "Identifier" &&
171 parent.object.name === name) {
172 if (isInvalid(name)) {
173 report(node);
174 }
175
176 // Report AssignmentExpressions left side's assigned variable id
177 } else if (effectiveParent.type === "AssignmentExpression" &&
178 effectiveParent.left.type === "MemberExpression" &&
179 effectiveParent.left.property.name === node.name) {
180 if (isInvalid(name)) {
181 report(node);
182 }
183
184 // Report AssignmentExpressions only if they are the left side of the assignment
185 } else if (effectiveParent.type === "AssignmentExpression" && effectiveParent.right.type !== "MemberExpression") {
186 if (isInvalid(name)) {
187 report(node);
188 }
189 }
190
191 /*
192 * Properties have their own rules, and
193 * AssignmentPattern nodes can be treated like Properties:
194 * e.g.: const { no_camelcased = false } = bar;
195 */
196 } else if (parent.type === "Property" || parent.type === "AssignmentPattern") {
197
198 if (parent.parent && parent.parent.type === "ObjectPattern") {
199 if (!ignoreDestructuring && parent.shorthand && parent.value.left && isInvalid(name)) {
200 report(node);
201 }
202
203 const assignmentKeyEqualsValue = parent.key.name === parent.value.name;
204
205 // prevent checking righthand side of destructured object
206 if (!assignmentKeyEqualsValue && parent.key === node) {
207 return;
208 }
209
210 const valueIsInvalid = parent.value.name && isInvalid(name);
211
212 // ignore destructuring if the option is set, unless a new identifier is created
213 if (valueIsInvalid && !(assignmentKeyEqualsValue && ignoreDestructuring)) {
214 report(node);
215 }
216 }
217
218 // never check properties or always ignore destructuring
219 if (!checkProperties || (ignoreDestructuring && isInsideObjectPattern(node))) {
220 return;
221 }
222
223 // don't check right hand side of AssignmentExpression to prevent duplicate warnings
224 if (parent.right !== node && shouldReport(effectiveParent, name)) {
225 report(node);
226 }
227
228 // Check if it's an import specifier
229 } else if (IMPORT_TYPES.has(parent.type)) {
230
231 // Report only if the local imported identifier is invalid
232 if (parent.local && parent.local.name === node.name && isInvalid(name)) {
233 report(node);
234 }
235
236 } else if (parent.type === "PropertyDefinition") {
237
238 if (checkClassFields && isInvalid(name)) {
239 report(node);
240 }
241
242 // Report anything that is invalid that isn't a CallExpression
243 } else if (shouldReport(effectiveParent, name)) {
244 report(node);
245 }
246 },
247
248 "PrivateIdentifier"(node) {
249
250 const isClassField = node.parent.type === "PropertyDefinition";
251
252 if (isClassField && !checkClassFields) {
253 return;
254 }
255
256 if (isInvalid(node.name)) {
257 report(node);
258 }
259 }
260
261 };
262
263 }
264 };