4 * Copyright 2018 Palantir Technologies, Inc.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
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);
27 return _super !== null && _super.apply(this, arguments) || this;
29 /* tslint:enable:object-literal-sort-keys */
30 Rule.FAILURE_STRING = function (name) {
31 return "'" + name + "' statements in finally blocks are forbidden.";
33 Rule.prototype.apply = function (sourceFile) {
34 return this.applyWithFunction(sourceFile, walk);
36 /* tslint:disable:object-literal-sort-keys */
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.",
44 optionExamples: [true],
45 type: "functionality",
46 typescriptOnly: false,
49 }(Lint.Rules.AbstractRule));
52 var inFinally = false;
53 ts.forEachChild(ctx.sourceFile, function cb(node) {
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);
61 if (finallyBlock !== undefined) {
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)));
77 return ts.forEachChild(node, cb);
81 function jumpIsLocalToFinallyBlock(jump) {
82 var isBreakOrContinue = utils.isBreakOrContinueStatement(jump);
83 var label = isBreakOrContinue ? jump.label : undefined;
85 // This should only be called inside a finally block, so we'll eventually reach the TryStatement case and return.
87 var parent = node.parent;
88 switch (parent.kind) {
89 case ts.SyntaxKind.TryStatement:
90 if (parent.finallyBlock === node) {
94 case ts.SyntaxKind.SwitchStatement:
95 if (jump.kind === ts.SyntaxKind.BreakStatement && label === undefined) {
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) {
108 case ts.SyntaxKind.LabeledStatement: {
109 var text = parent.label.text;
110 if (label !== undefined && label.text === text) {
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.
128 function printJumpKind(node) {
130 case ts.SyntaxKind.BreakStatement:
132 case ts.SyntaxKind.ContinueStatement:
134 case ts.SyntaxKind.ThrowStatement:
136 case ts.SyntaxKind.ReturnStatement:
140 var templateObject_1, templateObject_2;