项目原始demo,不改动
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 
 
 
 

195 lines
7.2 KiB

  1. /**
  2. * @fileoverview Rule to require function names to match the name of the variable or property to which they are assigned.
  3. * @author Annie Zhang, Pavel Strashkin
  4. */
  5. "use strict";
  6. //--------------------------------------------------------------------------
  7. // Requirements
  8. //--------------------------------------------------------------------------
  9. const astUtils = require("../ast-utils");
  10. const esutils = require("esutils");
  11. //--------------------------------------------------------------------------
  12. // Helpers
  13. //--------------------------------------------------------------------------
  14. /**
  15. * Determines if a pattern is `module.exports` or `module["exports"]`
  16. * @param {ASTNode} pattern The left side of the AssignmentExpression
  17. * @returns {boolean} True if the pattern is `module.exports` or `module["exports"]`
  18. */
  19. function isModuleExports(pattern) {
  20. if (pattern.type === "MemberExpression" && pattern.object.type === "Identifier" && pattern.object.name === "module") {
  21. // module.exports
  22. if (pattern.property.type === "Identifier" && pattern.property.name === "exports") {
  23. return true;
  24. }
  25. // module["exports"]
  26. if (pattern.property.type === "Literal" && pattern.property.value === "exports") {
  27. return true;
  28. }
  29. }
  30. return false;
  31. }
  32. /**
  33. * Determines if a string name is a valid identifier
  34. * @param {string} name The string to be checked
  35. * @param {int} ecmaVersion The ECMAScript version if specified in the parserOptions config
  36. * @returns {boolean} True if the string is a valid identifier
  37. */
  38. function isIdentifier(name, ecmaVersion) {
  39. if (ecmaVersion >= 6) {
  40. return esutils.keyword.isIdentifierES6(name);
  41. }
  42. return esutils.keyword.isIdentifierES5(name);
  43. }
  44. //------------------------------------------------------------------------------
  45. // Rule Definition
  46. //------------------------------------------------------------------------------
  47. const alwaysOrNever = { enum: ["always", "never"] };
  48. const optionsObject = {
  49. type: "object",
  50. properties: {
  51. includeCommonJSModuleExports: {
  52. type: "boolean"
  53. }
  54. },
  55. additionalProperties: false
  56. };
  57. module.exports = {
  58. meta: {
  59. docs: {
  60. description: "require function names to match the name of the variable or property to which they are assigned",
  61. category: "Stylistic Issues",
  62. recommended: false,
  63. url: "https://eslint.org/docs/rules/func-name-matching"
  64. },
  65. schema: {
  66. anyOf: [{
  67. type: "array",
  68. additionalItems: false,
  69. items: [alwaysOrNever, optionsObject]
  70. }, {
  71. type: "array",
  72. additionalItems: false,
  73. items: [optionsObject]
  74. }]
  75. }
  76. },
  77. create(context) {
  78. const options = (typeof context.options[0] === "object" ? context.options[0] : context.options[1]) || {};
  79. const nameMatches = typeof context.options[0] === "string" ? context.options[0] : "always";
  80. const includeModuleExports = options.includeCommonJSModuleExports;
  81. const ecmaVersion = context.parserOptions && context.parserOptions.ecmaVersion ? context.parserOptions.ecmaVersion : 5;
  82. /**
  83. * Compares identifiers based on the nameMatches option
  84. * @param {string} x the first identifier
  85. * @param {string} y the second identifier
  86. * @returns {boolean} whether the two identifiers should warn.
  87. */
  88. function shouldWarn(x, y) {
  89. return (nameMatches === "always" && x !== y) || (nameMatches === "never" && x === y);
  90. }
  91. /**
  92. * Reports
  93. * @param {ASTNode} node The node to report
  94. * @param {string} name The variable or property name
  95. * @param {string} funcName The function name
  96. * @param {boolean} isProp True if the reported node is a property assignment
  97. * @returns {void}
  98. */
  99. function report(node, name, funcName, isProp) {
  100. let message;
  101. if (nameMatches === "always" && isProp) {
  102. message = "Function name `{{funcName}}` should match property name `{{name}}`";
  103. } else if (nameMatches === "always") {
  104. message = "Function name `{{funcName}}` should match variable name `{{name}}`";
  105. } else if (isProp) {
  106. message = "Function name `{{funcName}}` should not match property name `{{name}}`";
  107. } else {
  108. message = "Function name `{{funcName}}` should not match variable name `{{name}}`";
  109. }
  110. context.report({
  111. node,
  112. message,
  113. data: {
  114. name,
  115. funcName
  116. }
  117. });
  118. }
  119. /**
  120. * Determines whether a given node is a string literal
  121. * @param {ASTNode} node The node to check
  122. * @returns {boolean} `true` if the node is a string literal
  123. */
  124. function isStringLiteral(node) {
  125. return node.type === "Literal" && typeof node.value === "string";
  126. }
  127. //--------------------------------------------------------------------------
  128. // Public
  129. //--------------------------------------------------------------------------
  130. return {
  131. VariableDeclarator(node) {
  132. if (!node.init || node.init.type !== "FunctionExpression" || node.id.type !== "Identifier") {
  133. return;
  134. }
  135. if (node.init.id && shouldWarn(node.id.name, node.init.id.name)) {
  136. report(node, node.id.name, node.init.id.name, false);
  137. }
  138. },
  139. AssignmentExpression(node) {
  140. if (
  141. node.right.type !== "FunctionExpression" ||
  142. (node.left.computed && node.left.property.type !== "Literal") ||
  143. (!includeModuleExports && isModuleExports(node.left)) ||
  144. (node.left.type !== "Identifier" && node.left.type !== "MemberExpression")
  145. ) {
  146. return;
  147. }
  148. const isProp = node.left.type === "MemberExpression";
  149. const name = isProp ? astUtils.getStaticPropertyName(node.left) : node.left.name;
  150. if (node.right.id && isIdentifier(name) && shouldWarn(name, node.right.id.name)) {
  151. report(node, name, node.right.id.name, isProp);
  152. }
  153. },
  154. Property(node) {
  155. if (node.value.type !== "FunctionExpression" || !node.value.id || node.computed && !isStringLiteral(node.key)) {
  156. return;
  157. }
  158. if (node.key.type === "Identifier" && shouldWarn(node.key.name, node.value.id.name)) {
  159. report(node, node.key.name, node.value.id.name, true);
  160. } else if (
  161. isStringLiteral(node.key) &&
  162. isIdentifier(node.key.value, ecmaVersion) &&
  163. shouldWarn(node.key.value, node.value.id.name)
  164. ) {
  165. report(node, node.key.value, node.value.id.name, true);
  166. }
  167. }
  168. };
  169. }
  170. };