forked from palantir/tslint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnoUnsafeFinallyRule.ts
150 lines (133 loc) · 5.42 KB
/
noUnsafeFinallyRule.ts
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/**
* @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.
*/
import * as utils from "tsutils";
import * as ts from "typescript";
import * as Lint from "../index";
export class Rule extends Lint.Rules.AbstractRule {
/* tslint:disable:object-literal-sort-keys */
public static metadata: Lint.IRuleMetadata = {
ruleName: "no-unsafe-finally",
description: Lint.Utils.dedent`
Disallows control flow statements, such as \`return\`, \`continue\`,
\`break\` and \`throws\` in finally blocks.`,
descriptionDetails: "",
rationale: Lint.Utils.dedent`
When used inside \`finally\` blocks, control flow statements,
such as \`return\`, \`continue\`, \`break\` and \`throws\`
override any other control flow statements in the same try/catch scope.
This is confusing and unexpected behavior.`,
optionsDescription: "Not configurable.",
options: null,
optionExamples: [true],
type: "functionality",
typescriptOnly: false,
};
/* tslint:enable:object-literal-sort-keys */
public static FAILURE_STRING(name: string): string {
return `'${name}' statements in finally blocks are forbidden.`;
}
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithFunction(sourceFile, walk);
}
}
type JumpStatement = ts.BreakStatement | ts.ContinueStatement | ts.ThrowStatement | ts.ReturnStatement;
function walk(ctx: Lint.WalkContext<void>): void {
let inFinally = false;
ts.forEachChild(ctx.sourceFile, function cb(node: ts.Node): void {
switch (node.kind) {
case ts.SyntaxKind.TryStatement:
const { tryBlock, catchClause, finallyBlock } = node as ts.TryStatement;
ts.forEachChild(tryBlock, cb);
if (catchClause !== undefined) {
ts.forEachChild(catchClause, cb);
}
if (finallyBlock !== undefined) {
const 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 as JumpStatement)) {
ctx.addFailureAtNode(node, Rule.FAILURE_STRING(printJumpKind(node as JumpStatement)));
}
// falls through
default:
return ts.forEachChild(node, cb);
}
});
}
function jumpIsLocalToFinallyBlock(jump: JumpStatement): boolean {
const isBreakOrContinue = utils.isBreakOrContinueStatement(jump);
const label = isBreakOrContinue ? (jump as ts.BreakOrContinueStatement).label : undefined;
let node: ts.Node = jump;
// This should only be called inside a finally block, so we'll eventually reach the TryStatement case and return.
while (true) {
const parent = node.parent!;
switch (parent.kind) {
case ts.SyntaxKind.TryStatement:
if ((parent as ts.TryStatement).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: {
const { text } = (parent as ts.LabeledStatement).label;
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: JumpStatement): string {
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";
}
}