From 2ba29d8d9d923d99b828a999b99c47db9cd1184f Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Wed, 17 Jan 2018 11:24:28 -0800 Subject: [PATCH] Support labeled statement --- src/compiler/transformers/es2017.ts | 6 ++-- src/compiler/utilities.ts | 29 +++++++++++++++++++ .../reference/asyncWithVarShadowing_es6.js | 15 ++++++++++ .../asyncWithVarShadowing_es6.symbols | 12 ++++++++ .../reference/asyncWithVarShadowing_es6.types | 15 ++++++++++ .../async/es6/asyncWithVarShadowing_es6.ts | 7 +++++ 6 files changed, 82 insertions(+), 2 deletions(-) diff --git a/src/compiler/transformers/es2017.ts b/src/compiler/transformers/es2017.ts index f1bd671d461..79668dc0b8c 100644 --- a/src/compiler/transformers/es2017.ts +++ b/src/compiler/transformers/es2017.ts @@ -95,6 +95,8 @@ namespace ts { return visitForInStatementInAsyncBody(node); case SyntaxKind.ForOfStatement: return visitForOfStatementInAsyncBody(node); + case SyntaxKind.CatchClause: + return visitCatchClauseInAsyncBody(node); case SyntaxKind.Block: case SyntaxKind.SwitchStatement: case SyntaxKind.CaseBlock: @@ -105,10 +107,10 @@ namespace ts { case SyntaxKind.WhileStatement: case SyntaxKind.IfStatement: case SyntaxKind.WithStatement: + case SyntaxKind.LabeledStatement: return visitEachChild(node, asyncBodyVisitor, context); - case SyntaxKind.CatchClause: - return visitCatchClauseInAsyncBody(node); } + Debug.assert(!isNodeWithPossibleVarDeclaration(node), "Unhandled node."); return visitor(node); } diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 796ed35a861..5510353d1da 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -1761,6 +1761,35 @@ namespace ts { return getAssignmentTargetKind(node) !== AssignmentKind.None; } + /** + * Indicates whether a node could contain embedded statements that share the same `var` + * declaration scope as its parent. + */ + export function isNodeWithPossibleVarDeclaration(node: Node) { + switch (node.kind) { + case SyntaxKind.SourceFile: + case SyntaxKind.Block: + case SyntaxKind.WithStatement: + case SyntaxKind.IfStatement: + case SyntaxKind.SwitchStatement: + case SyntaxKind.CaseBlock: + case SyntaxKind.CaseClause: + case SyntaxKind.DefaultClause: + case SyntaxKind.LabeledStatement: + case SyntaxKind.ForStatement: + case SyntaxKind.ForInStatement: + case SyntaxKind.ForOfStatement: + case SyntaxKind.DoStatement: + case SyntaxKind.WhileStatement: + case SyntaxKind.TryStatement: + case SyntaxKind.CatchClause: + case SyntaxKind.ModuleDeclaration: + case SyntaxKind.ModuleBlock: + return true; + } + return false; + } + function walkUp(node: Node, kind: SyntaxKind) { while (node && node.kind === kind) { node = node.parent; diff --git a/tests/baselines/reference/asyncWithVarShadowing_es6.js b/tests/baselines/reference/asyncWithVarShadowing_es6.js index 726bf935cf6..e362a74072d 100644 --- a/tests/baselines/reference/asyncWithVarShadowing_es6.js +++ b/tests/baselines/reference/asyncWithVarShadowing_es6.js @@ -203,6 +203,13 @@ async function fn38(x) { case y: var x; } +} + +async function fn39(x) { + foo: { + var x; + break foo; + } } //// [asyncWithVarShadowing_es6.js] @@ -461,3 +468,11 @@ function fn38(x) { }); var x; } +function fn39(x) { + return __awaiter(this, void 0, void 0, function* () { + foo: { + break foo; + } + }); + var x; +} diff --git a/tests/baselines/reference/asyncWithVarShadowing_es6.symbols b/tests/baselines/reference/asyncWithVarShadowing_es6.symbols index aec43c553a5..f3fd27e8971 100644 --- a/tests/baselines/reference/asyncWithVarShadowing_es6.symbols +++ b/tests/baselines/reference/asyncWithVarShadowing_es6.symbols @@ -404,3 +404,15 @@ async function fn38(x) { >x : Symbol(x, Decl(asyncWithVarShadowing_es6.ts, 199, 20), Decl(asyncWithVarShadowing_es6.ts, 202, 15)) } } + +async function fn39(x) { +>fn39 : Symbol(fn39, Decl(asyncWithVarShadowing_es6.ts, 204, 1)) +>x : Symbol(x, Decl(asyncWithVarShadowing_es6.ts, 206, 20), Decl(asyncWithVarShadowing_es6.ts, 208, 11)) + + foo: { + var x; +>x : Symbol(x, Decl(asyncWithVarShadowing_es6.ts, 206, 20), Decl(asyncWithVarShadowing_es6.ts, 208, 11)) + + break foo; + } +} diff --git a/tests/baselines/reference/asyncWithVarShadowing_es6.types b/tests/baselines/reference/asyncWithVarShadowing_es6.types index 42a81f168b0..5544c8be2c6 100644 --- a/tests/baselines/reference/asyncWithVarShadowing_es6.types +++ b/tests/baselines/reference/asyncWithVarShadowing_es6.types @@ -408,3 +408,18 @@ async function fn38(x) { >x : any } } + +async function fn39(x) { +>fn39 : (x: any) => Promise +>x : any + + foo: { +>foo : any + + var x; +>x : any + + break foo; +>foo : any + } +} diff --git a/tests/cases/conformance/async/es6/asyncWithVarShadowing_es6.ts b/tests/cases/conformance/async/es6/asyncWithVarShadowing_es6.ts index 017be33edb4..7c9a9846376 100644 --- a/tests/cases/conformance/async/es6/asyncWithVarShadowing_es6.ts +++ b/tests/cases/conformance/async/es6/asyncWithVarShadowing_es6.ts @@ -204,4 +204,11 @@ async function fn38(x) { case y: var x; } +} + +async function fn39(x) { + foo: { + var x; + break foo; + } } \ No newline at end of file