no-empty.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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("./utils/ast-utils");
  10. //------------------------------------------------------------------------------
  11. // Rule Definition
  12. //------------------------------------------------------------------------------
  13. /** @type {import('../types').Rule.RuleModule} */
  14. module.exports = {
  15. meta: {
  16. hasSuggestions: true,
  17. type: "suggestion",
  18. defaultOptions: [
  19. {
  20. allowEmptyCatch: false,
  21. },
  22. ],
  23. docs: {
  24. description: "Disallow empty block statements",
  25. recommended: true,
  26. url: "https://eslint.org/docs/latest/rules/no-empty",
  27. },
  28. schema: [
  29. {
  30. type: "object",
  31. properties: {
  32. allowEmptyCatch: {
  33. type: "boolean",
  34. },
  35. },
  36. additionalProperties: false,
  37. },
  38. ],
  39. messages: {
  40. unexpected: "Empty {{type}} statement.",
  41. suggestComment: "Add comment inside empty {{type}} statement.",
  42. },
  43. },
  44. create(context) {
  45. const [{ allowEmptyCatch }] = context.options;
  46. const sourceCode = context.sourceCode;
  47. return {
  48. BlockStatement(node) {
  49. // if the body is not empty, we can just return immediately
  50. if (node.body.length !== 0) {
  51. return;
  52. }
  53. // a function is generally allowed to be empty
  54. if (astUtils.isFunction(node.parent)) {
  55. return;
  56. }
  57. if (allowEmptyCatch && node.parent.type === "CatchClause") {
  58. return;
  59. }
  60. // any other block is only allowed to be empty, if it contains a comment
  61. if (sourceCode.getCommentsInside(node).length > 0) {
  62. return;
  63. }
  64. context.report({
  65. node,
  66. messageId: "unexpected",
  67. data: { type: "block" },
  68. suggest: [
  69. {
  70. messageId: "suggestComment",
  71. data: { type: "block" },
  72. fix(fixer) {
  73. const range = [
  74. node.range[0] + 1,
  75. node.range[1] - 1,
  76. ];
  77. return fixer.replaceTextRange(
  78. range,
  79. " /* empty */ ",
  80. );
  81. },
  82. },
  83. ],
  84. });
  85. },
  86. SwitchStatement(node) {
  87. if (
  88. typeof node.cases === "undefined" ||
  89. node.cases.length === 0
  90. ) {
  91. const openingBrace = sourceCode.getTokenAfter(
  92. node.discriminant,
  93. astUtils.isOpeningBraceToken,
  94. );
  95. const closingBrace = sourceCode.getLastToken(node);
  96. if (
  97. sourceCode.commentsExistBetween(
  98. openingBrace,
  99. closingBrace,
  100. )
  101. ) {
  102. return;
  103. }
  104. context.report({
  105. node,
  106. loc: {
  107. start: openingBrace.loc.start,
  108. end: closingBrace.loc.end,
  109. },
  110. messageId: "unexpected",
  111. data: { type: "switch" },
  112. suggest: [
  113. {
  114. messageId: "suggestComment",
  115. data: { type: "switch" },
  116. fix(fixer) {
  117. const range = [
  118. openingBrace.range[1],
  119. closingBrace.range[0],
  120. ];
  121. return fixer.replaceTextRange(
  122. range,
  123. " /* empty */ ",
  124. );
  125. },
  126. },
  127. ],
  128. });
  129. }
  130. },
  131. };
  132. },
  133. };