new-cap.js 7.0 KB

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