You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

177 lines
5.9 KiB

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