func-names.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /**
  2. * @fileoverview Rule to warn when a function expression does not have a name.
  3. * @author Kyle T. Nunery
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const astUtils = require("./utils/ast-utils");
  10. /**
  11. * Checks whether or not a given variable is a function name.
  12. * @param {eslint-scope.Variable} variable A variable to check.
  13. * @returns {boolean} `true` if the variable is a function name.
  14. */
  15. function isFunctionName(variable) {
  16. return variable && variable.defs[0].type === "FunctionName";
  17. }
  18. //------------------------------------------------------------------------------
  19. // Rule Definition
  20. //------------------------------------------------------------------------------
  21. /** @type {import('../types').Rule.RuleModule} */
  22. module.exports = {
  23. meta: {
  24. type: "suggestion",
  25. defaultOptions: ["always", {}],
  26. docs: {
  27. description: "Require or disallow named `function` expressions",
  28. recommended: false,
  29. url: "https://eslint.org/docs/latest/rules/func-names",
  30. },
  31. schema: {
  32. definitions: {
  33. value: {
  34. enum: ["always", "as-needed", "never"],
  35. },
  36. },
  37. items: [
  38. {
  39. $ref: "#/definitions/value",
  40. },
  41. {
  42. type: "object",
  43. properties: {
  44. generators: {
  45. $ref: "#/definitions/value",
  46. },
  47. },
  48. additionalProperties: false,
  49. },
  50. ],
  51. },
  52. messages: {
  53. unnamed: "Unexpected unnamed {{name}}.",
  54. named: "Unexpected named {{name}}.",
  55. },
  56. },
  57. create(context) {
  58. const sourceCode = context.sourceCode;
  59. /**
  60. * Returns the config option for the given node.
  61. * @param {ASTNode} node A node to get the config for.
  62. * @returns {string} The config option.
  63. */
  64. function getConfigForNode(node) {
  65. if (node.generator && context.options[1].generators) {
  66. return context.options[1].generators;
  67. }
  68. return context.options[0];
  69. }
  70. /**
  71. * Determines whether the current FunctionExpression node is a get, set, or
  72. * shorthand method in an object literal or a class.
  73. * @param {ASTNode} node A node to check.
  74. * @returns {boolean} True if the node is a get, set, or shorthand method.
  75. */
  76. function isObjectOrClassMethod(node) {
  77. const parent = node.parent;
  78. return (
  79. parent.type === "MethodDefinition" ||
  80. (parent.type === "Property" &&
  81. (parent.method ||
  82. parent.kind === "get" ||
  83. parent.kind === "set"))
  84. );
  85. }
  86. /**
  87. * Determines whether the current FunctionExpression node has a name that would be
  88. * inferred from context in a conforming ES6 environment.
  89. * @param {ASTNode} node A node to check.
  90. * @returns {boolean} True if the node would have a name assigned automatically.
  91. */
  92. function hasInferredName(node) {
  93. const parent = node.parent;
  94. return (
  95. isObjectOrClassMethod(node) ||
  96. (parent.type === "VariableDeclarator" &&
  97. parent.id.type === "Identifier" &&
  98. parent.init === node) ||
  99. (parent.type === "Property" && parent.value === node) ||
  100. (parent.type === "PropertyDefinition" &&
  101. parent.value === node) ||
  102. (parent.type === "AssignmentExpression" &&
  103. parent.left.type === "Identifier" &&
  104. parent.right === node) ||
  105. (parent.type === "AssignmentPattern" &&
  106. parent.left.type === "Identifier" &&
  107. parent.right === node)
  108. );
  109. }
  110. /**
  111. * Reports that an unnamed function should be named
  112. * @param {ASTNode} node The node to report in the event of an error.
  113. * @returns {void}
  114. */
  115. function reportUnexpectedUnnamedFunction(node) {
  116. context.report({
  117. node,
  118. messageId: "unnamed",
  119. loc: astUtils.getFunctionHeadLoc(node, sourceCode),
  120. data: { name: astUtils.getFunctionNameWithKind(node) },
  121. });
  122. }
  123. /**
  124. * Reports that a named function should be unnamed
  125. * @param {ASTNode} node The node to report in the event of an error.
  126. * @returns {void}
  127. */
  128. function reportUnexpectedNamedFunction(node) {
  129. context.report({
  130. node,
  131. messageId: "named",
  132. loc: astUtils.getFunctionHeadLoc(node, sourceCode),
  133. data: { name: astUtils.getFunctionNameWithKind(node) },
  134. });
  135. }
  136. /**
  137. * The listener for function nodes.
  138. * @param {ASTNode} node function node
  139. * @returns {void}
  140. */
  141. function handleFunction(node) {
  142. // Skip recursive functions.
  143. const nameVar = sourceCode.getDeclaredVariables(node)[0];
  144. if (isFunctionName(nameVar) && nameVar.references.length > 0) {
  145. return;
  146. }
  147. const hasName = Boolean(node.id && node.id.name);
  148. const config = getConfigForNode(node);
  149. if (config === "never") {
  150. if (hasName && node.type !== "FunctionDeclaration") {
  151. reportUnexpectedNamedFunction(node);
  152. }
  153. } else if (config === "as-needed") {
  154. if (!hasName && !hasInferredName(node)) {
  155. reportUnexpectedUnnamedFunction(node);
  156. }
  157. } else {
  158. if (!hasName && !isObjectOrClassMethod(node)) {
  159. reportUnexpectedUnnamedFunction(node);
  160. }
  161. }
  162. }
  163. return {
  164. "FunctionExpression:exit": handleFunction,
  165. "ExportDefaultDeclaration > FunctionDeclaration": handleFunction,
  166. };
  167. },
  168. };