2 * @fileoverview Rule that warns when identifier names are shorter or longer
3 * than the values provided in configuration.
4 * @author Burak Yigit Kaya aka BYK
9 //------------------------------------------------------------------------------
11 //------------------------------------------------------------------------------
18 description: "enforce minimum and maximum identifier lengths",
19 category: "Stylistic Issues",
21 url: "https://eslint.org/docs/rules/id-length"
50 enum: ["always", "never"]
53 additionalProperties: false
57 tooShort: "Identifier name '{{name}}' is too short (< {{min}}).",
58 tooLong: "Identifier name '{{name}}' is too long (> {{max}})."
63 const options = context.options[0] || {};
64 const minLength = typeof options.min !== "undefined" ? options.min : 2;
65 const maxLength = typeof options.max !== "undefined" ? options.max : Infinity;
66 const properties = options.properties !== "never";
67 const exceptions = new Set(options.exceptions);
68 const exceptionPatterns = (options.exceptionPatterns || []).map(pattern => new RegExp(pattern, "u"));
69 const reportedNode = new Set();
72 * Checks if a string matches the provided exception patterns
73 * @param {string} name The string to check.
74 * @returns {boolean} if the string is a match
77 function matchesExceptionPattern(name) {
78 return exceptionPatterns.some(pattern => pattern.test(name));
81 const SUPPORTED_EXPRESSIONS = {
82 MemberExpression: properties && function(parent) {
83 return !parent.computed && (
85 // regular property assignment
86 (parent.parent.left === parent && parent.parent.type === "AssignmentExpression" ||
88 // or the last identifier in an ObjectPattern destructuring
89 parent.parent.type === "Property" && parent.parent.value === parent &&
90 parent.parent.parent.type === "ObjectPattern" && parent.parent.parent.parent.left === parent.parent.parent)
93 AssignmentPattern(parent, node) {
94 return parent.left === node;
96 VariableDeclarator(parent, node) {
97 return parent.id === node;
99 Property(parent, node) {
101 if (parent.parent.type === "ObjectPattern") {
103 parent.value !== parent.key && parent.value === node ||
104 parent.value === parent.key && parent.key === node && properties
107 return properties && !parent.computed && parent.key === node;
109 ImportDefaultSpecifier: true,
111 FunctionExpression: true,
112 ArrowFunctionExpression: true,
113 ClassDeclaration: true,
114 FunctionDeclaration: true,
115 MethodDefinition: true,
122 const name = node.name;
123 const parent = node.parent;
125 const isShort = name.length < minLength;
126 const isLong = name.length > maxLength;
128 if (!(isShort || isLong) || exceptions.has(name) || matchesExceptionPattern(name)) {
129 return; // Nothing to report
132 const isValidExpression = SUPPORTED_EXPRESSIONS[parent.type];
134 if (isValidExpression && !reportedNode.has(node) && (isValidExpression === true || isValidExpression(parent, node))) {
135 reportedNode.add(node);
138 messageId: isShort ? "tooShort" : "tooLong",
139 data: { name, min: minLength, max: maxLength }