no-lone-blocks.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. module.exports = {
  10. meta: {
  11. type: "suggestion",
  12. docs: {
  13. description: "disallow unnecessary nested blocks",
  14. category: "Best Practices",
  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" ? "redundantNestedBlock" : "redundantBlock";
  35. context.report({
  36. node,
  37. messageId
  38. });
  39. }
  40. /**
  41. * Checks for any occurrence of a BlockStatement in a place where lists of statements can appear
  42. * @param {ASTNode} node The node to check
  43. * @returns {boolean} True if the node is a lone block.
  44. */
  45. function isLoneBlock(node) {
  46. return node.parent.type === "BlockStatement" ||
  47. node.parent.type === "Program" ||
  48. // Don't report blocks in switch cases if the block is the only statement of the case.
  49. node.parent.type === "SwitchCase" && !(node.parent.consequent[0] === node && node.parent.consequent.length === 1);
  50. }
  51. /**
  52. * Checks the enclosing block of the current node for block-level bindings,
  53. * and "marks it" as valid if any.
  54. * @returns {void}
  55. */
  56. function markLoneBlock() {
  57. if (loneBlocks.length === 0) {
  58. return;
  59. }
  60. const block = context.getAncestors().pop();
  61. if (loneBlocks[loneBlocks.length - 1] === block) {
  62. loneBlocks.pop();
  63. }
  64. }
  65. // Default rule definition: report all lone blocks
  66. ruleDef = {
  67. BlockStatement(node) {
  68. if (isLoneBlock(node)) {
  69. report(node);
  70. }
  71. }
  72. };
  73. // ES6: report blocks without block-level bindings, or that's only child of another block
  74. if (context.parserOptions.ecmaVersion >= 6) {
  75. ruleDef = {
  76. BlockStatement(node) {
  77. if (isLoneBlock(node)) {
  78. loneBlocks.push(node);
  79. }
  80. },
  81. "BlockStatement:exit"(node) {
  82. if (loneBlocks.length > 0 && loneBlocks[loneBlocks.length - 1] === node) {
  83. loneBlocks.pop();
  84. report(node);
  85. } else if (
  86. node.parent.type === "BlockStatement" &&
  87. node.parent.body.length === 1
  88. ) {
  89. report(node);
  90. }
  91. }
  92. };
  93. ruleDef.VariableDeclaration = function(node) {
  94. if (node.kind === "let" || node.kind === "const") {
  95. markLoneBlock();
  96. }
  97. };
  98. ruleDef.FunctionDeclaration = function() {
  99. if (context.getScope().isStrict) {
  100. markLoneBlock();
  101. }
  102. };
  103. ruleDef.ClassDeclaration = markLoneBlock;
  104. }
  105. return ruleDef;
  106. }
  107. };