Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

no-unreachable: allow type alias after function return #1183

Merged
merged 2 commits into from
Apr 29, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Down in line 54 we should restore the state of this.hasReturned like we do for function declarations

this.hasReturned = false;
}

Expand Down Expand Up @@ -99,4 +100,8 @@ class NoUnreachableWalker extends Lint.RuleWalker {
super.visitThrowStatement(node);
this.hasReturned = true;
}

public visitTypeAliasDeclaration(node: ts.TypeAliasDeclaration) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we actually need to add these lines - visitNode gets called on all nodes by default

this.visitNode(node);
}
}
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() {}
}