forked from palantir/tslint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
244 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright 2013 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. | ||
*/ | ||
|
||
/// <reference path='rule.ts'/> | ||
/// <reference path='abstractRule.ts'/> | ||
|
||
module Lint.Rules { | ||
export class NoUnreachableRule extends AbstractRule { | ||
public static FAILURE_STRING = "unreachable code"; | ||
|
||
public apply(syntaxTree: TypeScript.SyntaxTree): RuleFailure[] { | ||
return this.applyWithWalker(new UnreachableWalker(syntaxTree)); | ||
} | ||
} | ||
|
||
class UnreachableWalker extends Lint.RuleWalker { | ||
private hasReturned: boolean; | ||
|
||
constructor(syntaxTree: TypeScript.SyntaxTree) { | ||
super(syntaxTree); | ||
this.hasReturned = false; | ||
} | ||
|
||
public visitNode(node: TypeScript.SyntaxNode): void { | ||
if (this.hasReturned) { | ||
this.hasReturned = false; | ||
var position = this.position() + node.leadingTriviaWidth(); | ||
this.addFailure(this.createFailure(position, node.width(), NoUnreachableRule.FAILURE_STRING)); | ||
} | ||
|
||
super.visitNode(node); | ||
} | ||
|
||
public visitBlock(node: TypeScript.BlockSyntax): void { | ||
this.hasReturned = false; | ||
super.visitBlock(node); | ||
this.hasReturned = false; | ||
} | ||
|
||
public visitBreakStatement(node: TypeScript.BreakStatementSyntax): void { | ||
super.visitBreakStatement(node); | ||
this.hasReturned = true; | ||
} | ||
|
||
public visitContinueStatement(node: TypeScript.ContinueStatementSyntax): void { | ||
super.visitContinueStatement(node); | ||
this.hasReturned = true; | ||
} | ||
|
||
public visitReturnStatement(node: TypeScript.ReturnStatementSyntax): void { | ||
super.visitReturnStatement(node); | ||
this.hasReturned = true; | ||
} | ||
|
||
public visitThrowStatement(node: TypeScript.ThrowStatementSyntax): void { | ||
super.visitThrowStatement(node); | ||
this.hasReturned = true; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// invalid code | ||
|
||
function f1() { | ||
var x = 3; | ||
return; | ||
var y; | ||
var z; | ||
} | ||
|
||
var f2 = () => { | ||
if (x === 3) { | ||
throw new Error("error"); | ||
"123"; | ||
} else { | ||
return y; | ||
} | ||
|
||
return 123; | ||
}; | ||
|
||
lab: | ||
for (var i = 0; i < 10; ++i) { | ||
if (i === 3) { | ||
break; | ||
console.log("hi"); | ||
} else { | ||
continue lab; | ||
i = 4; | ||
} | ||
} | ||
|
||
// valid code | ||
var f2 = () => { | ||
if (x === 3) { | ||
throw new Error("error"); | ||
} else { | ||
return y; | ||
} | ||
|
||
return 123; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters