]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/id-length.js
3701c66e347a7287321da25d25d743ca7ff9305f
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 //------------------------------------------------------------------------------
13 /** @type {import('../shared/types').Rule} */
19 description
: "enforce minimum and maximum identifier lengths",
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 tooShortPrivate
: "Identifier name '#{{name}}' is too short (< {{min}}).",
59 tooLong
: "Identifier name '{{name}}' is too long (> {{max}}).",
60 tooLongPrivate
: "Identifier name #'{{name}}' is too long (> {{max}})."
65 const options
= context
.options
[0] || {};
66 const minLength
= typeof options
.min
!== "undefined" ? options
.min
: 2;
67 const maxLength
= typeof options
.max
!== "undefined" ? options
.max
: Infinity
;
68 const properties
= options
.properties
!== "never";
69 const exceptions
= new Set(options
.exceptions
);
70 const exceptionPatterns
= (options
.exceptionPatterns
|| []).map(pattern
=> new RegExp(pattern
, "u"));
71 const reportedNodes
= new Set();
74 * Checks if a string matches the provided exception patterns
75 * @param {string} name The string to check.
76 * @returns {boolean} if the string is a match
79 function matchesExceptionPattern(name
) {
80 return exceptionPatterns
.some(pattern
=> pattern
.test(name
));
83 const SUPPORTED_EXPRESSIONS
= {
84 MemberExpression
: properties
&& function(parent
) {
85 return !parent
.computed
&& (
87 // regular property assignment
88 (parent
.parent
.left
=== parent
&& parent
.parent
.type
=== "AssignmentExpression" ||
90 // or the last identifier in an ObjectPattern destructuring
91 parent
.parent
.type
=== "Property" && parent
.parent
.value
=== parent
&&
92 parent
.parent
.parent
.type
=== "ObjectPattern" && parent
.parent
.parent
.parent
.left
=== parent
.parent
.parent
)
95 AssignmentPattern(parent
, node
) {
96 return parent
.left
=== node
;
98 VariableDeclarator(parent
, node
) {
99 return parent
.id
=== node
;
101 Property(parent
, node
) {
103 if (parent
.parent
.type
=== "ObjectPattern") {
104 const isKeyAndValueSame
= parent
.value
.name
=== parent
.key
.name
;
107 !isKeyAndValueSame
&& parent
.value
=== node
||
108 isKeyAndValueSame
&& parent
.key
=== node
&& properties
111 return properties
&& !parent
.computed
&& parent
.key
.name
=== node
.name
;
113 ImportDefaultSpecifier
: true,
115 FunctionExpression
: true,
116 ArrowFunctionExpression
: true,
117 ClassDeclaration
: true,
118 FunctionDeclaration
: true,
119 MethodDefinition
: true,
120 PropertyDefinition
: true,
130 const name
= node
.name
;
131 const parent
= node
.parent
;
133 const isShort
= name
.length
< minLength
;
134 const isLong
= name
.length
> maxLength
;
136 if (!(isShort
|| isLong
) || exceptions
.has(name
) || matchesExceptionPattern(name
)) {
137 return; // Nothing to report
140 const isValidExpression
= SUPPORTED_EXPRESSIONS
[parent
.type
];
143 * We used the range instead of the node because it's possible
144 * for the same identifier to be represented by two different
145 * nodes, with the most clear example being shorthand properties:
147 * In this case, "foo" is represented by one node for the name
148 * and one for the value. The only way to know they are the same
149 * is to look at the range.
151 if (isValidExpression
&& !reportedNodes
.has(node
.range
.toString()) && (isValidExpression
=== true || isValidExpression(parent
, node
))) {
152 reportedNodes
.add(node
.range
.toString());
154 let messageId
= isShort
? "tooShort" : "tooLong";
156 if (node
.type
=== "PrivateIdentifier") {
157 messageId
+= "Private";
163 data
: { name
, min
: minLength
, max
: maxLength
}