1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | "use strict";
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | module.exports = {
|
14 | meta: {
|
15 | docs: {
|
16 | description: "enforce minimum and maximum identifier lengths",
|
17 | category: "Stylistic Issues",
|
18 | recommended: false,
|
19 | url: "https://eslint.org/docs/rules/id-length"
|
20 | },
|
21 |
|
22 | schema: [
|
23 | {
|
24 | type: "object",
|
25 | properties: {
|
26 | min: {
|
27 | type: "number"
|
28 | },
|
29 | max: {
|
30 | type: "number"
|
31 | },
|
32 | exceptions: {
|
33 | type: "array",
|
34 | uniqueItems: true,
|
35 | items: {
|
36 | type: "string"
|
37 | }
|
38 | },
|
39 | properties: {
|
40 | enum: ["always", "never"]
|
41 | }
|
42 | },
|
43 | additionalProperties: false
|
44 | }
|
45 | ]
|
46 | },
|
47 |
|
48 | create(context) {
|
49 | const options = context.options[0] || {};
|
50 | const minLength = typeof options.min !== "undefined" ? options.min : 2;
|
51 | const maxLength = typeof options.max !== "undefined" ? options.max : Infinity;
|
52 | const properties = options.properties !== "never";
|
53 | const exceptions = (options.exceptions ? options.exceptions : [])
|
54 | .reduce((obj, item) => {
|
55 | obj[item] = true;
|
56 |
|
57 | return obj;
|
58 | }, {});
|
59 |
|
60 | const SUPPORTED_EXPRESSIONS = {
|
61 | MemberExpression: properties && function(parent) {
|
62 | return !parent.computed && (
|
63 |
|
64 |
|
65 | (parent.parent.left === parent && parent.parent.type === "AssignmentExpression" ||
|
66 |
|
67 |
|
68 | parent.parent.type === "Property" && parent.parent.value === parent &&
|
69 | parent.parent.parent.type === "ObjectPattern" && parent.parent.parent.parent.left === parent.parent.parent)
|
70 | );
|
71 | },
|
72 | AssignmentPattern(parent, node) {
|
73 | return parent.left === node;
|
74 | },
|
75 | VariableDeclarator(parent, node) {
|
76 | return parent.id === node;
|
77 | },
|
78 | Property: properties && function(parent, node) {
|
79 | return parent.key === node;
|
80 | },
|
81 | ImportDefaultSpecifier: true,
|
82 | RestElement: true,
|
83 | FunctionExpression: true,
|
84 | ArrowFunctionExpression: true,
|
85 | ClassDeclaration: true,
|
86 | FunctionDeclaration: true,
|
87 | MethodDefinition: true,
|
88 | CatchClause: true
|
89 | };
|
90 |
|
91 | return {
|
92 | Identifier(node) {
|
93 | const name = node.name;
|
94 | const parent = node.parent;
|
95 |
|
96 | const isShort = name.length < minLength;
|
97 | const isLong = name.length > maxLength;
|
98 |
|
99 | if (!(isShort || isLong) || exceptions[name]) {
|
100 | return;
|
101 | }
|
102 |
|
103 | const isValidExpression = SUPPORTED_EXPRESSIONS[parent.type];
|
104 |
|
105 | if (isValidExpression && (isValidExpression === true || isValidExpression(parent, node))) {
|
106 | context.report({
|
107 | node,
|
108 | message: isShort
|
109 | ? "Identifier name '{{name}}' is too short (< {{min}})."
|
110 | : "Identifier name '{{name}}' is too long (> {{max}}).",
|
111 | data: { name, min: minLength, max: maxLength }
|
112 | });
|
113 | }
|
114 | }
|
115 | };
|
116 | }
|
117 | };
|