Skip to content

Wrong super keyword async scope resolution #30192

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions src/compiler/transformers/es2017.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace ts {
/**
* Keeps track of property names accessed on super (`super.x`) within async functions.
*/
let capturedSuperProperties: UnderscoreEscapedMap<true>;
let capturedSuperProperties: UnderscoreEscapedMap<true> | undefined;
/** Whether the async function contains an element access on super (`super[x]`). */
let hasSuperElementAccess: boolean;
/** A set of node IDs for generated super accessors (variable statements). */
Expand Down Expand Up @@ -86,13 +86,15 @@ namespace ts {
return visitArrowFunction(<ArrowFunction>node);

case SyntaxKind.PropertyAccessExpression:
if (capturedSuperProperties && isPropertyAccessExpression(node) && node.expression.kind === SyntaxKind.SuperKeyword) {
if (isPropertyAccessExpression(node) && node.expression.kind === SyntaxKind.SuperKeyword) {
capturedSuperProperties = capturedSuperProperties || createUnderscoreEscapedMap<true>();
capturedSuperProperties.set(node.name.escapedText, true);
}

return visitEachChild(node, visitor, context);

case SyntaxKind.ElementAccessExpression:
if (capturedSuperProperties && (<ElementAccessExpression>node).expression.kind === SyntaxKind.SuperKeyword) {
if ((<ElementAccessExpression>node).expression.kind === SyntaxKind.SuperKeyword) {
hasSuperElementAccess = true;
}
return visitEachChild(node, visitor, context);
Expand Down Expand Up @@ -418,11 +420,6 @@ namespace ts {
recordDeclarationName(parameter, enclosingFunctionParameterNames);
}

const savedCapturedSuperProperties = capturedSuperProperties;
const savedHasSuperElementAccess = hasSuperElementAccess;
capturedSuperProperties = createUnderscoreEscapedMap<true>();
hasSuperElementAccess = false;

let result: ConciseBody;
if (!isArrowFunction) {
const statements: Statement[] = [];
Expand All @@ -446,9 +443,15 @@ namespace ts {

if (emitSuperHelpers) {
enableSubstitutionForAsyncMethodsWithSuper();
const variableStatement = createSuperAccessVariableStatement(resolver, node, capturedSuperProperties);
substitutedSuperAccessors[getNodeId(variableStatement)] = true;
insertStatementsAfterStandardPrologue(statements, [variableStatement]);
// Do not create the _super variable if do not have any super call.
if (capturedSuperProperties) {
const variableStatement = createSuperAccessVariableStatement(resolver, node, capturedSuperProperties);
substitutedSuperAccessors[getNodeId(variableStatement)] = true;
insertStatementsAfterStandardPrologue(statements, [variableStatement]);

// Clean up to not affect the next method with previous method variables.
capturedSuperProperties = undefined;
}
}

const block = createBlock(statements, /*multiLine*/ true);
Expand Down Expand Up @@ -485,8 +488,6 @@ namespace ts {
}

enclosingFunctionParameterNames = savedEnclosingFunctionParameterNames;
capturedSuperProperties = savedCapturedSuperProperties;
hasSuperElementAccess = savedHasSuperElementAccess;
return result;
}

Expand Down
21 changes: 21 additions & 0 deletions tests/baselines/reference/asyncMethodWithSuperConflict_es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ class B extends A {
// destructuring assign with element access
({ f: super["x"] } = { f });
}

// inner async super call
async inner() {
(async () => {
super["x"]();
super.x();
})();
}
}


Expand Down Expand Up @@ -128,4 +136,17 @@ class B extends A {
({ f: _superIndex_1("x").value } = { f });
});
}
// inner async super call
inner() {
const _superIndex_1 = name => super[name];
const _super_1 = Object.create(null, {
x: { get: () => super.x }
});
return __awaiter(this, void 0, void 0, function* () {
(() => __awaiter(this, void 0, void 0, function* () {
_superIndex_1("x").call(this);
_super_1.x.call(this);
}))();
});
}
}
17 changes: 17 additions & 0 deletions tests/baselines/reference/asyncMethodWithSuperConflict_es6.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -120,5 +120,22 @@ class B extends A {
>"x" : Symbol(A.x, Decl(asyncMethodWithSuperConflict_es6.ts, 0, 9))
>f : Symbol(f, Decl(asyncMethodWithSuperConflict_es6.ts, 55, 30))
}

// inner async super call
async inner() {
>inner : Symbol(B.inner, Decl(asyncMethodWithSuperConflict_es6.ts, 56, 5))

(async () => {
super["x"]();
>super : Symbol(A, Decl(asyncMethodWithSuperConflict_es6.ts, 0, 0))
>"x" : Symbol(A.x, Decl(asyncMethodWithSuperConflict_es6.ts, 0, 9))

super.x();
>super.x : Symbol(A.x, Decl(asyncMethodWithSuperConflict_es6.ts, 0, 9))
>super : Symbol(A, Decl(asyncMethodWithSuperConflict_es6.ts, 0, 0))
>x : Symbol(A.x, Decl(asyncMethodWithSuperConflict_es6.ts, 0, 9))

})();
}
}

