]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/id-length.js
a68873ac06289b0b3c28b67a04d0ee064c2756c4
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"
43 enum: ["always", "never"]
46 additionalProperties
: false
50 tooShort
: "Identifier name '{{name}}' is too short (< {{min}}).",
51 tooLong
: "Identifier name '{{name}}' is too long (> {{max}})."
56 const options
= context
.options
[0] || {};
57 const minLength
= typeof options
.min
!== "undefined" ? options
.min
: 2;
58 const maxLength
= typeof options
.max
!== "undefined" ? options
.max
: Infinity
;
59 const properties
= options
.properties
!== "never";
60 const exceptions
= (options
.exceptions
? options
.exceptions
: [])
61 .reduce((obj
, item
) => {
66 const reportedNode
= new Set();
68 const SUPPORTED_EXPRESSIONS
= {
69 MemberExpression
: properties
&& function(parent
) {
70 return !parent
.computed
&& (
72 // regular property assignment
73 (parent
.parent
.left
=== parent
&& parent
.parent
.type
=== "AssignmentExpression" ||
75 // or the last identifier in an ObjectPattern destructuring
76 parent
.parent
.type
=== "Property" && parent
.parent
.value
=== parent
&&
77 parent
.parent
.parent
.type
=== "ObjectPattern" && parent
.parent
.parent
.parent
.left
=== parent
.parent
.parent
)
80 AssignmentPattern(parent
, node
) {
81 return parent
.left
=== node
;
83 VariableDeclarator(parent
, node
) {
84 return parent
.id
=== node
;
86 Property(parent
, node
) {
88 if (parent
.parent
.type
=== "ObjectPattern") {
90 parent
.value
!== parent
.key
&& parent
.value
=== node
||
91 parent
.value
=== parent
.key
&& parent
.key
=== node
&& properties
94 return properties
&& !parent
.computed
&& parent
.key
=== node
;
96 ImportDefaultSpecifier
: true,
98 FunctionExpression
: true,
99 ArrowFunctionExpression
: true,
100 ClassDeclaration
: true,
101 FunctionDeclaration
: true,
102 MethodDefinition
: true,
109 const name
= node
.name
;
110 const parent
= node
.parent
;
112 const isShort
= name
.length
< minLength
;
113 const isLong
= name
.length
> maxLength
;
115 if (!(isShort
|| isLong
) || exceptions
[name
]) {
116 return; // Nothing to report
119 const isValidExpression
= SUPPORTED_EXPRESSIONS
[parent
.type
];
121 if (isValidExpression
&& !reportedNode
.has(node
) && (isValidExpression
=== true || isValidExpression(parent
, node
))) {
122 reportedNode
.add(node
);
125 messageId
: isShort
? "tooShort" : "tooLong",
126 data
: { name
, min
: minLength
, max
: maxLength
}