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.
 
 
 
 

249 lines
9.8 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("../util/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. considerPropertyDescriptor: {
  52. type: "boolean"
  53. },
  54. includeCommonJSModuleExports: {
  55. type: "boolean"
  56. }
  57. },
  58. additionalProperties: false
  59. };
  60. module.exports = {
  61. meta: {
  62. docs: {
  63. description: "require function names to match the name of the variable or property to which they are assigned",
  64. category: "Stylistic Issues",
  65. recommended: false,
  66. url: "https://eslint.org/docs/rules/func-name-matching"
  67. },
  68. schema: {
  69. anyOf: [{
  70. type: "array",
  71. additionalItems: false,
  72. items: [alwaysOrNever, optionsObject]
  73. }, {
  74. type: "array",
  75. additionalItems: false,
  76. items: [optionsObject]
  77. }]
  78. },
  79. messages: {
  80. matchProperty: "Function name `{{funcName}}` should match property name `{{name}}`",
  81. matchVariable: "Function name `{{funcName}}` should match variable name `{{name}}`",
  82. notMatchProperty: "Function name `{{funcName}}` should not match property name `{{name}}`",
  83. notMatchVariable: "Function name `{{funcName}}` should not match variable name `{{name}}`"
  84. }
  85. },
  86. create(context) {
  87. const options = (typeof context.options[0] === "object" ? context.options[0] : context.options[1]) || {};
  88. const nameMatches = typeof context.options[0] === "string" ? context.options[0] : "always";
  89. const considerPropertyDescriptor = options.considerPropertyDescriptor;
  90. const includeModuleExports = options.includeCommonJSModuleExports;
  91. const ecmaVersion = context.parserOptions && context.parserOptions.ecmaVersion ? context.parserOptions.ecmaVersion : 5;
  92. /**
  93. * Check whether node is a certain CallExpression.
  94. * @param {string} objName object name
  95. * @param {string} funcName function name
  96. * @param {ASTNode} node The node to check
  97. * @returns {boolean} `true` if node matches CallExpression
  98. */
  99. function isPropertyCall(objName, funcName, node) {
  100. if (!node) {
  101. return false;
  102. }
  103. return node.type === "CallExpression" &&
  104. node.callee.object.name === objName &&
  105. node.callee.property.name === funcName;
  106. }
  107. /**
  108. * Compares identifiers based on the nameMatches option
  109. * @param {string} x the first identifier
  110. * @param {string} y the second identifier
  111. * @returns {boolean} whether the two identifiers should warn.
  112. */
  113. function shouldWarn(x, y) {
  114. return (nameMatches === "always" && x !== y) || (nameMatches === "never" && x === y);
  115. }
  116. /**
  117. * Reports
  118. * @param {ASTNode} node The node to report
  119. * @param {string} name The variable or property name
  120. * @param {string} funcName The function name
  121. * @param {boolean} isProp True if the reported node is a property assignment
  122. * @returns {void}
  123. */
  124. function report(node, name, funcName, isProp) {
  125. let messageId;
  126. if (nameMatches === "always" && isProp) {
  127. messageId = "matchProperty";
  128. } else if (nameMatches === "always") {
  129. messageId = "matchVariable";
  130. } else if (isProp) {
  131. messageId = "notMatchProperty";
  132. } else {
  133. messageId = "notMatchVariable";
  134. }
  135. context.report({
  136. node,
  137. messageId,
  138. data: {
  139. name,
  140. funcName
  141. }
  142. });
  143. }
  144. /**
  145. * Determines whether a given node is a string literal
  146. * @param {ASTNode} node The node to check
  147. * @returns {boolean} `true` if the node is a string literal
  148. */
  149. function isStringLiteral(node) {
  150. return node.type === "Literal" && typeof node.value === "string";
  151. }
  152. //--------------------------------------------------------------------------
  153. // Public
  154. //--------------------------------------------------------------------------
  155. return {
  156. VariableDeclarator(node) {
  157. if (!node.init || node.init.type !== "FunctionExpression" || node.id.type !== "Identifier") {
  158. return;
  159. }
  160. if (node.init.id && shouldWarn(node.id.name, node.init.id.name)) {
  161. report(node, node.id.name, node.init.id.name, false);
  162. }
  163. },
  164. AssignmentExpression(node) {
  165. if (
  166. node.right.type !== "FunctionExpression" ||
  167. (node.left.computed && node.left.property.type !== "Literal") ||
  168. (!includeModuleExports && isModuleExports(node.left)) ||
  169. (node.left.type !== "Identifier" && node.left.type !== "MemberExpression")
  170. ) {
  171. return;
  172. }
  173. const isProp = node.left.type === "MemberExpression";
  174. const name = isProp ? astUtils.getStaticPropertyName(node.left) : node.left.name;
  175. if (node.right.id && isIdentifier(name) && shouldWarn(name, node.right.id.name)) {
  176. report(node, name, node.right.id.name, isProp);
  177. }
  178. },
  179. Property(node) {
  180. if (node.value.type !== "FunctionExpression" || !node.value.id || node.computed && !isStringLiteral(node.key)) {
  181. return;
  182. }
  183. if (node.key.type === "Identifier") {
  184. const functionName = node.value.id.name;
  185. let propertyName = node.key.name;
  186. if (considerPropertyDescriptor && propertyName === "value") {
  187. if (isPropertyCall("Object", "defineProperty", node.parent.parent) || isPropertyCall("Reflect", "defineProperty", node.parent.parent)) {
  188. const property = node.parent.parent.arguments[1];
  189. if (isStringLiteral(property) && shouldWarn(property.value, functionName)) {
  190. report(node, property.value, functionName, true);
  191. }
  192. } else if (isPropertyCall("Object", "defineProperties", node.parent.parent.parent.parent)) {
  193. propertyName = node.parent.parent.key.name;
  194. if (!node.parent.parent.computed && shouldWarn(propertyName, functionName)) {
  195. report(node, propertyName, functionName, true);
  196. }
  197. } else if (isPropertyCall("Object", "create", node.parent.parent.parent.parent)) {
  198. propertyName = node.parent.parent.key.name;
  199. if (!node.parent.parent.computed && shouldWarn(propertyName, functionName)) {
  200. report(node, propertyName, functionName, true);
  201. }
  202. } else if (shouldWarn(propertyName, functionName)) {
  203. report(node, propertyName, functionName, true);
  204. }
  205. } else if (shouldWarn(propertyName, functionName)) {
  206. report(node, propertyName, functionName, true);
  207. }
  208. return;
  209. }
  210. if (
  211. isStringLiteral(node.key) &&
  212. isIdentifier(node.key.value, ecmaVersion) &&
  213. shouldWarn(node.key.value, node.value.id.name)
  214. ) {
  215. report(node, node.key.value, node.value.id.name, true);
  216. }
  217. }
  218. };
  219. }
  220. };