.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / noUnsafeFinallyRule.js
1 "use strict";
2 /**
3  * @license
4  * Copyright 2018 Palantir Technologies, Inc.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 Object.defineProperty(exports, "__esModule", { value: true });
19 var tslib_1 = require("tslib");
20 var utils = require("tsutils");
21 var ts = require("typescript");
22 var Lint = require("../index");
23 var utils_1 = require("../utils");
24 var Rule = /** @class */ (function (_super) {
25     tslib_1.__extends(Rule, _super);
26     function Rule() {
27         return _super !== null && _super.apply(this, arguments) || this;
28     }
29     /* tslint:enable:object-literal-sort-keys */
30     Rule.FAILURE_STRING = function (name) {
31         return "'" + name + "' statements in finally blocks are forbidden.";
32     };
33     Rule.prototype.apply = function (sourceFile) {
34         return this.applyWithFunction(sourceFile, walk);
35     };
36     /* tslint:disable:object-literal-sort-keys */
37     Rule.metadata = {
38         ruleName: "no-unsafe-finally",
39         description: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n            Disallows control flow statements, such as `return`, `continue`,\n            `break` and `throws` in finally blocks."], ["\n            Disallows control flow statements, such as \\`return\\`, \\`continue\\`,\n            \\`break\\` and \\`throws\\` in finally blocks."]))),
40         descriptionDetails: "",
41         rationale: Lint.Utils.dedent(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["\n            When used inside `finally` blocks, control flow statements,\n            such as `return`, `continue`, `break` and `throws`\n            override any other control flow statements in the same try/catch scope.\n            This is confusing and unexpected behavior."], ["\n            When used inside \\`finally\\` blocks, control flow statements,\n            such as \\`return\\`, \\`continue\\`, \\`break\\` and \\`throws\\`\n            override any other control flow statements in the same try/catch scope.\n            This is confusing and unexpected behavior."]))),
42         optionsDescription: "Not configurable.",
43         options: null,
44         optionExamples: [true],
45         type: "functionality",
46         typescriptOnly: false,
47     };
48     return Rule;
49 }(Lint.Rules.AbstractRule));
50 exports.Rule = Rule;
51 function walk(ctx) {
52     var inFinally = false;
53     ts.forEachChild(ctx.sourceFile, function cb(node) {
54         switch (node.kind) {
55             case ts.SyntaxKind.TryStatement:
56                 var _a = node, tryBlock = _a.tryBlock, catchClause = _a.catchClause, finallyBlock = _a.finallyBlock;
57                 ts.forEachChild(tryBlock, cb);
58                 if (catchClause !== undefined) {
59                     ts.forEachChild(catchClause, cb);
60                 }
61                 if (finallyBlock !== undefined) {
62                     var old = inFinally;
63                     inFinally = true;
64                     cb(finallyBlock);
65                     inFinally = old;
66                 }
67                 break;
68             case ts.SyntaxKind.BreakStatement:
69             case ts.SyntaxKind.ContinueStatement:
70             case ts.SyntaxKind.ThrowStatement:
71             case ts.SyntaxKind.ReturnStatement:
72                 if (inFinally && !jumpIsLocalToFinallyBlock(node)) {
73                     ctx.addFailureAtNode(node, Rule.FAILURE_STRING(printJumpKind(node)));
74                 }
75             // falls through
76             default:
77                 return ts.forEachChild(node, cb);
78         }
79     });
80 }
81 function jumpIsLocalToFinallyBlock(jump) {
82     var isBreakOrContinue = utils.isBreakOrContinueStatement(jump);
83     var label = isBreakOrContinue ? jump.label : undefined;
84     var node = jump;
85     // This should only be called inside a finally block, so we'll eventually reach the TryStatement case and return.
86     while (true) {
87         var parent = node.parent;
88         switch (parent.kind) {
89             case ts.SyntaxKind.TryStatement:
90                 if (parent.finallyBlock === node) {
91                     return false;
92                 }
93                 break;
94             case ts.SyntaxKind.SwitchStatement:
95                 if (jump.kind === ts.SyntaxKind.BreakStatement && label === undefined) {
96                     return true;
97                 }
98                 break;
99             case ts.SyntaxKind.ForInStatement:
100             case ts.SyntaxKind.ForOfStatement:
101             case ts.SyntaxKind.ForStatement:
102             case ts.SyntaxKind.WhileStatement:
103             case ts.SyntaxKind.DoStatement:
104                 if (isBreakOrContinue && label === undefined) {
105                     return true;
106                 }
107                 break;
108             case ts.SyntaxKind.LabeledStatement: {
109                 var text = parent.label.text;
110                 if (label !== undefined && label.text === text) {
111                     return true;
112                 }
113                 break;
114             }
115             default:
116                 // tslint:disable:deprecation This is needed for https://github.com/palantir/tslint/pull/4274 and will be fixed once TSLint
117                 // requires tsutils > 3.0.
118                 if (utils_1.isFunctionScopeBoundary(parent)) {
119                     // tslint:enable:deprecation
120                     // Haven't seen TryStatement yet, so the function is inside it.
121                     // No jump statement can escape a function, so the jump is local.
122                     return true;
123                 }
124         }
125         node = parent;
126     }
127 }
128 function printJumpKind(node) {
129     switch (node.kind) {
130         case ts.SyntaxKind.BreakStatement:
131             return "break";
132         case ts.SyntaxKind.ContinueStatement:
133             return "continue";
134         case ts.SyntaxKind.ThrowStatement:
135             return "throw";
136         case ts.SyntaxKind.ReturnStatement:
137             return "return";
138     }
139 }
140 var templateObject_1, templateObject_2;