Actualizacion maquina principal
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / eslint / lib / rules / no-unsafe-finally.js
1 /**
2  * @fileoverview Rule to flag unsafe statements in finally block
3  * @author Onur Temizkan
4  */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Helpers
10 //------------------------------------------------------------------------------
11
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;
15
16
17 //------------------------------------------------------------------------------
18 // Rule Definition
19 //------------------------------------------------------------------------------
20
21 module.exports = {
22     meta: {
23         type: "problem",
24
25         docs: {
26             description: "disallow control flow statements in `finally` blocks",
27             category: "Possible Errors",
28             recommended: true,
29             url: "https://eslint.org/docs/rules/no-unsafe-finally"
30         },
31
32         schema: []
33     },
34     create(context) {
35
36         /**
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
40          */
41         function isFinallyBlock(node) {
42             return node.parent.type === "TryStatement" && node.parent.finalizer === node;
43         }
44
45         /**
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
50          */
51         function isInFinallyBlock(node, label) {
52             let labelInside = false;
53             let sentinelNodeType;
54
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;
59             } else {
60                 sentinelNodeType = SENTINEL_NODE_TYPE_RETURN_THROW;
61             }
62
63             for (
64                 let currentNode = node;
65                 currentNode && !sentinelNodeType.test(currentNode.type);
66                 currentNode = currentNode.parent
67             ) {
68                 if (currentNode.parent.label && label && (currentNode.parent.label.name === label.name)) {
69                     labelInside = true;
70                 }
71                 if (isFinallyBlock(currentNode)) {
72                     if (label && labelInside) {
73                         return false;
74                     }
75                     return true;
76                 }
77             }
78             return false;
79         }
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                     message: "Unsafe usage of {{nodeType}}.",
90                     data: {
91                         nodeType: node.type
92                     },
93                     node,
94                     line: node.loc.line,
95                     column: node.loc.column
96                 });
97             }
98         }
99
100         return {
101             ReturnStatement: check,
102             ThrowStatement: check,
103             BreakStatement: check,
104             ContinueStatement: check
105         };
106     }
107 };