noUnreachableRule.js
3.57 KB
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
"use strict";
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var ts = require("typescript");
var Lint = require("../lint");
var Rule = (function (_super) {
__extends(Rule, _super);
function Rule() {
_super.apply(this, arguments);
}
Rule.prototype.apply = function (sourceFile) {
return this.applyWithWalker(new NoUnreachableWalker(sourceFile, this.getOptions()));
};
Rule.metadata = {
ruleName: "no-unreachable",
description: "Disallows unreachable code after `break`, `catch`, `throw`, and `return` statements.",
rationale: "Unreachable code is often indication of a logic error.",
optionsDescription: "Not configurable.",
options: null,
optionExamples: ["true"],
type: "functionality",
};
Rule.FAILURE_STRING = "unreachable code";
return Rule;
}(Lint.Rules.AbstractRule));
exports.Rule = Rule;
var NoUnreachableWalker = (function (_super) {
__extends(NoUnreachableWalker, _super);
function NoUnreachableWalker(sourceFile, options) {
_super.call(this, sourceFile, options);
this.hasReturned = false;
}
NoUnreachableWalker.prototype.visitNode = function (node) {
var previousReturned = this.hasReturned;
if (node.kind === ts.SyntaxKind.FunctionDeclaration || node.kind === ts.SyntaxKind.TypeAliasDeclaration) {
this.hasReturned = false;
}
if (this.hasReturned) {
this.hasReturned = false;
this.addFailure(this.createFailure(node.getStart(), node.getWidth(), Rule.FAILURE_STRING));
}
_super.prototype.visitNode.call(this, node);
if (node.kind === ts.SyntaxKind.FunctionDeclaration || node.kind === ts.SyntaxKind.TypeAliasDeclaration) {
this.hasReturned = previousReturned;
}
};
NoUnreachableWalker.prototype.visitBlock = function (node) {
_super.prototype.visitBlock.call(this, node);
this.hasReturned = false;
};
NoUnreachableWalker.prototype.visitCaseClause = function (node) {
_super.prototype.visitCaseClause.call(this, node);
this.hasReturned = false;
};
NoUnreachableWalker.prototype.visitDefaultClause = function (node) {
_super.prototype.visitDefaultClause.call(this, node);
this.hasReturned = false;
};
NoUnreachableWalker.prototype.visitIfStatement = function (node) {
this.visitNode(node.expression);
this.visitNode(node.thenStatement);
this.hasReturned = false;
if (node.elseStatement != null) {
this.visitNode(node.elseStatement);
this.hasReturned = false;
}
};
NoUnreachableWalker.prototype.visitBreakStatement = function (node) {
_super.prototype.visitBreakStatement.call(this, node);
this.hasReturned = true;
};
NoUnreachableWalker.prototype.visitContinueStatement = function (node) {
_super.prototype.visitContinueStatement.call(this, node);
this.hasReturned = true;
};
NoUnreachableWalker.prototype.visitReturnStatement = function (node) {
_super.prototype.visitReturnStatement.call(this, node);
this.hasReturned = true;
};
NoUnreachableWalker.prototype.visitThrowStatement = function (node) {
_super.prototype.visitThrowStatement.call(this, node);
this.hasReturned = true;
};
return NoUnreachableWalker;
}(Lint.RuleWalker));