From 5b45db790729508553016572a5cddb8e806bc214 Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Wed, 17 Jan 2018 11:55:43 -0800 Subject: [PATCH] PR Feedback --- src/compiler/transformers/es2017.ts | 51 +++++++++++++++-------------- src/compiler/utilities.ts | 26 ++++++++++++--- 2 files changed, 49 insertions(+), 28 deletions(-) diff --git a/src/compiler/transformers/es2017.ts b/src/compiler/transformers/es2017.ts index 79668dc0b8c..905686c8861 100644 --- a/src/compiler/transformers/es2017.ts +++ b/src/compiler/transformers/es2017.ts @@ -86,31 +86,34 @@ namespace ts { } function asyncBodyVisitor(node: Node): VisitResult { - switch (node.kind) { - case SyntaxKind.VariableStatement: - return visitVariableStatementInAsyncBody(node); - case SyntaxKind.ForStatement: - return visitForStatementInAsyncBody(node); - case SyntaxKind.ForInStatement: - return visitForInStatementInAsyncBody(node); - case SyntaxKind.ForOfStatement: - return visitForOfStatementInAsyncBody(node); - case SyntaxKind.CatchClause: - return visitCatchClauseInAsyncBody(node); - case SyntaxKind.Block: - case SyntaxKind.SwitchStatement: - case SyntaxKind.CaseBlock: - case SyntaxKind.CaseClause: - case SyntaxKind.DefaultClause: - case SyntaxKind.TryStatement: - case SyntaxKind.DoStatement: - case SyntaxKind.WhileStatement: - case SyntaxKind.IfStatement: - case SyntaxKind.WithStatement: - case SyntaxKind.LabeledStatement: - return visitEachChild(node, asyncBodyVisitor, context); + if (isNodeWithPossibleHoistedDeclaration(node)) { + switch (node.kind) { + case SyntaxKind.VariableStatement: + return visitVariableStatementInAsyncBody(node); + case SyntaxKind.ForStatement: + return visitForStatementInAsyncBody(node); + case SyntaxKind.ForInStatement: + return visitForInStatementInAsyncBody(node); + case SyntaxKind.ForOfStatement: + return visitForOfStatementInAsyncBody(node); + case SyntaxKind.CatchClause: + return visitCatchClauseInAsyncBody(node); + case SyntaxKind.Block: + case SyntaxKind.SwitchStatement: + case SyntaxKind.CaseBlock: + case SyntaxKind.CaseClause: + case SyntaxKind.DefaultClause: + case SyntaxKind.TryStatement: + case SyntaxKind.DoStatement: + case SyntaxKind.WhileStatement: + case SyntaxKind.IfStatement: + case SyntaxKind.WithStatement: + case SyntaxKind.LabeledStatement: + return visitEachChild(node, asyncBodyVisitor, context); + default: + return Debug.assertNever(node, "Unhandled node."); + } } - Debug.assert(!isNodeWithPossibleVarDeclaration(node), "Unhandled node."); return visitor(node); } diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 7b80da8f639..eabc9921c1a 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -1761,14 +1761,32 @@ namespace ts { return getAssignmentTargetKind(node) !== AssignmentKind.None; } + export type NodeWithPossibleHoistedDeclaration = + | Block + | VariableStatement + | WithStatement + | IfStatement + | SwitchStatement + | CaseBlock + | CaseClause + | DefaultClause + | LabeledStatement + | ForStatement + | ForInStatement + | ForOfStatement + | DoStatement + | WhileStatement + | TryStatement + | CatchClause; + /** - * Indicates whether a node could contain embedded statements that share the same `var` - * declaration scope as its parent. + * Indicates whether a node could contain a `var` VariableDeclarationList that contributes to + * the same `var` declaration scope as the node's parent. */ - export function isNodeWithPossibleVarDeclaration(node: Node) { + export function isNodeWithPossibleHoistedDeclaration(node: Node): node is NodeWithPossibleHoistedDeclaration { switch (node.kind) { - case SyntaxKind.SourceFile: case SyntaxKind.Block: + case SyntaxKind.VariableStatement: case SyntaxKind.WithStatement: case SyntaxKind.IfStatement: case SyntaxKind.SwitchStatement: