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 //------------------------------------------------------------------------------
21 /** @type {import('../shared/types').Rule} */
27 description
: "Disallow control flow statements in `finally` blocks",
29 url
: "https://eslint.org/docs/rules/no-unsafe-finally"
35 unsafeUsage
: "Unsafe usage of {{nodeType}}."
41 * Checks if the node is the finalizer of a TryStatement
42 * @param {ASTNode} node node to check.
43 * @returns {boolean} - true if the node is the finalizer of a TryStatement
45 function isFinallyBlock(node
) {
46 return node
.parent
.type
=== "TryStatement" && node
.parent
.finalizer
=== node
;
50 * Climbs up the tree if the node is not a sentinel node
51 * @param {ASTNode} node node to check.
52 * @param {string} label label of the break or continue statement
53 * @returns {boolean} - return whether the node is a finally block or a sentinel node
55 function isInFinallyBlock(node
, label
) {
56 let labelInside
= false;
59 if (node
.type
=== "BreakStatement" && !node
.label
) {
60 sentinelNodeType
= SENTINEL_NODE_TYPE_BREAK
;
61 } else if (node
.type
=== "ContinueStatement") {
62 sentinelNodeType
= SENTINEL_NODE_TYPE_CONTINUE
;
64 sentinelNodeType
= SENTINEL_NODE_TYPE_RETURN_THROW
;
68 let currentNode
= node
;
69 currentNode
&& !sentinelNodeType
.test(currentNode
.type
);
70 currentNode
= currentNode
.parent
72 if (currentNode
.parent
.label
&& label
&& (currentNode
.parent
.label
.name
=== label
.name
)) {
75 if (isFinallyBlock(currentNode
)) {
76 if (label
&& labelInside
) {
86 * Checks whether the possibly-unsafe statement is inside a finally block.
87 * @param {ASTNode} node node to check.
90 function check(node
) {
91 if (isInFinallyBlock(node
, node
.label
)) {
93 messageId
: "unsafeUsage",
99 column
: node
.loc
.column
105 ReturnStatement
: check
,
106 ThrowStatement
: check
,
107 BreakStatement
: check
,
108 ContinueStatement
: check