Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

new-cap.js 9.2KB

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. //------------------------------------------------------------------------------
  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. ];
  24. /**
  25. * Ensure that if the key is provided, it must be an array.
  26. * @param {Object} obj Object to check with `key`.
  27. * @param {string} key Object key to check on `obj`.
  28. * @param {*} fallback If obj[key] is not present, this will be returned.
  29. * @returns {string[]} Returns obj[key] if it's an Array, otherwise `fallback`
  30. */
  31. function checkArray(obj, key, fallback) {
  32. /* istanbul ignore if */
  33. if (Object.prototype.hasOwnProperty.call(obj, key) && !Array.isArray(obj[key])) {
  34. throw new TypeError(`${key}, if provided, must be an Array`);
  35. }
  36. return obj[key] || fallback;
  37. }
  38. /**
  39. * A reducer function to invert an array to an Object mapping the string form of the key, to `true`.
  40. * @param {Object} map Accumulator object for the reduce.
  41. * @param {string} key Object key to set to `true`.
  42. * @returns {Object} Returns the updated Object for further reduction.
  43. */
  44. function invert(map, key) {
  45. map[key] = true;
  46. return map;
  47. }
  48. /**
  49. * Creates an object with the cap is new exceptions as its keys and true as their values.
  50. * @param {Object} config Rule configuration
  51. * @returns {Object} Object with cap is new exceptions.
  52. */
  53. function calculateCapIsNewExceptions(config) {
  54. let capIsNewExceptions = checkArray(config, "capIsNewExceptions", CAPS_ALLOWED);
  55. if (capIsNewExceptions !== CAPS_ALLOWED) {
  56. capIsNewExceptions = capIsNewExceptions.concat(CAPS_ALLOWED);
  57. }
  58. return capIsNewExceptions.reduce(invert, {});
  59. }
  60. //------------------------------------------------------------------------------
  61. // Rule Definition
  62. //------------------------------------------------------------------------------
  63. module.exports = {
  64. meta: {
  65. type: "suggestion",
  66. docs: {
  67. description: "require constructor names to begin with a capital letter",
  68. category: "Stylistic Issues",
  69. recommended: false,
  70. url: "https://eslint.org/docs/rules/new-cap"
  71. },
  72. schema: [
  73. {
  74. type: "object",
  75. properties: {
  76. newIsCap: {
  77. type: "boolean"
  78. },
  79. capIsNew: {
  80. type: "boolean"
  81. },
  82. newIsCapExceptions: {
  83. type: "array",
  84. items: {
  85. type: "string"
  86. }
  87. },
  88. newIsCapExceptionPattern: {
  89. type: "string"
  90. },
  91. capIsNewExceptions: {
  92. type: "array",
  93. items: {
  94. type: "string"
  95. }
  96. },
  97. capIsNewExceptionPattern: {
  98. type: "string"
  99. },
  100. properties: {
  101. type: "boolean"
  102. }
  103. },
  104. additionalProperties: false
  105. }
  106. ]
  107. },
  108. create(context) {
  109. const config = context.options[0] ? Object.assign({}, context.options[0]) : {};
  110. config.newIsCap = config.newIsCap !== false;
  111. config.capIsNew = config.capIsNew !== false;
  112. const skipProperties = config.properties === false;
  113. const newIsCapExceptions = checkArray(config, "newIsCapExceptions", []).reduce(invert, {});
  114. const newIsCapExceptionPattern = config.newIsCapExceptionPattern ? new RegExp(config.newIsCapExceptionPattern) : null;
  115. const capIsNewExceptions = calculateCapIsNewExceptions(config);
  116. const capIsNewExceptionPattern = config.capIsNewExceptionPattern ? new RegExp(config.capIsNewExceptionPattern) : null;
  117. const listeners = {};
  118. const sourceCode = context.getSourceCode();
  119. //--------------------------------------------------------------------------
  120. // Helpers
  121. //--------------------------------------------------------------------------
  122. /**
  123. * Get exact callee name from expression
  124. * @param {ASTNode} node CallExpression or NewExpression node
  125. * @returns {string} name
  126. */
  127. function extractNameFromExpression(node) {
  128. let name = "";
  129. if (node.callee.type === "MemberExpression") {
  130. const property = node.callee.property;
  131. if (property.type === "Literal" && (typeof property.value === "string")) {
  132. name = property.value;
  133. } else if (property.type === "Identifier" && !node.callee.computed) {
  134. name = property.name;
  135. }
  136. } else {
  137. name = node.callee.name;
  138. }
  139. return name;
  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. if (calleeName === "UTC" && node.callee.type === "MemberExpression") {
  177. // allow if callee is Date.UTC
  178. return node.callee.object.type === "Identifier" &&
  179. node.callee.object.name === "Date";
  180. }
  181. return skipProperties && node.callee.type === "MemberExpression";
  182. }
  183. /**
  184. * Reports the given message for the given node. The location will be the start of the property or the callee.
  185. * @param {ASTNode} node CallExpression or NewExpression node.
  186. * @param {string} message The message to report.
  187. * @returns {void}
  188. */
  189. function report(node, message) {
  190. let callee = node.callee;
  191. if (callee.type === "MemberExpression") {
  192. callee = callee.property;
  193. }
  194. context.report({ node, loc: callee.loc.start, message });
  195. }
  196. //--------------------------------------------------------------------------
  197. // Public
  198. //--------------------------------------------------------------------------
  199. if (config.newIsCap) {
  200. listeners.NewExpression = function(node) {
  201. const constructorName = extractNameFromExpression(node);
  202. if (constructorName) {
  203. const capitalization = getCap(constructorName);
  204. const isAllowed = capitalization !== "lower" || isCapAllowed(newIsCapExceptions, node, constructorName, newIsCapExceptionPattern);
  205. if (!isAllowed) {
  206. report(node, "A constructor name should not start with a lowercase letter.");
  207. }
  208. }
  209. };
  210. }
  211. if (config.capIsNew) {
  212. listeners.CallExpression = function(node) {
  213. const calleeName = extractNameFromExpression(node);
  214. if (calleeName) {
  215. const capitalization = getCap(calleeName);
  216. const isAllowed = capitalization !== "upper" || isCapAllowed(capIsNewExceptions, node, calleeName, capIsNewExceptionPattern);
  217. if (!isAllowed) {
  218. report(node, "A function with a name starting with an uppercase letter should only be used as a constructor.");
  219. }
  220. }
  221. };
  222. }
  223. return listeners;
  224. }
  225. };