no-lone-blocks.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**
  2. * @fileoverview Rule to flag blocks with no reason to exist
  3. * @author Brandon Mills
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. /** @type {import('../shared/types').Rule} */
  10. module.exports = {
  11. meta: {
  12. type: "suggestion",
  13. docs: {
  14. description: "Disallow unnecessary nested blocks",
  15. recommended: false,
  16. url: "https://eslint.org/docs/rules/no-lone-blocks"
  17. },
  18. schema: [],
  19. messages: {
  20. redundantBlock: "Block is redundant.",
  21. redundantNestedBlock: "Nested block is redundant."
  22. }
  23. },
  24. create(context) {
  25. // A stack of lone blocks to be checked for block-level bindings
  26. const loneBlocks = [];
  27. let ruleDef;
  28. /**
  29. * Reports a node as invalid.
  30. * @param {ASTNode} node The node to be reported.
  31. * @returns {void}
  32. */
  33. function report(node) {
  34. const messageId = node.parent.type === "BlockStatement" || node.parent.type === "StaticBlock"
  35. ? "redundantNestedBlock"
  36. : "redundantBlock";
  37. context.report({
  38. node,
  39. messageId
  40. });
  41. }
  42. /**
  43. * Checks for any occurrence of a BlockStatement in a place where lists of statements can appear
  44. * @param {ASTNode} node The node to check
  45. * @returns {boolean} True if the node is a lone block.
  46. */
  47. function isLoneBlock(node) {
  48. return node.parent.type === "BlockStatement" ||
  49. node.parent.type === "StaticBlock" ||
  50. node.parent.type === "Program" ||
  51. // Don't report blocks in switch cases if the block is the only statement of the case.
  52. node.parent.type === "SwitchCase" && !(node.parent.consequent[0] === node && node.parent.consequent.length === 1);
  53. }
  54. /**
  55. * Checks the enclosing block of the current node for block-level bindings,
  56. * and "marks it" as valid if any.
  57. * @returns {void}
  58. */
  59. function markLoneBlock() {
  60. if (loneBlocks.length === 0) {
  61. return;
  62. }
  63. const block = context.getAncestors().pop();
  64. if (loneBlocks[loneBlocks.length - 1] === block) {
  65. loneBlocks.pop();
  66. }
  67. }
  68. // Default rule definition: report all lone blocks
  69. ruleDef = {
  70. BlockStatement(node) {
  71. if (isLoneBlock(node)) {
  72. report(node);
  73. }
  74. }
  75. };
  76. // ES6: report blocks without block-level bindings, or that's only child of another block
  77. if (context.languageOptions.ecmaVersion >= 2015) {
  78. ruleDef = {
  79. BlockStatement(node) {
  80. if (isLoneBlock(node)) {
  81. loneBlocks.push(node);
  82. }
  83. },
  84. "BlockStatement:exit"(node) {
  85. if (loneBlocks.length > 0 && loneBlocks[loneBlocks.length - 1] === node) {
  86. loneBlocks.pop();
  87. report(node);
  88. } else if (
  89. (
  90. node.parent.type === "BlockStatement" ||
  91. node.parent.type === "StaticBlock"
  92. ) &&
  93. node.parent.body.length === 1
  94. ) {
  95. report(node);
  96. }
  97. }
  98. };
  99. ruleDef.VariableDeclaration = function(node) {
  100. if (node.kind === "let" || node.kind === "const") {
  101. markLoneBlock();
  102. }
  103. };
  104. ruleDef.FunctionDeclaration = function() {
  105. if (context.getScope().isStrict) {
  106. markLoneBlock();
  107. }
  108. };
  109. ruleDef.ClassDeclaration = markLoneBlock;
  110. }
  111. return ruleDef;
  112. }
  113. };