]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/id-match.js
2 * @fileoverview Rule to flag non-matching identifiers
3 * @author Matthieu Larcher
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 /** @type {import('../shared/types').Rule} */
18 description
: "Require identifiers to match a specified regular expression",
20 url
: "https://eslint.org/docs/latest/rules/id-match"
42 ignoreDestructuring
: {
47 additionalProperties
: false
51 notMatch
: "Identifier '{{name}}' does not match the pattern '{{pattern}}'.",
52 notMatchPrivate
: "Identifier '#{{name}}' does not match the pattern '{{pattern}}'."
58 //--------------------------------------------------------------------------
60 //--------------------------------------------------------------------------
61 const pattern
= context
.options
[0] || "^.+$",
62 regexp
= new RegExp(pattern
, "u");
64 const options
= context
.options
[1] || {},
65 checkProperties
= !!options
.properties
,
66 checkClassFields
= !!options
.classFields
,
67 onlyDeclarations
= !!options
.onlyDeclarations
,
68 ignoreDestructuring
= !!options
.ignoreDestructuring
;
70 const sourceCode
= context
.sourceCode
;
73 //--------------------------------------------------------------------------
75 //--------------------------------------------------------------------------
77 // contains reported nodes to avoid reporting twice on destructuring with shorthand notation
78 const reportedNodes
= new Set();
79 const ALLOWED_PARENT_TYPES
= new Set(["CallExpression", "NewExpression"]);
80 const DECLARATION_TYPES
= new Set(["FunctionDeclaration", "VariableDeclarator"]);
81 const IMPORT_TYPES
= new Set(["ImportSpecifier", "ImportNamespaceSpecifier", "ImportDefaultSpecifier"]);
84 * Checks whether the given node represents a reference to a global variable that is not declared in the source code.
85 * These identifiers will be allowed, as it is assumed that user has no control over the names of external global variables.
86 * @param {ASTNode} node `Identifier` node to check.
87 * @returns {boolean} `true` if the node is a reference to a global variable.
89 function isReferenceToGlobalVariable(node
) {
90 const variable
= globalScope
.set.get(node
.name
);
92 return variable
&& variable
.defs
.length
=== 0 &&
93 variable
.references
.some(ref
=> ref
.identifier
=== node
);
97 * Checks if a string matches the provided pattern
98 * @param {string} name The string to check.
99 * @returns {boolean} if the string is a match
102 function isInvalid(name
) {
103 return !regexp
.test(name
);
107 * Checks if a parent of a node is an ObjectPattern.
108 * @param {ASTNode} node The node to check.
109 * @returns {boolean} if the node is inside an ObjectPattern
112 function isInsideObjectPattern(node
) {
113 let { parent
} = node
;
116 if (parent
.type
=== "ObjectPattern") {
120 parent
= parent
.parent
;
127 * Verifies if we should report an error or not based on the effective
128 * parent node and the identifier name.
129 * @param {ASTNode} effectiveParent The effective parent node of the node to be reported
130 * @param {string} name The identifier name of the identifier node
131 * @returns {boolean} whether an error should be reported or not
133 function shouldReport(effectiveParent
, name
) {
134 return (!onlyDeclarations
|| DECLARATION_TYPES
.has(effectiveParent
.type
)) &&
135 !ALLOWED_PARENT_TYPES
.has(effectiveParent
.type
) && isInvalid(name
);
139 * Reports an AST node as a rule violation.
140 * @param {ASTNode} node The node to report.
144 function report(node
) {
147 * We used the range instead of the node because it's possible
148 * for the same identifier to be represented by two different
149 * nodes, with the most clear example being shorthand properties:
151 * In this case, "foo" is represented by one node for the name
152 * and one for the value. The only way to know they are the same
153 * is to look at the range.
155 if (!reportedNodes
.has(node
.range
.toString())) {
157 const messageId
= (node
.type
=== "PrivateIdentifier")
158 ? "notMatchPrivate" : "notMatch";
168 reportedNodes
.add(node
.range
.toString());
175 globalScope
= sourceCode
.getScope(node
);
179 const name
= node
.name
,
180 parent
= node
.parent
,
181 effectiveParent
= (parent
.type
=== "MemberExpression") ? parent
.parent
: parent
;
183 if (isReferenceToGlobalVariable(node
)) {
187 if (parent
.type
=== "MemberExpression") {
189 if (!checkProperties
) {
193 // Always check object names
194 if (parent
.object
.type
=== "Identifier" &&
195 parent
.object
.name
=== name
) {
196 if (isInvalid(name
)) {
200 // Report AssignmentExpressions left side's assigned variable id
201 } else if (effectiveParent
.type
=== "AssignmentExpression" &&
202 effectiveParent
.left
.type
=== "MemberExpression" &&
203 effectiveParent
.left
.property
.name
=== node
.name
) {
204 if (isInvalid(name
)) {
208 // Report AssignmentExpressions only if they are the left side of the assignment
209 } else if (effectiveParent
.type
=== "AssignmentExpression" && effectiveParent
.right
.type
!== "MemberExpression") {
210 if (isInvalid(name
)) {
215 // For https://github.com/eslint/eslint/issues/15123
217 parent
.type
=== "Property" &&
218 parent
.parent
.type
=== "ObjectExpression" &&
219 parent
.key
=== node
&&
222 if (checkProperties
&& isInvalid(name
)) {
227 * Properties have their own rules, and
228 * AssignmentPattern nodes can be treated like Properties:
229 * e.g.: const { no_camelcased = false } = bar;
231 } else if (parent
.type
=== "Property" || parent
.type
=== "AssignmentPattern") {
233 if (parent
.parent
&& parent
.parent
.type
=== "ObjectPattern") {
234 if (!ignoreDestructuring
&& parent
.shorthand
&& parent
.value
.left
&& isInvalid(name
)) {
238 const assignmentKeyEqualsValue
= parent
.key
.name
=== parent
.value
.name
;
240 // prevent checking righthand side of destructured object
241 if (!assignmentKeyEqualsValue
&& parent
.key
=== node
) {
245 const valueIsInvalid
= parent
.value
.name
&& isInvalid(name
);
247 // ignore destructuring if the option is set, unless a new identifier is created
248 if (valueIsInvalid
&& !(assignmentKeyEqualsValue
&& ignoreDestructuring
)) {
253 // never check properties or always ignore destructuring
254 if ((!checkProperties
&& !parent
.computed
) || (ignoreDestructuring
&& isInsideObjectPattern(node
))) {
258 // don't check right hand side of AssignmentExpression to prevent duplicate warnings
259 if (parent
.right
!== node
&& shouldReport(effectiveParent
, name
)) {
263 // Check if it's an import specifier
264 } else if (IMPORT_TYPES
.has(parent
.type
)) {
266 // Report only if the local imported identifier is invalid
267 if (parent
.local
&& parent
.local
.name
=== node
.name
&& isInvalid(name
)) {
271 } else if (parent
.type
=== "PropertyDefinition") {
273 if (checkClassFields
&& isInvalid(name
)) {
277 // Report anything that is invalid that isn't a CallExpression
278 } else if (shouldReport(effectiveParent
, name
)) {
283 "PrivateIdentifier"(node
) {
285 const isClassField
= node
.parent
.type
=== "PropertyDefinition";
287 if (isClassField
&& !checkClassFields
) {
291 if (isInvalid(node
.name
)) {