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