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.js 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /**
  2. * @fileoverview Rule to flag use of an empty block statement
  3. * @author Nicholas C. Zakas
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const astUtils = require("../util/ast-utils");
  10. //------------------------------------------------------------------------------
  11. // Rule Definition
  12. //------------------------------------------------------------------------------
  13. module.exports = {
  14. meta: {
  15. type: "suggestion",
  16. docs: {
  17. description: "disallow empty block statements",
  18. category: "Possible Errors",
  19. recommended: true,
  20. url: "https://eslint.org/docs/rules/no-empty"
  21. },
  22. schema: [
  23. {
  24. type: "object",
  25. properties: {
  26. allowEmptyCatch: {
  27. type: "boolean"
  28. }
  29. },
  30. additionalProperties: false
  31. }
  32. ],
  33. messages: {
  34. unexpected: "Empty {{type}} statement."
  35. }
  36. },
  37. create(context) {
  38. const options = context.options[0] || {},
  39. allowEmptyCatch = options.allowEmptyCatch || false;
  40. const sourceCode = context.getSourceCode();
  41. return {
  42. BlockStatement(node) {
  43. // if the body is not empty, we can just return immediately
  44. if (node.body.length !== 0) {
  45. return;
  46. }
  47. // a function is generally allowed to be empty
  48. if (astUtils.isFunction(node.parent)) {
  49. return;
  50. }
  51. if (allowEmptyCatch && node.parent.type === "CatchClause") {
  52. return;
  53. }
  54. // any other block is only allowed to be empty, if it contains a comment
  55. if (sourceCode.getCommentsInside(node).length > 0) {
  56. return;
  57. }
  58. context.report({ node, messageId: "unexpected", data: { type: "block" } });
  59. },
  60. SwitchStatement(node) {
  61. if (typeof node.cases === "undefined" || node.cases.length === 0) {
  62. context.report({ node, messageId: "unexpected", data: { type: "switch" } });
  63. }
  64. }
  65. };
  66. }
  67. };