new-cap.js 9.4 KB

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