new-cap.js 9.2 KB

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