no-unsafe-finally.js 3.9 KB

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