Ohm-Management - Projektarbeit B-ME
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.

func-names.js 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. 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. /**
  62. * Returns the config option for the given node.
  63. * @param {ASTNode} node - A node to get the config for.
  64. * @returns {string} The config option.
  65. */
  66. function getConfigForNode(node) {
  67. if (
  68. node.generator &&
  69. context.options.length > 1 &&
  70. context.options[1].generators
  71. ) {
  72. return context.options[1].generators;
  73. }
  74. return context.options[0] || "always";
  75. }
  76. /**
  77. * Determines whether the current FunctionExpression node is a get, set, or
  78. * shorthand method in an object literal or a class.
  79. * @param {ASTNode} node - A node to check.
  80. * @returns {boolean} True if the node is a get, set, or shorthand method.
  81. */
  82. function isObjectOrClassMethod(node) {
  83. const parent = node.parent;
  84. return (parent.type === "MethodDefinition" || (
  85. parent.type === "Property" && (
  86. parent.method ||
  87. parent.kind === "get" ||
  88. parent.kind === "set"
  89. )
  90. ));
  91. }
  92. /**
  93. * Determines whether the current FunctionExpression node has a name that would be
  94. * inferred from context in a conforming ES6 environment.
  95. * @param {ASTNode} node - A node to check.
  96. * @returns {boolean} True if the node would have a name assigned automatically.
  97. */
  98. function hasInferredName(node) {
  99. const parent = node.parent;
  100. return isObjectOrClassMethod(node) ||
  101. (parent.type === "VariableDeclarator" && parent.id.type === "Identifier" && parent.init === node) ||
  102. (parent.type === "Property" && parent.value === node) ||
  103. (parent.type === "AssignmentExpression" && parent.left.type === "Identifier" && parent.right === node) ||
  104. (parent.type === "ExportDefaultDeclaration" && parent.declaration === node) ||
  105. (parent.type === "AssignmentPattern" && 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. data: { name: astUtils.getFunctionNameWithKind(node) }
  117. });
  118. }
  119. /**
  120. * Reports that a named function should be unnamed
  121. * @param {ASTNode} node - The node to report in the event of an error.
  122. * @returns {void}
  123. */
  124. function reportUnexpectedNamedFunction(node) {
  125. context.report({
  126. node,
  127. messageId: "named",
  128. data: { name: astUtils.getFunctionNameWithKind(node) }
  129. });
  130. }
  131. return {
  132. "FunctionExpression:exit"(node) {
  133. // Skip recursive functions.
  134. const nameVar = context.getDeclaredVariables(node)[0];
  135. if (isFunctionName(nameVar) && nameVar.references.length > 0) {
  136. return;
  137. }
  138. const hasName = Boolean(node.id && node.id.name);
  139. const config = getConfigForNode(node);
  140. if (config === "never") {
  141. if (hasName) {
  142. reportUnexpectedNamedFunction(node);
  143. }
  144. } else if (config === "as-needed") {
  145. if (!hasName && !hasInferredName(node)) {
  146. reportUnexpectedUnnamedFunction(node);
  147. }
  148. } else {
  149. if (!hasName && !isObjectOrClassMethod(node)) {
  150. reportUnexpectedUnnamedFunction(node);
  151. }
  152. }
  153. }
  154. };
  155. }
  156. };