Skip to content

Commit 21c2c89

Browse files
authored
Merge pull request #10926 from Microsoft/fix10862
Fix super in down-level async method
2 parents e9178a5 + e50105b commit 21c2c89

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

src/compiler/transformers/es6.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace ts {
1010
/** Enables substitutions for block-scoped bindings. */
1111
BlockScopedBindings = 1 << 1,
1212
}
13+
1314
/**
1415
* If loop contains block scoped binding captured in some function then loop body is converted to a function.
1516
* Lexical bindings declared in loop initializer will be passed into the loop body function as parameters,
@@ -166,6 +167,9 @@ namespace ts {
166167
let enclosingBlockScopeContainerParent: Node;
167168
let containingNonArrowFunction: FunctionLikeDeclaration | ClassElement;
168169

170+
/** Tracks the container that determines whether `super.x` is a static. */
171+
let superScopeContainer: FunctionLikeDeclaration | ClassElement;
172+
169173
/**
170174
* Used to track if we are emitting body of the converted loop
171175
*/
@@ -203,6 +207,7 @@ namespace ts {
203207

204208
function saveStateAndInvoke<T>(node: Node, f: (node: Node) => T): T {
205209
const savedContainingNonArrowFunction = containingNonArrowFunction;
210+
const savedSuperScopeContainer = superScopeContainer;
206211
const savedCurrentParent = currentParent;
207212
const savedCurrentNode = currentNode;
208213
const savedEnclosingBlockScopeContainer = enclosingBlockScopeContainer;
@@ -219,6 +224,7 @@ namespace ts {
219224

220225
convertedLoopState = savedConvertedLoopState;
221226
containingNonArrowFunction = savedContainingNonArrowFunction;
227+
superScopeContainer = savedSuperScopeContainer;
222228
currentParent = savedCurrentParent;
223229
currentNode = savedCurrentNode;
224230
enclosingBlockScopeContainer = savedEnclosingBlockScopeContainer;
@@ -414,13 +420,16 @@ namespace ts {
414420
}
415421

416422
switch (currentParent.kind) {
423+
case SyntaxKind.FunctionExpression:
417424
case SyntaxKind.Constructor:
418425
case SyntaxKind.MethodDeclaration:
419426
case SyntaxKind.GetAccessor:
420427
case SyntaxKind.SetAccessor:
421428
case SyntaxKind.FunctionDeclaration:
422-
case SyntaxKind.FunctionExpression:
423429
containingNonArrowFunction = <FunctionLikeDeclaration>currentParent;
430+
if (!(containingNonArrowFunction.emitFlags & NodeEmitFlags.AsyncFunctionBody)) {
431+
superScopeContainer = containingNonArrowFunction;
432+
}
424433
break;
425434
}
426435
}
@@ -2820,9 +2829,9 @@ namespace ts {
28202829
* Visits the `super` keyword
28212830
*/
28222831
function visitSuperKeyword(node: PrimaryExpression): LeftHandSideExpression {
2823-
return containingNonArrowFunction
2824-
&& isClassElement(containingNonArrowFunction)
2825-
&& !hasModifier(containingNonArrowFunction, ModifierFlags.Static)
2832+
return superScopeContainer
2833+
&& isClassElement(superScopeContainer)
2834+
&& !hasModifier(superScopeContainer, ModifierFlags.Static)
28262835
&& currentParent.kind !== SyntaxKind.CallExpression
28272836
? createPropertyAccess(createIdentifier("_super"), "prototype")
28282837
: createIdentifier("_super");

tests/baselines/reference/asyncMethodWithSuper_es5.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ var B = (function (_super) {
6969
var a, b;
7070
return __generator(this, function (_a) {
7171
// call with property access
72-
_super.x.call(this);
72+
_super.prototype.x.call(this);
7373
// call with element access
74-
_super["x"].call(this);
75-
a = _super.x;
76-
b = _super["x"];
74+
_super.prototype["x"].call(this);
75+
a = _super.prototype.x;
76+
b = _super.prototype["x"];
7777
return [2 /*return*/];
7878
});
7979
});
@@ -85,15 +85,15 @@ var B = (function (_super) {
8585
return __generator(this, function (_c) {
8686
f = function () { };
8787
// call with property access
88-
_super.x.call(this);
88+
_super.prototype.x.call(this);
8989
// call with element access
90-
_super["x"].call(this);
91-
a = _super.x;
92-
b = _super["x"];
90+
_super.prototype["x"].call(this);
91+
a = _super.prototype.x;
92+
b = _super.prototype["x"];
9393
// property access (assign)
94-
_super.x = f;
94+
_super.prototype.x = f;
9595
// element access (assign)
96-
_super["x"] = f;
96+
_super.prototype["x"] = f;
9797
// destructuring assign with property access
9898
(_a = { f: f }, super.x = _a.f, _a);
9999
// destructuring assign with element access

0 commit comments

Comments
 (0)