]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | /** |
2 | * @fileoverview Rule to flag use of constructors without capital letters | |
3 | * @author Nicholas C. Zakas | |
4 | */ | |
5 | ||
6 | "use strict"; | |
7 | ||
8 | //------------------------------------------------------------------------------ | |
9 | // Requirements | |
10 | //------------------------------------------------------------------------------ | |
11 | ||
12 | const astUtils = require("./utils/ast-utils"); | |
13 | ||
14 | //------------------------------------------------------------------------------ | |
15 | // Helpers | |
16 | //------------------------------------------------------------------------------ | |
17 | ||
18 | const CAPS_ALLOWED = [ | |
19 | "Array", | |
20 | "Boolean", | |
21 | "Date", | |
22 | "Error", | |
23 | "Function", | |
24 | "Number", | |
25 | "Object", | |
26 | "RegExp", | |
27 | "String", | |
28 | "Symbol", | |
29 | "BigInt" | |
30 | ]; | |
31 | ||
32 | /** | |
33 | * Ensure that if the key is provided, it must be an array. | |
34 | * @param {Object} obj Object to check with `key`. | |
35 | * @param {string} key Object key to check on `obj`. | |
609c276f TL |
36 | * @param {any} fallback If obj[key] is not present, this will be returned. |
37 | * @throws {TypeError} If key is not an own array type property of `obj`. | |
eb39fafa DC |
38 | * @returns {string[]} Returns obj[key] if it's an Array, otherwise `fallback` |
39 | */ | |
40 | function checkArray(obj, key, fallback) { | |
41 | ||
42 | /* istanbul ignore if */ | |
43 | if (Object.prototype.hasOwnProperty.call(obj, key) && !Array.isArray(obj[key])) { | |
44 | throw new TypeError(`${key}, if provided, must be an Array`); | |
45 | } | |
46 | return obj[key] || fallback; | |
47 | } | |
48 | ||
49 | /** | |
50 | * A reducer function to invert an array to an Object mapping the string form of the key, to `true`. | |
51 | * @param {Object} map Accumulator object for the reduce. | |
52 | * @param {string} key Object key to set to `true`. | |
53 | * @returns {Object} Returns the updated Object for further reduction. | |
54 | */ | |
55 | function invert(map, key) { | |
56 | map[key] = true; | |
57 | return map; | |
58 | } | |
59 | ||
60 | /** | |
61 | * Creates an object with the cap is new exceptions as its keys and true as their values. | |
62 | * @param {Object} config Rule configuration | |
63 | * @returns {Object} Object with cap is new exceptions. | |
64 | */ | |
65 | function calculateCapIsNewExceptions(config) { | |
66 | let capIsNewExceptions = checkArray(config, "capIsNewExceptions", CAPS_ALLOWED); | |
67 | ||
68 | if (capIsNewExceptions !== CAPS_ALLOWED) { | |
69 | capIsNewExceptions = capIsNewExceptions.concat(CAPS_ALLOWED); | |
70 | } | |
71 | ||
72 | return capIsNewExceptions.reduce(invert, {}); | |
73 | } | |
74 | ||
75 | //------------------------------------------------------------------------------ | |
76 | // Rule Definition | |
77 | //------------------------------------------------------------------------------ | |
78 | ||
79 | module.exports = { | |
80 | meta: { | |
81 | type: "suggestion", | |
82 | ||
83 | docs: { | |
84 | description: "require constructor names to begin with a capital letter", | |
eb39fafa DC |
85 | recommended: false, |
86 | url: "https://eslint.org/docs/rules/new-cap" | |
87 | }, | |
88 | ||
89 | schema: [ | |
90 | { | |
91 | type: "object", | |
92 | properties: { | |
93 | newIsCap: { | |
94 | type: "boolean", | |
95 | default: true | |
96 | }, | |
97 | capIsNew: { | |
98 | type: "boolean", | |
99 | default: true | |
100 | }, | |
101 | newIsCapExceptions: { | |
102 | type: "array", | |
103 | items: { | |
104 | type: "string" | |
105 | } | |
106 | }, | |
107 | newIsCapExceptionPattern: { | |
108 | type: "string" | |
109 | }, | |
110 | capIsNewExceptions: { | |
111 | type: "array", | |
112 | items: { | |
113 | type: "string" | |
114 | } | |
115 | }, | |
116 | capIsNewExceptionPattern: { | |
117 | type: "string" | |
118 | }, | |
119 | properties: { | |
120 | type: "boolean", | |
121 | default: true | |
122 | } | |
123 | }, | |
124 | additionalProperties: false | |
125 | } | |
126 | ], | |
127 | messages: { | |
128 | upper: "A function with a name starting with an uppercase letter should only be used as a constructor.", | |
129 | lower: "A constructor name should not start with a lowercase letter." | |
130 | } | |
131 | }, | |
132 | ||
133 | create(context) { | |
134 | ||
135 | const config = Object.assign({}, context.options[0]); | |
136 | ||
137 | config.newIsCap = config.newIsCap !== false; | |
138 | config.capIsNew = config.capIsNew !== false; | |
139 | const skipProperties = config.properties === false; | |
140 | ||
141 | const newIsCapExceptions = checkArray(config, "newIsCapExceptions", []).reduce(invert, {}); | |
142 | const newIsCapExceptionPattern = config.newIsCapExceptionPattern ? new RegExp(config.newIsCapExceptionPattern, "u") : null; | |
143 | ||
144 | const capIsNewExceptions = calculateCapIsNewExceptions(config); | |
145 | const capIsNewExceptionPattern = config.capIsNewExceptionPattern ? new RegExp(config.capIsNewExceptionPattern, "u") : null; | |
146 | ||
147 | const listeners = {}; | |
148 | ||
149 | const sourceCode = context.getSourceCode(); | |
150 | ||
151 | //-------------------------------------------------------------------------- | |
152 | // Helpers | |
153 | //-------------------------------------------------------------------------- | |
154 | ||
155 | /** | |
156 | * Get exact callee name from expression | |
157 | * @param {ASTNode} node CallExpression or NewExpression node | |
158 | * @returns {string} name | |
159 | */ | |
160 | function extractNameFromExpression(node) { | |
6f036462 TL |
161 | return node.callee.type === "Identifier" |
162 | ? node.callee.name | |
163 | : astUtils.getStaticPropertyName(node.callee) || ""; | |
eb39fafa DC |
164 | } |
165 | ||
166 | /** | |
167 | * Returns the capitalization state of the string - | |
168 | * Whether the first character is uppercase, lowercase, or non-alphabetic | |
169 | * @param {string} str String | |
170 | * @returns {string} capitalization state: "non-alpha", "lower", or "upper" | |
171 | */ | |
172 | function getCap(str) { | |
173 | const firstChar = str.charAt(0); | |
174 | ||
175 | const firstCharLower = firstChar.toLowerCase(); | |
176 | const firstCharUpper = firstChar.toUpperCase(); | |
177 | ||
178 | if (firstCharLower === firstCharUpper) { | |
179 | ||
180 | // char has no uppercase variant, so it's non-alphabetic | |
181 | return "non-alpha"; | |
182 | } | |
183 | if (firstChar === firstCharLower) { | |
184 | return "lower"; | |
185 | } | |
186 | return "upper"; | |
187 | ||
188 | } | |
189 | ||
190 | /** | |
191 | * Check if capitalization is allowed for a CallExpression | |
192 | * @param {Object} allowedMap Object mapping calleeName to a Boolean | |
193 | * @param {ASTNode} node CallExpression node | |
194 | * @param {string} calleeName Capitalized callee name from a CallExpression | |
195 | * @param {Object} pattern RegExp object from options pattern | |
196 | * @returns {boolean} Returns true if the callee may be capitalized | |
197 | */ | |
198 | function isCapAllowed(allowedMap, node, calleeName, pattern) { | |
199 | const sourceText = sourceCode.getText(node.callee); | |
200 | ||
201 | if (allowedMap[calleeName] || allowedMap[sourceText]) { | |
202 | return true; | |
203 | } | |
204 | ||
205 | if (pattern && pattern.test(sourceText)) { | |
206 | return true; | |
207 | } | |
208 | ||
6f036462 TL |
209 | const callee = astUtils.skipChainExpression(node.callee); |
210 | ||
211 | if (calleeName === "UTC" && callee.type === "MemberExpression") { | |
eb39fafa DC |
212 | |
213 | // allow if callee is Date.UTC | |
6f036462 TL |
214 | return callee.object.type === "Identifier" && |
215 | callee.object.name === "Date"; | |
eb39fafa DC |
216 | } |
217 | ||
6f036462 | 218 | return skipProperties && callee.type === "MemberExpression"; |
eb39fafa DC |
219 | } |
220 | ||
221 | /** | |
222 | * Reports the given messageId for the given node. The location will be the start of the property or the callee. | |
223 | * @param {ASTNode} node CallExpression or NewExpression node. | |
224 | * @param {string} messageId The messageId to report. | |
225 | * @returns {void} | |
226 | */ | |
227 | function report(node, messageId) { | |
6f036462 | 228 | let callee = astUtils.skipChainExpression(node.callee); |
eb39fafa DC |
229 | |
230 | if (callee.type === "MemberExpression") { | |
231 | callee = callee.property; | |
232 | } | |
233 | ||
56c4a2cb | 234 | context.report({ node, loc: callee.loc, messageId }); |
eb39fafa DC |
235 | } |
236 | ||
237 | //-------------------------------------------------------------------------- | |
238 | // Public | |
239 | //-------------------------------------------------------------------------- | |
240 | ||
241 | if (config.newIsCap) { | |
242 | listeners.NewExpression = function(node) { | |
243 | ||
244 | const constructorName = extractNameFromExpression(node); | |
245 | ||
246 | if (constructorName) { | |
247 | const capitalization = getCap(constructorName); | |
248 | const isAllowed = capitalization !== "lower" || isCapAllowed(newIsCapExceptions, node, constructorName, newIsCapExceptionPattern); | |
249 | ||
250 | if (!isAllowed) { | |
251 | report(node, "lower"); | |
252 | } | |
253 | } | |
254 | }; | |
255 | } | |
256 | ||
257 | if (config.capIsNew) { | |
258 | listeners.CallExpression = function(node) { | |
259 | ||
260 | const calleeName = extractNameFromExpression(node); | |
261 | ||
262 | if (calleeName) { | |
263 | const capitalization = getCap(calleeName); | |
264 | const isAllowed = capitalization !== "upper" || isCapAllowed(capIsNewExceptions, node, calleeName, capIsNewExceptionPattern); | |
265 | ||
266 | if (!isAllowed) { | |
267 | report(node, "upper"); | |
268 | } | |
269 | } | |
270 | }; | |
271 | } | |
272 | ||
273 | return listeners; | |
274 | } | |
275 | }; |