2 * @fileoverview Rule to flag unsafe statements in finally block
3 * @author Onur Temizkan
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const SENTINEL_NODE_TYPE_RETURN_THROW = /^(?:Program|(?:Function|Class)(?:Declaration|Expression)|ArrowFunctionExpression)$/u;
13 const SENTINEL_NODE_TYPE_BREAK = /^(?:Program|(?:Function|Class)(?:Declaration|Expression)|ArrowFunctionExpression|DoWhileStatement|WhileStatement|ForOfStatement|ForInStatement|ForStatement|SwitchStatement)$/u;
14 const SENTINEL_NODE_TYPE_CONTINUE = /^(?:Program|(?:Function|Class)(?:Declaration|Expression)|ArrowFunctionExpression|DoWhileStatement|WhileStatement|ForOfStatement|ForInStatement|ForStatement)$/u;
17 //------------------------------------------------------------------------------
19 //------------------------------------------------------------------------------
26 description: "disallow control flow statements in `finally` blocks",
27 category: "Possible Errors",
29 url: "https://eslint.org/docs/rules/no-unsafe-finally"
37 * Checks if the node is the finalizer of a TryStatement
38 * @param {ASTNode} node node to check.
39 * @returns {boolean} - true if the node is the finalizer of a TryStatement
41 function isFinallyBlock(node) {
42 return node.parent.type === "TryStatement" && node.parent.finalizer === node;
46 * Climbs up the tree if the node is not a sentinel node
47 * @param {ASTNode} node node to check.
48 * @param {string} label label of the break or continue statement
49 * @returns {boolean} - return whether the node is a finally block or a sentinel node
51 function isInFinallyBlock(node, label) {
52 let labelInside = false;
55 if (node.type === "BreakStatement" && !node.label) {
56 sentinelNodeType = SENTINEL_NODE_TYPE_BREAK;
57 } else if (node.type === "ContinueStatement") {
58 sentinelNodeType = SENTINEL_NODE_TYPE_CONTINUE;
60 sentinelNodeType = SENTINEL_NODE_TYPE_RETURN_THROW;
64 let currentNode = node;
65 currentNode && !sentinelNodeType.test(currentNode.type);
66 currentNode = currentNode.parent
68 if (currentNode.parent.label && label && (currentNode.parent.label.name === label.name)) {
71 if (isFinallyBlock(currentNode)) {
72 if (label && labelInside) {
82 * Checks whether the possibly-unsafe statement is inside a finally block.
83 * @param {ASTNode} node node to check.
86 function check(node) {
87 if (isInFinallyBlock(node, node.label)) {
89 message: "Unsafe usage of {{nodeType}}.",
95 column: node.loc.column
101 ReturnStatement: check,
102 ThrowStatement: check,
103 BreakStatement: check,
104 ContinueStatement: check