@@ -26,6 +26,7 @@ namespace ts {
2626 let enabledSubstitutions : ESNextSubstitutionFlags ;
2727 let enclosingFunctionFlags : FunctionFlags ;
2828 let enclosingSuperContainerFlags : NodeCheckFlags = 0 ;
29+ let topLevel : boolean ;
2930
3031 /** Keeps track of property names accessed on super (`super.x`) within async functions. */
3132 let capturedSuperProperties : UnderscoreEscapedMap < true > ;
@@ -42,6 +43,7 @@ namespace ts {
4243 }
4344
4445 exportedVariableStatement = false ;
46+ topLevel = isEffectiveStrictModeSourceFile ( node , compilerOptions ) ;
4547 const visited = visitEachChild ( node , visitor , context ) ;
4648 addEmitHelpers ( visited , context . readEmitHelpers ( ) ) ;
4749 return visited ;
@@ -62,6 +64,20 @@ namespace ts {
6264 return node ;
6365 }
6466
67+ function doOutsideOfTopLevel < T , U > ( cb : ( value : T ) => U , value : T ) {
68+ if ( topLevel ) {
69+ topLevel = false ;
70+ const result = cb ( value ) ;
71+ topLevel = true ;
72+ return result ;
73+ }
74+ return cb ( value ) ;
75+ }
76+
77+ function visitDefault ( node : Node ) : VisitResult < Node > {
78+ return visitEachChild ( node , visitor , context ) ;
79+ }
80+
6581 function visitorWorker ( node : Node , noDestructuringValue : boolean ) : VisitResult < Node > {
6682 if ( ( node . transformFlags & TransformFlags . ContainsES2018 ) === 0 ) {
6783 return node ;
@@ -92,17 +108,17 @@ namespace ts {
92108 case SyntaxKind . VoidExpression :
93109 return visitVoidExpression ( node as VoidExpression ) ;
94110 case SyntaxKind . Constructor :
95- return visitConstructorDeclaration ( node as ConstructorDeclaration ) ;
111+ return doOutsideOfTopLevel ( visitConstructorDeclaration , node as ConstructorDeclaration ) ;
96112 case SyntaxKind . MethodDeclaration :
97- return visitMethodDeclaration ( node as MethodDeclaration ) ;
113+ return doOutsideOfTopLevel ( visitMethodDeclaration , node as MethodDeclaration ) ;
98114 case SyntaxKind . GetAccessor :
99- return visitGetAccessorDeclaration ( node as GetAccessorDeclaration ) ;
115+ return doOutsideOfTopLevel ( visitGetAccessorDeclaration , node as GetAccessorDeclaration ) ;
100116 case SyntaxKind . SetAccessor :
101- return visitSetAccessorDeclaration ( node as SetAccessorDeclaration ) ;
117+ return doOutsideOfTopLevel ( visitSetAccessorDeclaration , node as SetAccessorDeclaration ) ;
102118 case SyntaxKind . FunctionDeclaration :
103- return visitFunctionDeclaration ( node as FunctionDeclaration ) ;
119+ return doOutsideOfTopLevel ( visitFunctionDeclaration , node as FunctionDeclaration ) ;
104120 case SyntaxKind . FunctionExpression :
105- return visitFunctionExpression ( node as FunctionExpression ) ;
121+ return doOutsideOfTopLevel ( visitFunctionExpression , node as FunctionExpression ) ;
106122 case SyntaxKind . ArrowFunction :
107123 return visitArrowFunction ( node as ArrowFunction ) ;
108124 case SyntaxKind . Parameter :
@@ -121,6 +137,9 @@ namespace ts {
121137 hasSuperElementAccess = true ;
122138 }
123139 return visitEachChild ( node , visitor , context ) ;
140+ case SyntaxKind . ClassDeclaration :
141+ case SyntaxKind . ClassExpression :
142+ return doOutsideOfTopLevel ( visitDefault , node ) ;
124143 default :
125144 return visitEachChild ( node , visitor , context ) ;
126145 }
@@ -754,7 +773,8 @@ namespace ts {
754773 node . body ! ,
755774 visitLexicalEnvironment ( node . body ! . statements , visitor , context , statementOffset )
756775 )
757- )
776+ ) ,
777+ ! topLevel
758778 )
759779 ) ;
760780
@@ -1057,7 +1077,7 @@ namespace ts {
10571077 };`
10581078 } ;
10591079
1060- function createAsyncGeneratorHelper ( context : TransformationContext , generatorFunc : FunctionExpression ) {
1080+ function createAsyncGeneratorHelper ( context : TransformationContext , generatorFunc : FunctionExpression , hasLexicalThis : boolean ) {
10611081 context . requestEmitHelper ( awaitHelper ) ;
10621082 context . requestEmitHelper ( asyncGeneratorHelper ) ;
10631083
@@ -1068,7 +1088,7 @@ namespace ts {
10681088 getUnscopedHelperName ( "__asyncGenerator" ) ,
10691089 /*typeArguments*/ undefined ,
10701090 [
1071- createThis ( ) ,
1091+ hasLexicalThis ? createThis ( ) : createVoidZero ( ) ,
10721092 createIdentifier ( "arguments" ) ,
10731093 generatorFunc
10741094 ]
0 commit comments