项目原始demo,不改动
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
Questo repository è archiviato. Puoi vedere i file e clonarli, ma non puoi effettuare richieste di pushj o aprire problemi/richieste di pull.
 
 
 
 

274 righe
9.2 KiB

  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. docs: {
  66. description: "require constructor names to begin with a capital letter",
  67. category: "Stylistic Issues",
  68. recommended: false,
  69. url: "https://eslint.org/docs/rules/new-cap"
  70. },
  71. schema: [
  72. {
  73. type: "object",
  74. properties: {
  75. newIsCap: {
  76. type: "boolean"
  77. },
  78. capIsNew: {
  79. type: "boolean"
  80. },
  81. newIsCapExceptions: {
  82. type: "array",
  83. items: {
  84. type: "string"
  85. }
  86. },
  87. newIsCapExceptionPattern: {
  88. type: "string"
  89. },
  90. capIsNewExceptions: {
  91. type: "array",
  92. items: {
  93. type: "string"
  94. }
  95. },
  96. capIsNewExceptionPattern: {
  97. type: "string"
  98. },
  99. properties: {
  100. type: "boolean"
  101. }
  102. },
  103. additionalProperties: false
  104. }
  105. ]
  106. },
  107. create(context) {
  108. const config = context.options[0] ? Object.assign({}, context.options[0]) : {};
  109. config.newIsCap = config.newIsCap !== false;
  110. config.capIsNew = config.capIsNew !== false;
  111. const skipProperties = config.properties === false;
  112. const newIsCapExceptions = checkArray(config, "newIsCapExceptions", []).reduce(invert, {});
  113. const newIsCapExceptionPattern = config.newIsCapExceptionPattern ? new RegExp(config.newIsCapExceptionPattern) : null;
  114. const capIsNewExceptions = calculateCapIsNewExceptions(config);
  115. const capIsNewExceptionPattern = config.capIsNewExceptionPattern ? new RegExp(config.capIsNewExceptionPattern) : null;
  116. const listeners = {};
  117. const sourceCode = context.getSourceCode();
  118. //--------------------------------------------------------------------------
  119. // Helpers
  120. //--------------------------------------------------------------------------
  121. /**
  122. * Get exact callee name from expression
  123. * @param {ASTNode} node CallExpression or NewExpression node
  124. * @returns {string} name
  125. */
  126. function extractNameFromExpression(node) {
  127. let name = "";
  128. if (node.callee.type === "MemberExpression") {
  129. const property = node.callee.property;
  130. if (property.type === "Literal" && (typeof property.value === "string")) {
  131. name = property.value;
  132. } else if (property.type === "Identifier" && !node.callee.computed) {
  133. name = property.name;
  134. }
  135. } else {
  136. name = node.callee.name;
  137. }
  138. return name;
  139. }
  140. /**
  141. * Returns the capitalization state of the string -
  142. * Whether the first character is uppercase, lowercase, or non-alphabetic
  143. * @param {string} str String
  144. * @returns {string} capitalization state: "non-alpha", "lower", or "upper"
  145. */
  146. function getCap(str) {
  147. const firstChar = str.charAt(0);
  148. const firstCharLower = firstChar.toLowerCase();
  149. const firstCharUpper = firstChar.toUpperCase();
  150. if (firstCharLower === firstCharUpper) {
  151. // char has no uppercase variant, so it's non-alphabetic
  152. return "non-alpha";
  153. }
  154. if (firstChar === firstCharLower) {
  155. return "lower";
  156. }
  157. return "upper";
  158. }
  159. /**
  160. * Check if capitalization is allowed for a CallExpression
  161. * @param {Object} allowedMap Object mapping calleeName to a Boolean
  162. * @param {ASTNode} node CallExpression node
  163. * @param {string} calleeName Capitalized callee name from a CallExpression
  164. * @param {Object} pattern RegExp object from options pattern
  165. * @returns {boolean} Returns true if the callee may be capitalized
  166. */
  167. function isCapAllowed(allowedMap, node, calleeName, pattern) {
  168. const sourceText = sourceCode.getText(node.callee);
  169. if (allowedMap[calleeName] || allowedMap[sourceText]) {
  170. return true;
  171. }
  172. if (pattern && pattern.test(sourceText)) {
  173. return true;
  174. }
  175. if (calleeName === "UTC" && node.callee.type === "MemberExpression") {
  176. // allow if callee is Date.UTC
  177. return node.callee.object.type === "Identifier" &&
  178. node.callee.object.name === "Date";
  179. }
  180. return skipProperties && node.callee.type === "MemberExpression";
  181. }
  182. /**
  183. * Reports the given message for the given node. The location will be the start of the property or the callee.
  184. * @param {ASTNode} node CallExpression or NewExpression node.
  185. * @param {string} message The message to report.
  186. * @returns {void}
  187. */
  188. function report(node, message) {
  189. let callee = node.callee;
  190. if (callee.type === "MemberExpression") {
  191. callee = callee.property;
  192. }
  193. context.report({ node, loc: callee.loc.start, message });
  194. }
  195. //--------------------------------------------------------------------------
  196. // Public
  197. //--------------------------------------------------------------------------
  198. if (config.newIsCap) {
  199. listeners.NewExpression = function(node) {
  200. const constructorName = extractNameFromExpression(node);
  201. if (constructorName) {
  202. const capitalization = getCap(constructorName);
  203. const isAllowed = capitalization !== "lower" || isCapAllowed(newIsCapExceptions, node, constructorName, newIsCapExceptionPattern);
  204. if (!isAllowed) {
  205. report(node, "A constructor name should not start with a lowercase letter.");
  206. }
  207. }
  208. };
  209. }
  210. if (config.capIsNew) {
  211. listeners.CallExpression = function(node) {
  212. const calleeName = extractNameFromExpression(node);
  213. if (calleeName) {
  214. const capitalization = getCap(calleeName);
  215. const isAllowed = capitalization !== "upper" || isCapAllowed(capIsNewExceptions, node, calleeName, capIsNewExceptionPattern);
  216. if (!isAllowed) {
  217. report(node, "A function with a name starting with an uppercase letter should only be used as a constructor.");
  218. }
  219. }
  220. };
  221. }
  222. return listeners;
  223. }
  224. };