Skip to content

Commit

Permalink
no-unreachable: allow type alias after function return (palantir#1183)
Browse files Browse the repository at this point in the history
  • Loading branch information
YuichiNukiyama authored and tomduncalf committed Jun 14, 2016
1 parent 1ce8f7b commit b0e88a5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/rules/noUnreachableRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ class NoUnreachableWalker extends Lint.RuleWalker {

public visitNode(node: ts.Node) {
const previousReturned = this.hasReturned;
// function declarations can be hoisted -- so set hasReturned to false until we're done with the function
if (node.kind === ts.SyntaxKind.FunctionDeclaration) {
// function declarations and type alias declarations can be hoisted
// -- so set hasReturned to false until we're done with the function
if (node.kind === ts.SyntaxKind.FunctionDeclaration || node.kind === ts.SyntaxKind.TypeAliasDeclaration) {
this.hasReturned = false;
}

Expand All @@ -50,7 +51,7 @@ class NoUnreachableWalker extends Lint.RuleWalker {

// if there is further code after the hoisted function and we returned before that code is unreachable
// so reset hasReturned to its previous state to check for that
if (node.kind === ts.SyntaxKind.FunctionDeclaration) {
if (node.kind === ts.SyntaxKind.FunctionDeclaration || node.kind === ts.SyntaxKind.TypeAliasDeclaration) {
this.hasReturned = previousReturned;
}
}
Expand Down
14 changes: 14 additions & 0 deletions test/rules/no-unreachable/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,18 @@ function f8() {
} catch (e) {
console.log("here");
}
}

// valid case

function f9() {
return 1;
function bar() {}
type Bar = string;
}

function f10() {
type Bar = string;
return 1;
function bar() {}
}

0 comments on commit b0e88a5

Please sign in to comment.