[d565449] | 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/latest/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 | const sourceCode = context.sourceCode;
|
---|
| 71 | let globalScope;
|
---|
| 72 |
|
---|
| 73 | //--------------------------------------------------------------------------
|
---|
| 74 | // Helpers
|
---|
| 75 | //--------------------------------------------------------------------------
|
---|
| 76 |
|
---|
| 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"]);
|
---|
| 82 |
|
---|
| 83 | /**
|
---|
| 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.
|
---|
| 88 | */
|
---|
| 89 | function isReferenceToGlobalVariable(node) {
|
---|
| 90 | const variable = globalScope.set.get(node.name);
|
---|
| 91 |
|
---|
| 92 | return variable && variable.defs.length === 0 &&
|
---|
| 93 | variable.references.some(ref => ref.identifier === node);
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | /**
|
---|
| 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
|
---|
| 100 | * @private
|
---|
| 101 | */
|
---|
| 102 | function isInvalid(name) {
|
---|
| 103 | return !regexp.test(name);
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | /**
|
---|
| 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
|
---|
| 110 | * @private
|
---|
| 111 | */
|
---|
| 112 | function isInsideObjectPattern(node) {
|
---|
| 113 | let { parent } = node;
|
---|
| 114 |
|
---|
| 115 | while (parent) {
|
---|
| 116 | if (parent.type === "ObjectPattern") {
|
---|
| 117 | return true;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | parent = parent.parent;
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | return false;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | /**
|
---|
| 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
|
---|
| 132 | */
|
---|
| 133 | function shouldReport(effectiveParent, name) {
|
---|
| 134 | return (!onlyDeclarations || DECLARATION_TYPES.has(effectiveParent.type)) &&
|
---|
| 135 | !ALLOWED_PARENT_TYPES.has(effectiveParent.type) && isInvalid(name);
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | /**
|
---|
| 139 | * Reports an AST node as a rule violation.
|
---|
| 140 | * @param {ASTNode} node The node to report.
|
---|
| 141 | * @returns {void}
|
---|
| 142 | * @private
|
---|
| 143 | */
|
---|
| 144 | function report(node) {
|
---|
| 145 |
|
---|
| 146 | /*
|
---|
| 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:
|
---|
| 150 | * { foo }
|
---|
| 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.
|
---|
| 154 | */
|
---|
| 155 | if (!reportedNodes.has(node.range.toString())) {
|
---|
| 156 |
|
---|
| 157 | const messageId = (node.type === "PrivateIdentifier")
|
---|
| 158 | ? "notMatchPrivate" : "notMatch";
|
---|
| 159 |
|
---|
| 160 | context.report({
|
---|
| 161 | node,
|
---|
| 162 | messageId,
|
---|
| 163 | data: {
|
---|
| 164 | name: node.name,
|
---|
| 165 | pattern
|
---|
| 166 | }
|
---|
| 167 | });
|
---|
| 168 | reportedNodes.add(node.range.toString());
|
---|
| 169 | }
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | return {
|
---|
| 173 |
|
---|
| 174 | Program(node) {
|
---|
| 175 | globalScope = sourceCode.getScope(node);
|
---|
| 176 | },
|
---|
| 177 |
|
---|
| 178 | Identifier(node) {
|
---|
| 179 | const name = node.name,
|
---|
| 180 | parent = node.parent,
|
---|
| 181 | effectiveParent = (parent.type === "MemberExpression") ? parent.parent : parent;
|
---|
| 182 |
|
---|
| 183 | if (isReferenceToGlobalVariable(node)) {
|
---|
| 184 | return;
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | if (parent.type === "MemberExpression") {
|
---|
| 188 |
|
---|
| 189 | if (!checkProperties) {
|
---|
| 190 | return;
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | // Always check object names
|
---|
| 194 | if (parent.object.type === "Identifier" &&
|
---|
| 195 | parent.object.name === name) {
|
---|
| 196 | if (isInvalid(name)) {
|
---|
| 197 | report(node);
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 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)) {
|
---|
| 205 | report(node);
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 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)) {
|
---|
| 211 | report(node);
|
---|
| 212 | }
|
---|
| 213 | }
|
---|
| 214 |
|
---|
| 215 | // For https://github.com/eslint/eslint/issues/15123
|
---|
| 216 | } else if (
|
---|
| 217 | parent.type === "Property" &&
|
---|
| 218 | parent.parent.type === "ObjectExpression" &&
|
---|
| 219 | parent.key === node &&
|
---|
| 220 | !parent.computed
|
---|
| 221 | ) {
|
---|
| 222 | if (checkProperties && isInvalid(name)) {
|
---|
| 223 | report(node);
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | /*
|
---|
| 227 | * Properties have their own rules, and
|
---|
| 228 | * AssignmentPattern nodes can be treated like Properties:
|
---|
| 229 | * e.g.: const { no_camelcased = false } = bar;
|
---|
| 230 | */
|
---|
| 231 | } else if (parent.type === "Property" || parent.type === "AssignmentPattern") {
|
---|
| 232 |
|
---|
| 233 | if (parent.parent && parent.parent.type === "ObjectPattern") {
|
---|
| 234 | if (!ignoreDestructuring && parent.shorthand && parent.value.left && isInvalid(name)) {
|
---|
| 235 | report(node);
|
---|
| 236 | }
|
---|
| 237 |
|
---|
| 238 | const assignmentKeyEqualsValue = parent.key.name === parent.value.name;
|
---|
| 239 |
|
---|
| 240 | // prevent checking righthand side of destructured object
|
---|
| 241 | if (!assignmentKeyEqualsValue && parent.key === node) {
|
---|
| 242 | return;
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | const valueIsInvalid = parent.value.name && isInvalid(name);
|
---|
| 246 |
|
---|
| 247 | // ignore destructuring if the option is set, unless a new identifier is created
|
---|
| 248 | if (valueIsInvalid && !(assignmentKeyEqualsValue && ignoreDestructuring)) {
|
---|
| 249 | report(node);
|
---|
| 250 | }
|
---|
| 251 | }
|
---|
| 252 |
|
---|
| 253 | // never check properties or always ignore destructuring
|
---|
| 254 | if ((!checkProperties && !parent.computed) || (ignoreDestructuring && isInsideObjectPattern(node))) {
|
---|
| 255 | return;
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 | // don't check right hand side of AssignmentExpression to prevent duplicate warnings
|
---|
| 259 | if (parent.right !== node && shouldReport(effectiveParent, name)) {
|
---|
| 260 | report(node);
|
---|
| 261 | }
|
---|
| 262 |
|
---|
| 263 | // Check if it's an import specifier
|
---|
| 264 | } else if (IMPORT_TYPES.has(parent.type)) {
|
---|
| 265 |
|
---|
| 266 | // Report only if the local imported identifier is invalid
|
---|
| 267 | if (parent.local && parent.local.name === node.name && isInvalid(name)) {
|
---|
| 268 | report(node);
|
---|
| 269 | }
|
---|
| 270 |
|
---|
| 271 | } else if (parent.type === "PropertyDefinition") {
|
---|
| 272 |
|
---|
| 273 | if (checkClassFields && isInvalid(name)) {
|
---|
| 274 | report(node);
|
---|
| 275 | }
|
---|
| 276 |
|
---|
| 277 | // Report anything that is invalid that isn't a CallExpression
|
---|
| 278 | } else if (shouldReport(effectiveParent, name)) {
|
---|
| 279 | report(node);
|
---|
| 280 | }
|
---|
| 281 | },
|
---|
| 282 |
|
---|
| 283 | "PrivateIdentifier"(node) {
|
---|
| 284 |
|
---|
| 285 | const isClassField = node.parent.type === "PropertyDefinition";
|
---|
| 286 |
|
---|
| 287 | if (isClassField && !checkClassFields) {
|
---|
| 288 | return;
|
---|
| 289 | }
|
---|
| 290 |
|
---|
| 291 | if (isInvalid(node.name)) {
|
---|
| 292 | report(node);
|
---|
| 293 | }
|
---|
| 294 | }
|
---|
| 295 |
|
---|
| 296 | };
|
---|
| 297 |
|
---|
| 298 | }
|
---|
| 299 | };
|
---|