no-unsafe-finally.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. * @fileoverview Rule to flag unsafe statements in finally block
  3. * @author Onur Temizkan
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Helpers
  8. //------------------------------------------------------------------------------
  9. const SENTINEL_NODE_TYPE_RETURN_THROW =
  10. /^(?:Program|(?:Function|Class)(?:Declaration|Expression)|ArrowFunctionExpression)$/u;
  11. const SENTINEL_NODE_TYPE_BREAK =
  12. /^(?:Program|(?:Function|Class)(?:Declaration|Expression)|ArrowFunctionExpression|DoWhileStatement|WhileStatement|ForOfStatement|ForInStatement|ForStatement|SwitchStatement)$/u;
  13. const SENTINEL_NODE_TYPE_CONTINUE =
  14. /^(?:Program|(?:Function|Class)(?:Declaration|Expression)|ArrowFunctionExpression|DoWhileStatement|WhileStatement|ForOfStatement|ForInStatement|ForStatement)$/u;
  15. //------------------------------------------------------------------------------
  16. // Rule Definition
  17. //------------------------------------------------------------------------------
  18. /** @type {import('../types').Rule.RuleModule} */
  19. module.exports = {
  20. meta: {
  21. type: "problem",
  22. docs: {
  23. description: "Disallow control flow statements in `finally` blocks",
  24. recommended: true,
  25. url: "https://eslint.org/docs/latest/rules/no-unsafe-finally",
  26. },
  27. schema: [],
  28. messages: {
  29. unsafeUsage: "Unsafe usage of {{nodeType}}.",
  30. },
  31. },
  32. create(context) {
  33. /**
  34. * Checks if the node is the finalizer of a TryStatement
  35. * @param {ASTNode} node node to check.
  36. * @returns {boolean} - true if the node is the finalizer of a TryStatement
  37. */
  38. function isFinallyBlock(node) {
  39. return (
  40. node.parent.type === "TryStatement" &&
  41. node.parent.finalizer === node
  42. );
  43. }
  44. /**
  45. * Climbs up the tree if the node is not a sentinel node
  46. * @param {ASTNode} node node to check.
  47. * @param {string} label label of the break or continue statement
  48. * @returns {boolean} - return whether the node is a finally block or a sentinel node
  49. */
  50. function isInFinallyBlock(node, label) {
  51. let labelInside = false;
  52. let sentinelNodeType;
  53. if (node.type === "BreakStatement" && !node.label) {
  54. sentinelNodeType = SENTINEL_NODE_TYPE_BREAK;
  55. } else if (node.type === "ContinueStatement") {
  56. sentinelNodeType = SENTINEL_NODE_TYPE_CONTINUE;
  57. } else {
  58. sentinelNodeType = SENTINEL_NODE_TYPE_RETURN_THROW;
  59. }
  60. for (
  61. let currentNode = node;
  62. currentNode && !sentinelNodeType.test(currentNode.type);
  63. currentNode = currentNode.parent
  64. ) {
  65. if (
  66. currentNode.parent.label &&
  67. label &&
  68. currentNode.parent.label.name === label.name
  69. ) {
  70. labelInside = true;
  71. }
  72. if (isFinallyBlock(currentNode)) {
  73. if (label && labelInside) {
  74. return false;
  75. }
  76. return true;
  77. }
  78. }
  79. return false;
  80. }
  81. /**
  82. * Checks whether the possibly-unsafe statement is inside a finally block.
  83. * @param {ASTNode} node node to check.
  84. * @returns {void}
  85. */
  86. function check(node) {
  87. if (isInFinallyBlock(node, node.label)) {
  88. context.report({
  89. messageId: "unsafeUsage",
  90. data: {
  91. nodeType: node.type,
  92. },
  93. node,
  94. line: node.loc.line,
  95. column: node.loc.column,
  96. });
  97. }
  98. }
  99. return {
  100. ReturnStatement: check,
  101. ThrowStatement: check,
  102. BreakStatement: check,
  103. ContinueStatement: check,
  104. };
  105. },
  106. };