From edf8543f701aad29ab1013973b161f481f007948 Mon Sep 17 00:00:00 2001 From: Yuichi Nukiyama Date: Fri, 29 Apr 2016 13:17:22 +0900 Subject: [PATCH 1/2] no-unreachable: allow type alias after function return --- src/rules/noUnreachableRule.ts | 9 +++++++-- test/rules/no-unreachable/test.ts.lint | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/rules/noUnreachableRule.ts b/src/rules/noUnreachableRule.ts index 090d8d57962..1324d63aaca 100644 --- a/src/rules/noUnreachableRule.ts +++ b/src/rules/noUnreachableRule.ts @@ -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; } @@ -99,4 +100,8 @@ class NoUnreachableWalker extends Lint.RuleWalker { super.visitThrowStatement(node); this.hasReturned = true; } + + public visitTypeAliasDeclaration(node: ts.TypeAliasDeclaration) { + this.visitNode(node); + } } diff --git a/test/rules/no-unreachable/test.ts.lint b/test/rules/no-unreachable/test.ts.lint index 7f1c945214e..15d51b06714 100644 --- a/test/rules/no-unreachable/test.ts.lint +++ b/test/rules/no-unreachable/test.ts.lint @@ -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() {} } \ No newline at end of file From a7b47fbedd1cda4e24c7e40ec42f2155bd6a8b90 Mon Sep 17 00:00:00 2001 From: Yuichi Nukiyama Date: Sat, 30 Apr 2016 06:01:53 +0900 Subject: [PATCH 2/2] follow advise --- src/rules/noUnreachableRule.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/rules/noUnreachableRule.ts b/src/rules/noUnreachableRule.ts index 1324d63aaca..f8e3d742f77 100644 --- a/src/rules/noUnreachableRule.ts +++ b/src/rules/noUnreachableRule.ts @@ -51,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; } } @@ -100,8 +100,4 @@ class NoUnreachableWalker extends Lint.RuleWalker { super.visitThrowStatement(node); this.hasReturned = true; } - - public visitTypeAliasDeclaration(node: ts.TypeAliasDeclaration) { - this.visitNode(node); - } }