func-names.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. module.exports = {
  22. meta: {
  23. type: "suggestion",
  24. docs: {
  25. description: "require or disallow named `function` expressions",
  26. category: "Stylistic Issues",
  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 === "AssignmentExpression" && parent.left.type === "Identifier" && parent.right === node) ||
  105. (parent.type === "AssignmentPattern" && parent.left.type === "Identifier" && parent.right === node);
  106. }
  107. /**
  108. * Reports that an unnamed function should be named
  109. * @param {ASTNode} node The node to report in the event of an error.
  110. * @returns {void}
  111. */
  112. function reportUnexpectedUnnamedFunction(node) {
  113. context.report({
  114. node,
  115. messageId: "unnamed",
  116. loc: astUtils.getFunctionHeadLoc(node, sourceCode),
  117. data: { name: astUtils.getFunctionNameWithKind(node) }
  118. });
  119. }
  120. /**
  121. * Reports that a named function should be unnamed
  122. * @param {ASTNode} node The node to report in the event of an error.
  123. * @returns {void}
  124. */
  125. function reportUnexpectedNamedFunction(node) {
  126. context.report({
  127. node,
  128. messageId: "named",
  129. loc: astUtils.getFunctionHeadLoc(node, sourceCode),
  130. data: { name: astUtils.getFunctionNameWithKind(node) }
  131. });
  132. }
  133. /**
  134. * The listener for function nodes.
  135. * @param {ASTNode} node function node
  136. * @returns {void}
  137. */
  138. function handleFunction(node) {
  139. // Skip recursive functions.
  140. const nameVar = context.getDeclaredVariables(node)[0];
  141. if (isFunctionName(nameVar) && nameVar.references.length > 0) {
  142. return;
  143. }
  144. const hasName = Boolean(node.id && node.id.name);
  145. const config = getConfigForNode(node);
  146. if (config === "never") {
  147. if (hasName && node.type !== "FunctionDeclaration") {
  148. reportUnexpectedNamedFunction(node);
  149. }
  150. } else if (config === "as-needed") {
  151. if (!hasName && !hasInferredName(node)) {
  152. reportUnexpectedUnnamedFunction(node);
  153. }
  154. } else {
  155. if (!hasName && !isObjectOrClassMethod(node)) {
  156. reportUnexpectedUnnamedFunction(node);
  157. }
  158. }
  159. }
  160. return {
  161. "FunctionExpression:exit": handleFunction,
  162. "ExportDefaultDeclaration > FunctionDeclaration": handleFunction
  163. };
  164. }
  165. };