func-name-matching.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 (
  21. pattern.type === "MemberExpression" &&
  22. pattern.object.type === "Identifier" &&
  23. pattern.object.name === "module"
  24. ) {
  25. // module.exports
  26. if (
  27. pattern.property.type === "Identifier" &&
  28. pattern.property.name === "exports"
  29. ) {
  30. return true;
  31. }
  32. // module["exports"]
  33. if (
  34. pattern.property.type === "Literal" &&
  35. pattern.property.value === "exports"
  36. ) {
  37. return true;
  38. }
  39. }
  40. return false;
  41. }
  42. /**
  43. * Determines if a string name is a valid identifier
  44. * @param {string} name The string to be checked
  45. * @param {number} ecmaVersion The ECMAScript version if specified in the parserOptions config
  46. * @returns {boolean} True if the string is a valid identifier
  47. */
  48. function isIdentifier(name, ecmaVersion) {
  49. if (ecmaVersion >= 2015) {
  50. return esutils.keyword.isIdentifierES6(name);
  51. }
  52. return esutils.keyword.isIdentifierES5(name);
  53. }
  54. //------------------------------------------------------------------------------
  55. // Rule Definition
  56. //------------------------------------------------------------------------------
  57. const alwaysOrNever = { enum: ["always", "never"] };
  58. const optionsObject = {
  59. type: "object",
  60. properties: {
  61. considerPropertyDescriptor: {
  62. type: "boolean",
  63. },
  64. includeCommonJSModuleExports: {
  65. type: "boolean",
  66. },
  67. },
  68. additionalProperties: false,
  69. };
  70. /** @type {import('../types').Rule.RuleModule} */
  71. module.exports = {
  72. meta: {
  73. type: "suggestion",
  74. docs: {
  75. description:
  76. "Require function names to match the name of the variable or property to which they are assigned",
  77. recommended: false,
  78. frozen: true,
  79. url: "https://eslint.org/docs/latest/rules/func-name-matching",
  80. },
  81. schema: {
  82. anyOf: [
  83. {
  84. type: "array",
  85. additionalItems: false,
  86. items: [alwaysOrNever, optionsObject],
  87. },
  88. {
  89. type: "array",
  90. additionalItems: false,
  91. items: [optionsObject],
  92. },
  93. ],
  94. },
  95. messages: {
  96. matchProperty:
  97. "Function name `{{funcName}}` should match property name `{{name}}`.",
  98. matchVariable:
  99. "Function name `{{funcName}}` should match variable name `{{name}}`.",
  100. notMatchProperty:
  101. "Function name `{{funcName}}` should not match property name `{{name}}`.",
  102. notMatchVariable:
  103. "Function name `{{funcName}}` should not match variable name `{{name}}`.",
  104. },
  105. },
  106. create(context) {
  107. const options =
  108. (typeof context.options[0] === "object"
  109. ? context.options[0]
  110. : context.options[1]) || {};
  111. const nameMatches =
  112. typeof context.options[0] === "string"
  113. ? context.options[0]
  114. : "always";
  115. const considerPropertyDescriptor = options.considerPropertyDescriptor;
  116. const includeModuleExports = options.includeCommonJSModuleExports;
  117. const ecmaVersion = context.languageOptions.ecmaVersion;
  118. /**
  119. * Check whether node is a certain CallExpression.
  120. * @param {string} objName object name
  121. * @param {string} funcName function name
  122. * @param {ASTNode} node The node to check
  123. * @returns {boolean} `true` if node matches CallExpression
  124. */
  125. function isPropertyCall(objName, funcName, node) {
  126. if (!node) {
  127. return false;
  128. }
  129. return (
  130. node.type === "CallExpression" &&
  131. astUtils.isSpecificMemberAccess(node.callee, objName, funcName)
  132. );
  133. }
  134. /**
  135. * Compares identifiers based on the nameMatches option
  136. * @param {string} x the first identifier
  137. * @param {string} y the second identifier
  138. * @returns {boolean} whether the two identifiers should warn.
  139. */
  140. function shouldWarn(x, y) {
  141. return (
  142. (nameMatches === "always" && x !== y) ||
  143. (nameMatches === "never" && x === y)
  144. );
  145. }
  146. /**
  147. * Reports
  148. * @param {ASTNode} node The node to report
  149. * @param {string} name The variable or property name
  150. * @param {string} funcName The function name
  151. * @param {boolean} isProp True if the reported node is a property assignment
  152. * @returns {void}
  153. */
  154. function report(node, name, funcName, isProp) {
  155. let messageId;
  156. if (nameMatches === "always" && isProp) {
  157. messageId = "matchProperty";
  158. } else if (nameMatches === "always") {
  159. messageId = "matchVariable";
  160. } else if (isProp) {
  161. messageId = "notMatchProperty";
  162. } else {
  163. messageId = "notMatchVariable";
  164. }
  165. context.report({
  166. node,
  167. messageId,
  168. data: {
  169. name,
  170. funcName,
  171. },
  172. });
  173. }
  174. /**
  175. * Determines whether a given node is a string literal
  176. * @param {ASTNode} node The node to check
  177. * @returns {boolean} `true` if the node is a string literal
  178. */
  179. function isStringLiteral(node) {
  180. return node.type === "Literal" && typeof node.value === "string";
  181. }
  182. //--------------------------------------------------------------------------
  183. // Public
  184. //--------------------------------------------------------------------------
  185. return {
  186. VariableDeclarator(node) {
  187. if (
  188. !node.init ||
  189. node.init.type !== "FunctionExpression" ||
  190. node.id.type !== "Identifier"
  191. ) {
  192. return;
  193. }
  194. if (
  195. node.init.id &&
  196. shouldWarn(node.id.name, node.init.id.name)
  197. ) {
  198. report(node, node.id.name, node.init.id.name, false);
  199. }
  200. },
  201. AssignmentExpression(node) {
  202. if (
  203. node.right.type !== "FunctionExpression" ||
  204. (node.left.computed &&
  205. node.left.property.type !== "Literal") ||
  206. (!includeModuleExports && isModuleExports(node.left)) ||
  207. (node.left.type !== "Identifier" &&
  208. node.left.type !== "MemberExpression")
  209. ) {
  210. return;
  211. }
  212. const isProp = node.left.type === "MemberExpression";
  213. const name = isProp
  214. ? astUtils.getStaticPropertyName(node.left)
  215. : node.left.name;
  216. if (
  217. node.right.id &&
  218. name &&
  219. isIdentifier(name) &&
  220. shouldWarn(name, node.right.id.name)
  221. ) {
  222. report(node, name, node.right.id.name, isProp);
  223. }
  224. },
  225. "Property, PropertyDefinition[value]"(node) {
  226. if (
  227. !(node.value.type === "FunctionExpression" && node.value.id)
  228. ) {
  229. return;
  230. }
  231. if (node.key.type === "Identifier" && !node.computed) {
  232. const functionName = node.value.id.name;
  233. let propertyName = node.key.name;
  234. if (
  235. considerPropertyDescriptor &&
  236. propertyName === "value" &&
  237. node.parent.type === "ObjectExpression"
  238. ) {
  239. if (
  240. isPropertyCall(
  241. "Object",
  242. "defineProperty",
  243. node.parent.parent,
  244. ) ||
  245. isPropertyCall(
  246. "Reflect",
  247. "defineProperty",
  248. node.parent.parent,
  249. )
  250. ) {
  251. const property = node.parent.parent.arguments[1];
  252. if (
  253. isStringLiteral(property) &&
  254. shouldWarn(property.value, functionName)
  255. ) {
  256. report(
  257. node,
  258. property.value,
  259. functionName,
  260. true,
  261. );
  262. }
  263. } else if (
  264. isPropertyCall(
  265. "Object",
  266. "defineProperties",
  267. node.parent.parent.parent.parent,
  268. )
  269. ) {
  270. propertyName = node.parent.parent.key.name;
  271. if (
  272. !node.parent.parent.computed &&
  273. shouldWarn(propertyName, functionName)
  274. ) {
  275. report(node, propertyName, functionName, true);
  276. }
  277. } else if (
  278. isPropertyCall(
  279. "Object",
  280. "create",
  281. node.parent.parent.parent.parent,
  282. )
  283. ) {
  284. propertyName = node.parent.parent.key.name;
  285. if (
  286. !node.parent.parent.computed &&
  287. shouldWarn(propertyName, functionName)
  288. ) {
  289. report(node, propertyName, functionName, true);
  290. }
  291. } else if (shouldWarn(propertyName, functionName)) {
  292. report(node, propertyName, functionName, true);
  293. }
  294. } else if (shouldWarn(propertyName, functionName)) {
  295. report(node, propertyName, functionName, true);
  296. }
  297. return;
  298. }
  299. if (
  300. isStringLiteral(node.key) &&
  301. isIdentifier(node.key.value, ecmaVersion) &&
  302. shouldWarn(node.key.value, node.value.id.name)
  303. ) {
  304. report(node, node.key.value, node.value.id.name, true);
  305. }
  306. },
  307. };
  308. },
  309. };