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.

no-empty-function.js 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /**
  2. * @fileoverview Rule to disallow empty functions.
  3. * @author Toru Nagashima
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const astUtils = require("../util/ast-utils");
  10. //------------------------------------------------------------------------------
  11. // Helpers
  12. //------------------------------------------------------------------------------
  13. const ALLOW_OPTIONS = Object.freeze([
  14. "functions",
  15. "arrowFunctions",
  16. "generatorFunctions",
  17. "methods",
  18. "generatorMethods",
  19. "getters",
  20. "setters",
  21. "constructors"
  22. ]);
  23. /**
  24. * Gets the kind of a given function node.
  25. *
  26. * @param {ASTNode} node - A function node to get. This is one of
  27. * an ArrowFunctionExpression, a FunctionDeclaration, or a
  28. * FunctionExpression.
  29. * @returns {string} The kind of the function. This is one of "functions",
  30. * "arrowFunctions", "generatorFunctions", "asyncFunctions", "methods",
  31. * "generatorMethods", "asyncMethods", "getters", "setters", and
  32. * "constructors".
  33. */
  34. function getKind(node) {
  35. const parent = node.parent;
  36. let kind = "";
  37. if (node.type === "ArrowFunctionExpression") {
  38. return "arrowFunctions";
  39. }
  40. // Detects main kind.
  41. if (parent.type === "Property") {
  42. if (parent.kind === "get") {
  43. return "getters";
  44. }
  45. if (parent.kind === "set") {
  46. return "setters";
  47. }
  48. kind = parent.method ? "methods" : "functions";
  49. } else if (parent.type === "MethodDefinition") {
  50. if (parent.kind === "get") {
  51. return "getters";
  52. }
  53. if (parent.kind === "set") {
  54. return "setters";
  55. }
  56. if (parent.kind === "constructor") {
  57. return "constructors";
  58. }
  59. kind = "methods";
  60. } else {
  61. kind = "functions";
  62. }
  63. // Detects prefix.
  64. let prefix = "";
  65. if (node.generator) {
  66. prefix = "generator";
  67. } else if (node.async) {
  68. prefix = "async";
  69. } else {
  70. return kind;
  71. }
  72. return prefix + kind[0].toUpperCase() + kind.slice(1);
  73. }
  74. //------------------------------------------------------------------------------
  75. // Rule Definition
  76. //------------------------------------------------------------------------------
  77. module.exports = {
  78. meta: {
  79. type: "suggestion",
  80. docs: {
  81. description: "disallow empty functions",
  82. category: "Best Practices",
  83. recommended: false,
  84. url: "https://eslint.org/docs/rules/no-empty-function"
  85. },
  86. schema: [
  87. {
  88. type: "object",
  89. properties: {
  90. allow: {
  91. type: "array",
  92. items: { enum: ALLOW_OPTIONS },
  93. uniqueItems: true
  94. }
  95. },
  96. additionalProperties: false
  97. }
  98. ],
  99. messages: {
  100. unexpected: "Unexpected empty {{name}}."
  101. }
  102. },
  103. create(context) {
  104. const options = context.options[0] || {};
  105. const allowed = options.allow || [];
  106. const sourceCode = context.getSourceCode();
  107. /**
  108. * Reports a given function node if the node matches the following patterns.
  109. *
  110. * - Not allowed by options.
  111. * - The body is empty.
  112. * - The body doesn't have any comments.
  113. *
  114. * @param {ASTNode} node - A function node to report. This is one of
  115. * an ArrowFunctionExpression, a FunctionDeclaration, or a
  116. * FunctionExpression.
  117. * @returns {void}
  118. */
  119. function reportIfEmpty(node) {
  120. const kind = getKind(node);
  121. const name = astUtils.getFunctionNameWithKind(node);
  122. const innerComments = sourceCode.getTokens(node.body, {
  123. includeComments: true,
  124. filter: astUtils.isCommentToken
  125. });
  126. if (allowed.indexOf(kind) === -1 &&
  127. node.body.type === "BlockStatement" &&
  128. node.body.body.length === 0 &&
  129. innerComments.length === 0
  130. ) {
  131. context.report({
  132. node,
  133. loc: node.body.loc.start,
  134. messageId: "unexpected",
  135. data: { name }
  136. });
  137. }
  138. }
  139. return {
  140. ArrowFunctionExpression: reportIfEmpty,
  141. FunctionDeclaration: reportIfEmpty,
  142. FunctionExpression: reportIfEmpty
  143. };
  144. }
  145. };