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