24 changes: 24 additions & 0 deletions tests/baselines/reference/asyncMethodWithSuperConflict_es6.types
Original file line number Diff line number Diff line change
Expand Up @@ -146,5 +146,29 @@ class B extends A {
>{ f } : { f: () => void; }
>f : () => void
}

// inner async super call
async inner() {
>inner : () => Promise<void>

(async () => {
>(async () => { super["x"](); super.x(); })() : Promise<void>
>(async () => { super["x"](); super.x(); }) : () => Promise<void>
>async () => { super["x"](); super.x(); } : () => Promise<void>

super["x"]();
>super["x"]() : void
>super["x"] : () => void
>super : A
>"x" : "x"

super.x();
>super.x() : void
>super.x : () => void
>super : A
>x : () => void

})();
}
}

15 changes: 15 additions & 0 deletions tests/baselines/reference/asyncMethodWithSuper_es2017.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ class B extends A {
// destructuring assign with element access
({ f: super["x"] } = { f });
}

// inner async super call
async inner() {
(async () => {
super["x"]();
super.x();
})();
}
}


Expand Down Expand Up @@ -96,4 +104,11 @@ class B extends A {
// destructuring assign with element access
({ f: super["x"] } = { f });
}
// inner async super call
async inner() {
(async () => {
super["x"]();
super.x();
})();
}
}
17 changes: 17 additions & 0 deletions tests/baselines/reference/asyncMethodWithSuper_es2017.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,22 @@ class B extends A {
>"x" : Symbol(A.x, Decl(asyncMethodWithSuper_es2017.ts, 0, 9))
>f : Symbol(f, Decl(asyncMethodWithSuper_es2017.ts, 51, 30))
}

// inner async super call
async inner() {
>inner : Symbol(B.inner, Decl(asyncMethodWithSuper_es2017.ts, 52, 5))

(async () => {
super["x"]();
>super : Symbol(A, Decl(asyncMethodWithSuper_es2017.ts, 0, 0))
>"x" : Symbol(A.x, Decl(asyncMethodWithSuper_es2017.ts, 0, 9))

super.x();
>super.x : Symbol(A.x, Decl(asyncMethodWithSuper_es2017.ts, 0, 9))
>super : Symbol(A, Decl(asyncMethodWithSuper_es2017.ts, 0, 0))
>x : Symbol(A.x, Decl(asyncMethodWithSuper_es2017.ts, 0, 9))

})();
}
}

24 changes: 24 additions & 0 deletions tests/baselines/reference/asyncMethodWithSuper_es2017.types
Original file line number Diff line number Diff line change
Expand Up @@ -130,5 +130,29 @@ class B extends A {
>{ f } : { f: () => void; }
>f : () => void
}

// inner async super call
async inner() {
>inner : () => Promise<void>

(async () => {
>(async () => { super["x"](); super.x(); })() : Promise<void>
>(async () => { super["x"](); super.x(); }) : () => Promise<void>
>async () => { super["x"](); super.x(); } : () => Promise<void>

super["x"]();
>super["x"]() : void
>super["x"] : () => void
>super : A
>"x" : "x"

super.x();
>super.x() : void
>super.x : () => void
>super : A
>x : () => void

})();
}
}

24 changes: 24 additions & 0 deletions tests/baselines/reference/asyncMethodWithSuper_es5.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ class B extends A {
// destructuring assign with element access
({ f: super["x"] } = { f });
}

