func-name-matching.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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" &&
  105. node.callee.type === "MemberExpression" &&
  106. node.callee.object.name === objName &&
  107. node.callee.property.name === funcName;
  108. }
  109. /**
  110. * Compares identifiers based on the nameMatches option
  111. * @param {string} x the first identifier
  112. * @param {string} y the second identifier
  113. * @returns {boolean} whether the two identifiers should warn.
  114. */
  115. function shouldWarn(x, y) {
  116. return (nameMatches === "always" && x !== y) || (nameMatches === "never" && x === y);
  117. }
  118. /**
  119. * Reports
  120. * @param {ASTNode} node The node to report
  121. * @param {string} name The variable or property name
  122. * @param {string} funcName The function name
  123. * @param {boolean} isProp True if the reported node is a property assignment
  124. * @returns {void}
  125. */
  126. function report(node, name, funcName, isProp) {
  127. let messageId;
  128. if (nameMatches === "always" && isProp) {
  129. messageId = "matchProperty";
  130. } else if (nameMatches === "always") {
  131. messageId = "matchVariable";
  132. } else if (isProp) {
  133. messageId = "notMatchProperty";
  134. } else {
  135. messageId = "notMatchVariable";
  136. }
  137. context.report({
  138. node,
  139. messageId,
  140. data: {
  141. name,
  142. funcName
  143. }
  144. });
  145. }
  146. /**
  147. * Determines whether a given node is a string literal
  148. * @param {ASTNode} node The node to check
  149. * @returns {boolean} `true` if the node is a string literal
  150. */
  151. function isStringLiteral(node) {
  152. return node.type === "Literal" && typeof node.value === "string";
  153. }
  154. //--------------------------------------------------------------------------
  155. // Public
  156. //--------------------------------------------------------------------------
  157. return {
  158. VariableDeclarator(node) {
  159. if (!node.init || node.init.type !== "FunctionExpression" || node.id.type !== "Identifier") {
  160. return;
  161. }
  162. if (node.init.id && shouldWarn(node.id.name, node.init.id.name)) {
  163. report(node, node.id.name, node.init.id.name, false);
  164. }
  165. },
  166. AssignmentExpression(node) {
  167. if (
  168. node.right.type !== "FunctionExpression" ||
  169. (node.left.computed && node.left.property.type !== "Literal") ||
  170. (!includeModuleExports && isModuleExports(node.left)) ||
  171. (node.left.type !== "Identifier" && node.left.type !== "MemberExpression")
  172. ) {
  173. return;
  174. }
  175. const isProp = node.left.type === "MemberExpression";
  176. const name = isProp ? astUtils.getStaticPropertyName(node.left) : node.left.name;
  177. if (node.right.id && isIdentifier(name) && shouldWarn(name, node.right.id.name)) {
  178. report(node, name, node.right.id.name, isProp);
  179. }
  180. },
  181. Property(node) {
  182. if (node.value.type !== "FunctionExpression" || !node.value.id || node.computed && !isStringLiteral(node.key)) {
  183. return;
  184. }
  185. if (node.key.type === "Identifier") {
  186. const functionName = node.value.id.name;
  187. let propertyName = node.key.name;
  188. if (considerPropertyDescriptor && propertyName === "value") {
  189. if (isPropertyCall("Object", "defineProperty", node.parent.parent) || isPropertyCall("Reflect", "defineProperty", node.parent.parent)) {
  190. const property = node.parent.parent.arguments[1];
  191. if (isStringLiteral(property) && shouldWarn(property.value, functionName)) {
  192. report(node, property.value, functionName, true);
  193. }
  194. } else if (isPropertyCall("Object", "defineProperties", node.parent.parent.parent.parent)) {
  195. propertyName = node.parent.parent.key.name;
  196. if (!node.parent.parent.computed && shouldWarn(propertyName, functionName)) {
  197. report(node, propertyName, functionName, true);
  198. }
  199. } else if (isPropertyCall("Object", "create", node.parent.parent.parent.parent)) {
  200. propertyName = node.parent.parent.key.name;
  201. if (!node.parent.parent.computed && shouldWarn(propertyName, functionName)) {
  202. report(node, propertyName, functionName, true);
  203. }
  204. } else if (shouldWarn(propertyName, functionName)) {
  205. report(node, propertyName, functionName, true);
  206. }
  207. } else if (shouldWarn(propertyName, functionName)) {
  208. report(node, propertyName, functionName, true);
  209. }
  210. return;
  211. }
  212. if (
  213. isStringLiteral(node.key) &&
  214. isIdentifier(node.key.value, ecmaVersion) &&
  215. shouldWarn(node.key.value, node.value.id.name)
  216. ) {
  217. report(node, node.key.value, node.value.id.name, true);
  218. }
  219. }
  220. };
  221. }
  222. };