new-cap.js 9.3 KB

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