// inner async super call
async inner() {
(async () => {
super["x"]();
super.x();
})();
}
}


Expand Down Expand Up @@ -111,5 +119,21 @@ var B = /** @class */ (function (_super) {
});
});
};
// inner async super call
B.prototype.inner = function () {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
(function () { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
_super.prototype["x"].call(this);
_super.prototype.x.call(this);
return [2 /*return*/];
});
}); })();
return [2 /*return*/];
});
});
};
return B;
}(A));
17 changes: 17 additions & 0 deletions tests/baselines/reference/asyncMethodWithSuper_es5.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,22 @@ class B extends A {
>"x" : Symbol(A.x, Decl(asyncMethodWithSuper_es5.ts, 0, 9))
>f : Symbol(f, Decl(asyncMethodWithSuper_es5.ts, 51, 30))
}

// inner async super call
async inner() {
>inner : Symbol(B.inner, Decl(asyncMethodWithSuper_es5.ts, 52, 5))

(async () => {
super["x"]();
>super : Symbol(A, Decl(asyncMethodWithSuper_es5.ts, 0, 0))
>"x" : Symbol(A.x, Decl(asyncMethodWithSuper_es5.ts, 0, 9))

super.x();
>super.x : Symbol(A.x, Decl(asyncMethodWithSuper_es5.ts, 0, 9))
>super : Symbol(A, Decl(asyncMethodWithSuper_es5.ts, 0, 0))
>x : Symbol(A.x, Decl(asyncMethodWithSuper_es5.ts, 0, 9))

})();
}
}

24 changes: 24 additions & 0 deletions tests/baselines/reference/asyncMethodWithSuper_es5.types
Original file line number Diff line number Diff line change
Expand Up @@ -130,5 +130,29 @@ class B extends A {
>{ f } : { f: () => void; }
>f : () => void
}

// inner async super call
async inner() {
>inner : () => Promise<void>

(async () => {
>(async () => { super["x"](); super.x(); })() : Promise<void>
>(async () => { super["x"](); super.x(); }) : () => Promise<void>
>async () => { super["x"](); super.x(); } : () => Promise<void>

super["x"]();
>super["x"]() : void
>super["x"] : () => void
>super : A
>"x" : "x"

super.x();
>super.x() : void
>super.x : () => void
>super : A
>x : () => void

})();
}
}

21 changes: 21 additions & 0 deletions tests/baselines/reference/asyncMethodWithSuper_es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ class B extends A {
// destructuring assign with element access
({ f: super["x"] } = { f });
}

// inner async super call
async inner() {
(async () => {
super["x"]();
super.x();
})();
}
}


Expand Down Expand Up @@ -112,4 +120,17 @@ class B extends A {
({ f: _superIndex("x").value } = { f });
});
}
// inner async super call
inner() {
const _superIndex = name => super[name];
const _super = Object.create(null, {
x: { get: () => super.x }
});
return __awaiter(this, void 0, void 0, function* () {
(() => __awaiter(this, void 0, void 0, function* () {
_superIndex("x").call(this);
_super.x.call(this);
}))();
});
}
}
17 changes: 17 additions & 0 deletions tests/baselines/reference/asyncMethodWithSuper_es6.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,22 @@ class B extends A {
>"x" : Symbol(A.x, Decl(asyncMethodWithSuper_es6.ts, 0, 9))
>f : Symbol(f, Decl(asyncMethodWithSuper_es6.ts, 51, 30))
}

// inner async super call
async inner() {
>inner : Symbol(B.inner, Decl(asyncMethodWithSuper_es6.ts, 52, 5))

(async () => {
super["x"]();
>super : Symbol(A, Decl(asyncMethodWithSuper_es6.ts, 0, 0))
>"x" : Symbol(A.x, Decl(asyncMethodWithSuper_es6.ts, 0, 9))

super.x();
>super.x : Symbol(A.x, Decl(asyncMethodWithSuper_es6.ts, 0, 9))
>super : Symbol(A, Decl(asyncMethodWithSuper_es6.ts, 0, 0))
>x : Symbol(A.x, Decl(asyncMethodWithSuper_es6.ts, 0, 9))

})();
}
}

Loading