func-name-matching.js 9.8 KB

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