no-unsafe-finally.js 3.8 KB

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