2 * @fileoverview Rule to flag non-matching identifiers
3 * @author Matthieu Larcher
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
17 description: "require identifiers to match a specified regular expression",
18 category: "Stylistic Issues",
20 url: "https://eslint.org/docs/rules/id-match"
38 ignoreDestructuring: {
43 additionalProperties: false
47 notMatch: "Identifier '{{name}}' does not match the pattern '{{pattern}}'."
53 //--------------------------------------------------------------------------
55 //--------------------------------------------------------------------------
56 const pattern = context.options[0] || "^.+$",
57 regexp = new RegExp(pattern, "u");
59 const options = context.options[1] || {},
60 properties = !!options.properties,
61 onlyDeclarations = !!options.onlyDeclarations,
62 ignoreDestructuring = !!options.ignoreDestructuring;
64 //--------------------------------------------------------------------------
66 //--------------------------------------------------------------------------
68 // contains reported nodes to avoid reporting twice on destructuring with shorthand notation
69 const reported = new Map();
70 const ALLOWED_PARENT_TYPES = new Set(["CallExpression", "NewExpression"]);
71 const DECLARATION_TYPES = new Set(["FunctionDeclaration", "VariableDeclarator"]);
72 const IMPORT_TYPES = new Set(["ImportSpecifier", "ImportNamespaceSpecifier", "ImportDefaultSpecifier"]);
75 * Checks if a string matches the provided pattern
76 * @param {string} name The string to check.
77 * @returns {boolean} if the string is a match
80 function isInvalid(name) {
81 return !regexp.test(name);
85 * Checks if a parent of a node is an ObjectPattern.
86 * @param {ASTNode} node The node to check.
87 * @returns {boolean} if the node is inside an ObjectPattern
90 function isInsideObjectPattern(node) {
91 let { parent } = node;
94 if (parent.type === "ObjectPattern") {
98 parent = parent.parent;
105 * Verifies if we should report an error or not based on the effective
106 * parent node and the identifier name.
107 * @param {ASTNode} effectiveParent The effective parent node of the node to be reported
108 * @param {string} name The identifier name of the identifier node
109 * @returns {boolean} whether an error should be reported or not
111 function shouldReport(effectiveParent, name) {
112 return (!onlyDeclarations || DECLARATION_TYPES.has(effectiveParent.type)) &&
113 !ALLOWED_PARENT_TYPES.has(effectiveParent.type) && isInvalid(name);
117 * Reports an AST node as a rule violation.
118 * @param {ASTNode} node The node to report.
122 function report(node) {
123 if (!reported.has(node)) {
126 messageId: "notMatch",
132 reported.set(node, true);
139 const name = node.name,
140 parent = node.parent,
141 effectiveParent = (parent.type === "MemberExpression") ? parent.parent : parent;
143 if (parent.type === "MemberExpression") {
149 // Always check object names
150 if (parent.object.type === "Identifier" &&
151 parent.object.name === name) {
152 if (isInvalid(name)) {
156 // Report AssignmentExpressions left side's assigned variable id
157 } else if (effectiveParent.type === "AssignmentExpression" &&
158 effectiveParent.left.type === "MemberExpression" &&
159 effectiveParent.left.property.name === node.name) {
160 if (isInvalid(name)) {
164 // Report AssignmentExpressions only if they are the left side of the assignment
165 } else if (effectiveParent.type === "AssignmentExpression" && effectiveParent.right.type !== "MemberExpression") {
166 if (isInvalid(name)) {
172 * Properties have their own rules, and
173 * AssignmentPattern nodes can be treated like Properties:
174 * e.g.: const { no_camelcased = false } = bar;
176 } else if (parent.type === "Property" || parent.type === "AssignmentPattern") {
178 if (parent.parent && parent.parent.type === "ObjectPattern") {
179 if (parent.shorthand && parent.value.left && isInvalid(name)) {
184 const assignmentKeyEqualsValue = parent.key.name === parent.value.name;
186 // prevent checking righthand side of destructured object
187 if (!assignmentKeyEqualsValue && parent.key === node) {
191 const valueIsInvalid = parent.value.name && isInvalid(name);
193 // ignore destructuring if the option is set, unless a new identifier is created
194 if (valueIsInvalid && !(assignmentKeyEqualsValue && ignoreDestructuring)) {
199 // never check properties or always ignore destructuring
200 if (!properties || (ignoreDestructuring && isInsideObjectPattern(node))) {
204 // don't check right hand side of AssignmentExpression to prevent duplicate warnings
205 if (parent.right !== node && shouldReport(effectiveParent, name)) {
209 // Check if it's an import specifier
210 } else if (IMPORT_TYPES.has(parent.type)) {
212 // Report only if the local imported identifier is invalid
213 if (parent.local && parent.local.name === node.name && isInvalid(name)) {
217 // Report anything that is invalid that isn't a CallExpression
218 } else if (shouldReport(effectiveParent, name)) {