Skip to content
Open
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
5 changes: 3 additions & 2 deletions tsc/internal/transformers/estransforms/async.go
Original file line number Diff line number Diff line change
Expand Up @@ -897,11 +897,12 @@ func (tx *asyncTransformer) transformAsyncFunctionBodyWorker(body *ast.Node) *as
// This avoids relying on parent pointers (IsAssignmentTarget) which may not be set
// on synthesized AST nodes from prior transforms.
func assignmentTargetContainsSuperProperty(node *ast.Node) bool {
// An assignment target may be wrapped in parentheses, `!`, `as`, `satisfies` or `<T>`;
// the type eraser has already turned the assertions into partially emitted expressions.
node = ast.SkipOuterExpressions(node, ast.OEKAll)
switch node.Kind {
case ast.KindPropertyAccessExpression, ast.KindElementAccessExpression:
return node.Expression().Kind == ast.KindSuperKeyword
case ast.KindParenthesizedExpression:
return assignmentTargetContainsSuperProperty(node.AsParenthesizedExpression().Expression)
case ast.KindArrayLiteralExpression:
return slices.ContainsFunc(node.AsArrayLiteralExpression().Elements.Nodes, assignmentTargetContainsSuperProperty)
case ast.KindObjectLiteralExpression:
Expand Down
63 changes: 48 additions & 15 deletions tsc/internal/transformers/estransforms/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,16 @@ func (s *superAccessState) visitSuperAccessNode(node *ast.Node) *ast.Node {
switch node.Kind {
case ast.KindCallExpression:
call := node.AsCallExpression()
if ast.IsSuperProperty(call.Expression) {
if ast.IsSuperProperty(ast.SkipOuterExpressions(call.Expression, ast.OEKAll)) {
return s.substituteCallExpressionWithSuperAccess(call, s.superAccessVisitor)
}
return s.superAccessVisitor.VisitEachChild(node)
case ast.KindTaggedTemplateExpression:
tagged := node.AsTaggedTemplateExpression()
if ast.IsSuperProperty(ast.SkipOuterExpressions(tagged.Tag, ast.OEKAll)) {
return s.substituteTaggedTemplateWithSuperAccess(tagged, s.superAccessVisitor)
}
return s.superAccessVisitor.VisitEachChild(node)
case ast.KindPropertyAccessExpression:
if node.Expression().Kind == ast.KindSuperKeyword {
// super.x → _super.x
Expand Down Expand Up @@ -112,28 +118,31 @@ func (s *superAccessState) substituteSuperAccessesInBody(body *ast.Node) *ast.No
return s.superAccessVisitor.VisitNode(body)
}

// substituteCallExpressionWithSuperAccess handles super.x(args) and super[x](args).
func (s *superAccessState) substituteCallExpressionWithSuperAccess(call *ast.CallExpression, visitor *ast.NodeVisitor) *ast.Node {
expression := call.Expression
// createSuperCallTarget replaces the super property in `expression`, which may be wrapped in
// parentheses or type assertions, with its _super/_superIndex reference and keeps the wrappers.
func (s *superAccessState) createSuperCallTarget(expression *ast.Expression) *ast.Expression {
superProperty := ast.SkipOuterExpressions(expression, ast.OEKAll)
var target *ast.Node

if ast.IsPropertyAccessExpression(expression) {
// super.x(args) → _super.x.call(this, args)
if ast.IsPropertyAccessExpression(superProperty) {
target = s.factory.NewPropertyAccessExpression(
s.superBinding, nil,
expression.AsPropertyAccessExpression().Name(), ast.NodeFlagsNone,
superProperty.AsPropertyAccessExpression().Name(), ast.NodeFlagsNone,
)
} else if ast.IsElementAccessExpression(expression) {
// super[x](args) → _superIndex(x).call(this, args) or _superIndex(x).value.call(this, args)
} else {
target = s.createSuperElementAccessInAsyncMethod(
expression.AsElementAccessExpression().ArgumentExpression,
superProperty.AsElementAccessExpression().ArgumentExpression,
)
} else {
return visitor.VisitEachChild(call.AsNode())
}
return s.factory.RestoreOuterExpressions(expression, target, ast.OEKAll)
}

// substituteCallExpressionWithSuperAccess handles super.x(args) and super[x](args), including
// a callee wrapped in parentheses or type assertions such as (super.x as any)(args).
func (s *superAccessState) substituteCallExpressionWithSuperAccess(call *ast.CallExpression, visitor *ast.NodeVisitor) *ast.Node {
// super.x(args) → _super.x.call(this, args)
// super[x](args) → _superIndex(x).call(this, args) or _superIndex(x).value.call(this, args)
callTarget := s.factory.NewPropertyAccessExpression(
target, nil,
s.createSuperCallTarget(call.Expression), nil,
s.factory.NewIdentifier("call"), ast.NodeFlagsNone,
)

Expand All @@ -154,11 +163,35 @@ func (s *superAccessState) substituteCallExpressionWithSuperAccess(call *ast.Cal
return result
}

// substituteTaggedTemplateWithSuperAccess handles super.x`...` and super[x]`...`, binding the
// tag to `this` the way a call through `.call(this, ...)` would.
func (s *superAccessState) substituteTaggedTemplateWithSuperAccess(node *ast.TaggedTemplateExpression, visitor *ast.NodeVisitor) *ast.Node {
// super.x`t` → _super.x.bind(this)`t`
bindTarget := s.factory.NewCallExpression(
s.factory.NewPropertyAccessExpression(
s.createSuperCallTarget(node.Tag), nil,
s.factory.NewIdentifier("bind"), ast.NodeFlagsNone,
),
nil, nil,
s.factory.NewNodeList([]*ast.Node{s.factory.NewThisExpression()}),
ast.NodeFlagsNone,
)
return s.factory.UpdateTaggedTemplateExpression(
node,
bindTarget,
node.QuestionDotToken,
nil, /*typeArguments*/
visitor.VisitNode(node.Template),
node.Flags,
)
}

// createSuperElementAccessInAsyncMethod creates _superIndex(x) or _superIndex(x).value.
// The argument is visited too, since it may itself contain super accesses (super[super.k]).
func (s *superAccessState) createSuperElementAccessInAsyncMethod(argumentExpression *ast.Node) *ast.Node {
superIndexCall := s.factory.NewCallExpression(
s.superIndexBinding, nil, nil,
s.factory.NewNodeList([]*ast.Node{argumentExpression}),
s.factory.NewNodeList([]*ast.Node{s.superAccessVisitor.VisitNode(argumentExpression)}),
ast.NodeFlagsNone,
)
if s.hasSuperPropertyAssignment {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,250 @@
//// [tests/cases/compiler/asyncMethodSuperWrappedCallsAndTags.ts] ////

//// [asyncMethodSuperWrappedCallsAndTags.ts]
// https://github.com/microsoft/TypeScript/issues/65641
// Inside a lowered async body, a `super` member call or tag keeps `this` as its receiver,
// and a `super` member written through any outer expression gets a setter, whatever
// parentheses, type assertions or non-null assertions wrap the reference. A super access
// inside the argument of `super[...]` is replaced too.

class B {
m(..._: unknown[]): any { }
get x(): any { return 0; }
set x(v: any) { }
get k(): "m" { return "m"; }
static sm(..._: unknown[]): any { }
}

class C extends B {
async calls() {
super.m();
(super.m)();
((super.m))();
(super.m as any)();
(super.m satisfies unknown as any)();
super.m!();
(<any>super.m)();
(super.m as any)(await 0);
super["m"]();
(super["m"])();
(super["m"] as any)();
super["m"]!();
}

async tags() {
super.m`t`;
(super.m)`t`;
(super.m as any)`t${await 0}`;
super.m!`t`;
super["m"]`t`;
(super["m"] as any)`t`;
}

async assignAs() {
(super.x as any) = 1;
}

async assignSatisfies() {
(super.x satisfies any) = 1;
}

async assignNonNull() {
super.x! = 1;
}

async assignTypeAssertion() {
(<any>super.x) = 1;
}

async compoundAs() {
(super.x as any) += 1;
}

async incrementNonNull() {
super.x!++;
}

async destructuringWrapped() {
[(super.x as any)] = [1];
({ a: super.x! } = { a: 1 });
}

async elementAssignAs() {
(super["x"] as any) = 1;
}

async elementArgumentWithSuper() {
super[super.k]();
super[super.k]`t`;
return super[(super.k as "m")];
}

async *asyncGenerator() {
(super.m as any)();
super.m`t`;
}

async *assignAsyncGenerator() {
(super.x as any) = 1;
}

static async staticCalls() {
(super.sm as any)();
super.sm`t`;
}
}


//// [asyncMethodSuperWrappedCallsAndTags.js]
"use strict";
// https://github.com/microsoft/TypeScript/issues/65641
// Inside a lowered async body, a `super` member call or tag keeps `this` as its receiver,
// and a `super` member written through any outer expression gets a setter, whatever
// parentheses, type assertions or non-null assertions wrap the reference. A super access
// inside the argument of `super[...]` is replaced too.
class B {
m(..._) { }
get x() { return 0; }
set x(v) { }
get k() { return "m"; }
static sm(..._) { }
}
class C extends B {
calls() {
const _superIndex = name => super[name];
const _super = Object.create(null, {
m: { get: () => super.m }
});
return __awaiter(this, void 0, void 0, function* () {
_super.m.call(this);
(_super.m).call(this);
((_super.m)).call(this);
_super.m.call(this);
_super.m.call(this);
_super.m.call(this);
_super.m.call(this);
_super.m.call(this, yield 0);
_superIndex("m").call(this);
(_superIndex("m")).call(this);
_superIndex("m").call(this);
_superIndex("m").call(this);
});
}
tags() {
const _superIndex = name => super[name];
const _super = Object.create(null, {
m: { get: () => super.m }
});
return __awaiter(this, void 0, void 0, function* () {
_super.m.bind(this) `t`;
(_super.m).bind(this) `t`;
_super.m.bind(this) `t${yield 0}`;
_super.m.bind(this) `t`;
_superIndex("m").bind(this) `t`;
_superIndex("m").bind(this) `t`;
});
}
assignAs() {
const _super = Object.create(null, {
x: { get: () => super.x, set: v => super.x = v }
});
return __awaiter(this, void 0, void 0, function* () {
_super.x = 1;
});
}
assignSatisfies() {
const _super = Object.create(null, {
x: { get: () => super.x, set: v => super.x = v }
});
return __awaiter(this, void 0, void 0, function* () {
_super.x = 1;
});
}
assignNonNull() {
const _super = Object.create(null, {
x: { get: () => super.x, set: v => super.x = v }
});
return __awaiter(this, void 0, void 0, function* () {
_super.x = 1;
});
}
assignTypeAssertion() {
const _super = Object.create(null, {
x: { get: () => super.x, set: v => super.x = v }
});
return __awaiter(this, void 0, void 0, function* () {
_super.x = 1;
});
}
compoundAs() {
const _super = Object.create(null, {
x: { get: () => super.x, set: v => super.x = v }
});
return __awaiter(this, void 0, void 0, function* () {
_super.x += 1;
});
}
incrementNonNull() {
const _super = Object.create(null, {
x: { get: () => super.x, set: v => super.x = v }
});
return __awaiter(this, void 0, void 0, function* () {
_super.x++;
});
}
destructuringWrapped() {
const _super = Object.create(null, {
x: { get: () => super.x, set: v => super.x = v }
});
return __awaiter(this, void 0, void 0, function* () {
[_super.x] = [1];
({ a: _super.x } = { a: 1 });
});
}
elementAssignAs() {
const _superIndex = (function (geti, seti) {
const cache = Object.create(null);
return name => cache[name] || (cache[name] = { get value() { return geti(name); }, set value(v) { seti(name, v); } });
})(name => super[name], (name, value) => super[name] = value);
return __awaiter(this, void 0, void 0, function* () {
_superIndex("x").value = 1;
});
}
elementArgumentWithSuper() {
const _superIndex = name => super[name];
const _super = Object.create(null, {
k: { get: () => super.k }
});
return __awaiter(this, void 0, void 0, function* () {
_superIndex(_super.k).call(this);
_superIndex(_super.k).bind(this) `t`;
return _superIndex(_super.k);
});
}
asyncGenerator() {
const _super = Object.create(null, {
m: { get: () => super.m }
});
return __asyncGenerator(this, arguments, function* asyncGenerator_1() {
_super.m.call(this);
_super.m.bind(this) `t`;
});
}
assignAsyncGenerator() {
const _super = Object.create(null, {
x: { get: () => super.x, set: v => super.x = v }
});
return __asyncGenerator(this, arguments, function* assignAsyncGenerator_1() {
_super.x = 1;
});
}
static staticCalls() {
const _super = Object.create(null, {
sm: { get: () => super.sm }
});
return __awaiter(this, void 0, void 0, function* () {
_super.sm.call(this);
_super.sm.bind(this) `t`;
});
}
}
Loading