Skip to content

Commit 2ba29d8

Browse files
committed
Support labeled statement
1 parent c4fddba commit 2ba29d8

6 files changed

Lines changed: 82 additions & 2 deletions

File tree

src/compiler/transformers/es2017.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ namespace ts {
9595
return visitForInStatementInAsyncBody(<ForInStatement>node);
9696
case SyntaxKind.ForOfStatement:
9797
return visitForOfStatementInAsyncBody(<ForOfStatement>node);
98+
case SyntaxKind.CatchClause:
99+
return visitCatchClauseInAsyncBody(<CatchClause>node);
98100
case SyntaxKind.Block:
99101
case SyntaxKind.SwitchStatement:
100102
case SyntaxKind.CaseBlock:
@@ -105,10 +107,10 @@ namespace ts {
105107
case SyntaxKind.WhileStatement:
106108
case SyntaxKind.IfStatement:
107109
case SyntaxKind.WithStatement:
110+
case SyntaxKind.LabeledStatement:
108111
return visitEachChild(node, asyncBodyVisitor, context);
109-
case SyntaxKind.CatchClause:
110-
return visitCatchClauseInAsyncBody(<CatchClause>node);
111112
}
113+
Debug.assert(!isNodeWithPossibleVarDeclaration(node), "Unhandled node.");
112114
return visitor(node);
113115
}
114116

src/compiler/utilities.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1761,6 +1761,35 @@ namespace ts {
17611761
return getAssignmentTargetKind(node) !== AssignmentKind.None;
17621762
}
17631763

1764+
/**
1765+
* Indicates whether a node could contain embedded statements that share the same `var`
1766+
* declaration scope as its parent.
1767+
*/
1768+
export function isNodeWithPossibleVarDeclaration(node: Node) {
1769+
switch (node.kind) {
1770+
case SyntaxKind.SourceFile:
1771+
case SyntaxKind.Block:
1772+
case SyntaxKind.WithStatement:
1773+
case SyntaxKind.IfStatement:
1774+
case SyntaxKind.SwitchStatement:
1775+
case SyntaxKind.CaseBlock:
1776+
case SyntaxKind.CaseClause:
1777+
case SyntaxKind.DefaultClause:
1778+
case SyntaxKind.LabeledStatement:
1779+
case SyntaxKind.ForStatement:
1780+
case SyntaxKind.ForInStatement:
1781+
case SyntaxKind.ForOfStatement:
1782+
case SyntaxKind.DoStatement:
1783+
case SyntaxKind.WhileStatement:
1784+
case SyntaxKind.TryStatement:
1785+
case SyntaxKind.CatchClause:
1786+
case SyntaxKind.ModuleDeclaration:
1787+
case SyntaxKind.ModuleBlock:
1788+
return true;
1789+
}
1790+
return false;
1791+
}
1792+
17641793
function walkUp(node: Node, kind: SyntaxKind) {
17651794
while (node && node.kind === kind) {
17661795
node = node.parent;

tests/baselines/reference/asyncWithVarShadowing_es6.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,13 @@ async function fn38(x) {
203203
case y:
204204
var x;
205205
}
206+
}
207+
208+
async function fn39(x) {
209+
foo: {
210+
var x;
211+
break foo;
212+
}
206213
}
207214

208215
//// [asyncWithVarShadowing_es6.js]
@@ -461,3 +468,11 @@ function fn38(x) {
461468
});
462469
var x;
463470
}
471+
function fn39(x) {
472+
return __awaiter(this, void 0, void 0, function* () {
473+
foo: {
474+
break foo;
475+
}
476+
});
477+
var x;
478+
}

tests/baselines/reference/asyncWithVarShadowing_es6.symbols

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,3 +404,15 @@ async function fn38(x) {
404404
>x : Symbol(x, Decl(asyncWithVarShadowing_es6.ts, 199, 20), Decl(asyncWithVarShadowing_es6.ts, 202, 15))
405405
}
406406
}
407+
408+
async function fn39(x) {
409+
>fn39 : Symbol(fn39, Decl(asyncWithVarShadowing_es6.ts, 204, 1))
410+
>x : Symbol(x, Decl(asyncWithVarShadowing_es6.ts, 206, 20), Decl(asyncWithVarShadowing_es6.ts, 208, 11))
411+
412+
foo: {
413+
var x;
414+
>x : Symbol(x, Decl(asyncWithVarShadowing_es6.ts, 206, 20), Decl(asyncWithVarShadowing_es6.ts, 208, 11))
415+
416+
break foo;
417+
}
418+
}

tests/baselines/reference/asyncWithVarShadowing_es6.types

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,3 +408,18 @@ async function fn38(x) {
408408
>x : any
409409
}
410410
}
411+
412+
async function fn39(x) {
413+
>fn39 : (x: any) => Promise<void>
414+
>x : any
415+
416+
foo: {
417+
>foo : any
418+
419+
var x;
420+
>x : any
421+
422+
break foo;
423+
>foo : any
424+
}
425+
}

tests/cases/conformance/async/es6/asyncWithVarShadowing_es6.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,4 +204,11 @@ async function fn38(x) {
204204
case y:
205205
var x;
206206
}
207+
}
208+
209+
async function fn39(x) {
210+
foo: {
211+
var x;
212+
break foo;
213+
}
207214
}

0 commit comments

Comments
 (0)