func-names.js 6.3 KB

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