]>
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/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 //--------------------------------------------------------------------------
72 //--------------------------------------------------------------------------
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"]);
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
86 function isInvalid(name
) {
87 return !regexp
.test(name
);
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
96 function isInsideObjectPattern(node
) {
97 let { parent
} = node
;
100 if (parent
.type
=== "ObjectPattern") {
104 parent
= parent
.parent
;
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
117 function shouldReport(effectiveParent
, name
) {
118 return (!onlyDeclarations
|| DECLARATION_TYPES
.has(effectiveParent
.type
)) &&
119 !ALLOWED_PARENT_TYPES
.has(effectiveParent
.type
) && isInvalid(name
);
123 * Reports an AST node as a rule violation.
124 * @param {ASTNode} node The node to report.
128 function report(node
) {
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:
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.
139 if (!reportedNodes
.has(node
.range
.toString())) {
141 const messageId
= (node
.type
=== "PrivateIdentifier")
142 ? "notMatchPrivate" : "notMatch";
152 reportedNodes
.add(node
.range
.toString());
159 const name
= node
.name
,
160 parent
= node
.parent
,
161 effectiveParent
= (parent
.type
=== "MemberExpression") ? parent
.parent
: parent
;
163 if (parent
.type
=== "MemberExpression") {
165 if (!checkProperties
) {
169 // Always check object names
170 if (parent
.object
.type
=== "Identifier" &&
171 parent
.object
.name
=== name
) {
172 if (isInvalid(name
)) {
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
)) {
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
)) {
192 * Properties have their own rules, and
193 * AssignmentPattern nodes can be treated like Properties:
194 * e.g.: const { no_camelcased = false } = bar;
196 } else if (parent
.type
=== "Property" || parent
.type
=== "AssignmentPattern") {
198 if (parent
.parent
&& parent
.parent
.type
=== "ObjectPattern") {
199 if (!ignoreDestructuring
&& parent
.shorthand
&& parent
.value
.left
&& isInvalid(name
)) {
203 const assignmentKeyEqualsValue
= parent
.key
.name
=== parent
.value
.name
;
205 // prevent checking righthand side of destructured object
206 if (!assignmentKeyEqualsValue
&& parent
.key
=== node
) {
210 const valueIsInvalid
= parent
.value
.name
&& isInvalid(name
);
212 // ignore destructuring if the option is set, unless a new identifier is created
213 if (valueIsInvalid
&& !(assignmentKeyEqualsValue
&& ignoreDestructuring
)) {
218 // never check properties or always ignore destructuring
219 if (!checkProperties
|| (ignoreDestructuring
&& isInsideObjectPattern(node
))) {
223 // don't check right hand side of AssignmentExpression to prevent duplicate warnings
224 if (parent
.right
!== node
&& shouldReport(effectiveParent
, name
)) {
228 // Check if it's an import specifier
229 } else if (IMPORT_TYPES
.has(parent
.type
)) {
231 // Report only if the local imported identifier is invalid
232 if (parent
.local
&& parent
.local
.name
=== node
.name
&& isInvalid(name
)) {
236 } else if (parent
.type
=== "PropertyDefinition") {
238 if (checkClassFields
&& isInvalid(name
)) {
242 // Report anything that is invalid that isn't a CallExpression
243 } else if (shouldReport(effectiveParent
, name
)) {
248 "PrivateIdentifier"(node
) {
250 const isClassField
= node
.parent
.type
=== "PropertyDefinition";
252 if (isClassField
&& !checkClassFields
) {
256 if (isInvalid(node
.name
)) {