diff --git a/tsc/internal/transformers/estransforms/async.go b/tsc/internal/transformers/estransforms/async.go index 74a6aa18b3ef3..565a155c669ca 100644 --- a/tsc/internal/transformers/estransforms/async.go +++ b/tsc/internal/transformers/estransforms/async.go @@ -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 ``; + // 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: diff --git a/tsc/internal/transformers/estransforms/utilities.go b/tsc/internal/transformers/estransforms/utilities.go index f30003ba09cff..e7f6178382154 100644 --- a/tsc/internal/transformers/estransforms/utilities.go +++ b/tsc/internal/transformers/estransforms/utilities.go @@ -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 @@ -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, ) @@ -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 { diff --git a/tsc/testdata/baselines/reference/compiler/asyncMethodSuperWrappedCallsAndTags(target=es2015).js b/tsc/testdata/baselines/reference/compiler/asyncMethodSuperWrappedCallsAndTags(target=es2015).js new file mode 100644 index 0000000000000..181173111f557 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/asyncMethodSuperWrappedCallsAndTags(target=es2015).js @@ -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!(); + (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() { + (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`; + }); + } +} diff --git a/tsc/testdata/baselines/reference/compiler/asyncMethodSuperWrappedCallsAndTags(target=es2017).js b/tsc/testdata/baselines/reference/compiler/asyncMethodSuperWrappedCallsAndTags(target=es2017).js new file mode 100644 index 0000000000000..829e016f3273a --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/asyncMethodSuperWrappedCallsAndTags(target=es2017).js @@ -0,0 +1,186 @@ +//// [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!(); + (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() { + (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 { + async calls() { + super.m(); + (super.m)(); + ((super.m))(); + super.m(); + super.m(); + super.m(); + super.m(); + super.m(await 0); + super["m"](); + (super["m"])(); + super["m"](); + super["m"](); + } + async tags() { + super.m `t`; + (super.m) `t`; + super.m `t${await 0}`; + super.m `t`; + super["m"] `t`; + super["m"] `t`; + } + async assignAs() { + super.x = 1; + } + async assignSatisfies() { + super.x = 1; + } + async assignNonNull() { + super.x = 1; + } + async assignTypeAssertion() { + super.x = 1; + } + async compoundAs() { + super.x += 1; + } + async incrementNonNull() { + super.x++; + } + async destructuringWrapped() { + [super.x] = [1]; + ({ a: super.x } = { a: 1 }); + } + async elementAssignAs() { + super["x"] = 1; + } + async elementArgumentWithSuper() { + super[super.k](); + super[super.k] `t`; + return super[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 async staticCalls() { + super.sm(); + super.sm `t`; + } +} diff --git a/tsc/testdata/tests/cases/compiler/asyncMethodSuperWrappedCallsAndTags.ts b/tsc/testdata/tests/cases/compiler/asyncMethodSuperWrappedCallsAndTags.ts new file mode 100644 index 0000000000000..2032df436f05c --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/asyncMethodSuperWrappedCallsAndTags.ts @@ -0,0 +1,97 @@ +// @target: es2015,es2017 +// @lib: esnext +// @noEmitHelpers: true +// @noTypesAndSymbols: true + +// 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!(); + (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() { + (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`; + } +}