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: {
46 notMatch: "Identifier '{{name}}' does not match the pattern '{{pattern}}'."
52 //--------------------------------------------------------------------------
54 //--------------------------------------------------------------------------
55 const pattern = context.options[0] || "^.+$",
56 regexp = new RegExp(pattern, "u");
58 const options = context.options[1] || {},
59 properties = !!options.properties,
60 onlyDeclarations = !!options.onlyDeclarations,
61 ignoreDestructuring = !!options.ignoreDestructuring;
63 //--------------------------------------------------------------------------
65 //--------------------------------------------------------------------------
67 // contains reported nodes to avoid reporting twice on destructuring with shorthand notation
68 const reported = new Map();
69 const ALLOWED_PARENT_TYPES = new Set(["CallExpression", "NewExpression"]);
70 const DECLARATION_TYPES = new Set(["FunctionDeclaration", "VariableDeclarator"]);
71 const IMPORT_TYPES = new Set(["ImportSpecifier", "ImportNamespaceSpecifier", "ImportDefaultSpecifier"]);
74 * Checks if a string matches the provided pattern
75 * @param {string} name The string to check.
76 * @returns {boolean} if the string is a match
79 function isInvalid(name) {
80 return !regexp.test(name);
84 * Checks if a parent of a node is an ObjectPattern.
85 * @param {ASTNode} node The node to check.
86 * @returns {boolean} if the node is inside an ObjectPattern
89 function isInsideObjectPattern(node) {
90 let { parent } = node;
93 if (parent.type === "ObjectPattern") {
97 parent = parent.parent;
104 * Verifies if we should report an error or not based on the effective
105 * parent node and the identifier name.
106 * @param {ASTNode} effectiveParent The effective parent node of the node to be reported
107 * @param {string} name The identifier name of the identifier node
108 * @returns {boolean} whether an error should be reported or not
110 function shouldReport(effectiveParent, name) {
111 return (!onlyDeclarations || DECLARATION_TYPES.has(effectiveParent.type)) &&
112 !ALLOWED_PARENT_TYPES.has(effectiveParent.type) && isInvalid(name);
116 * Reports an AST node as a rule violation.
117 * @param {ASTNode} node The node to report.
121 function report(node) {
122 if (!reported.has(node)) {
125 messageId: "notMatch",
131 reported.set(node, true);
138 const name = node.name,
139 parent = node.parent,
140 effectiveParent = (parent.type === "MemberExpression") ? parent.parent : parent;
142 if (parent.type === "MemberExpression") {
148 // Always check object names
149 if (parent.object.type === "Identifier" &&
150 parent.object.name === name) {
151 if (isInvalid(name)) {
155 // Report AssignmentExpressions left side's assigned variable id
156 } else if (effectiveParent.type === "AssignmentExpression" &&
157 effectiveParent.left.type === "MemberExpression" &&
158 effectiveParent.left.property.name === node.name) {
159 if (isInvalid(name)) {
163 // Report AssignmentExpressions only if they are the left side of the assignment
164 } else if (effectiveParent.type === "AssignmentExpression" && effectiveParent.right.type !== "MemberExpression") {
165 if (isInvalid(name)) {
171 * Properties have their own rules, and
172 * AssignmentPattern nodes can be treated like Properties:
173 * e.g.: const { no_camelcased = false } = bar;
175 } else if (parent.type === "Property" || parent.type === "AssignmentPattern") {
177 if (parent.parent && parent.parent.type === "ObjectPattern") {
178 if (parent.shorthand && parent.value.left && isInvalid(name)) {
183 const assignmentKeyEqualsValue = parent.key.name === parent.value.name;
185 // prevent checking righthand side of destructured object
186 if (!assignmentKeyEqualsValue && parent.key === node) {
190 const valueIsInvalid = parent.value.name && isInvalid(name);
192 // ignore destructuring if the option is set, unless a new identifier is created
193 if (valueIsInvalid && !(assignmentKeyEqualsValue && ignoreDestructuring)) {
198 // never check properties or always ignore destructuring
199 if (!properties || (ignoreDestructuring && isInsideObjectPattern(node))) {
203 // don't check right hand side of AssignmentExpression to prevent duplicate warnings
204 if (parent.right !== node && shouldReport(effectiveParent, name)) {
208 // Check if it's an import specifier
209 } else if (IMPORT_TYPES.has(parent.type)) {
211 // Report only if the local imported identifier is invalid
212 if (parent.local && parent.local.name === node.name && isInvalid(name)) {
216 // Report anything that is invalid that isn't a CallExpression
217 } else if (shouldReport(effectiveParent, name)) {