diff --git a/tsc/internal/transformers/estransforms/async.go b/tsc/internal/transformers/estransforms/async.go index 74a6aa18b3ef3..e06ad276743a3 100644 --- a/tsc/internal/transformers/estransforms/async.go +++ b/tsc/internal/transformers/estransforms/async.go @@ -164,6 +164,9 @@ func (tx *asyncTransformer) visit(node *ast.Node) *ast.Node { func (tx *asyncTransformer) visitAsyncBodyNode(node *ast.Node) *ast.Node { if isNodeWithPossibleHoistedDeclaration(node) { + // These statements bypass `visit`, so their own super accesses are recorded here: + // the initializer of a `for-in` or `for-of` statement is an assignment target. + tx.trackSuperAccess(node) switch node.Kind { case ast.KindVariableStatement: return tx.visitVariableStatementInAsyncBody(node) diff --git a/tsc/internal/transformers/estransforms/utilities.go b/tsc/internal/transformers/estransforms/utilities.go index f30003ba09cff..5b46906c2ad86 100644 --- a/tsc/internal/transformers/estransforms/utilities.go +++ b/tsc/internal/transformers/estransforms/utilities.go @@ -273,6 +273,11 @@ func (s *superAccessState) trackSuperAccess(node *ast.Node) { if isUpdateExpression(node) && assignmentTargetContainsSuperProperty(node.AsPostfixUnaryExpression().Operand) { s.hasSuperPropertyAssignment = true } + case ast.KindForInStatement, ast.KindForOfStatement: + // `for (super.x in ...)` and `for (super.x of ...)` assign to the initializer on each iteration. + if assignmentTargetContainsSuperProperty(node.Initializer()) { + s.hasSuperPropertyAssignment = true + } } } diff --git a/tsc/testdata/baselines/reference/compiler/asyncMethodSuperPropertyForInOfTarget(target=es2015).js b/tsc/testdata/baselines/reference/compiler/asyncMethodSuperPropertyForInOfTarget(target=es2015).js new file mode 100644 index 0000000000000..68be14b7f4f90 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/asyncMethodSuperPropertyForInOfTarget(target=es2015).js @@ -0,0 +1,176 @@ +//// [tests/cases/compiler/asyncMethodSuperPropertyForInOfTarget.ts] //// + +//// [asyncMethodSuperPropertyForInOfTarget.ts] +// https://github.com/microsoft/TypeScript/issues/65655 +// A `super` property in the head of a `for-of` or `for-in` statement is an assignment +// target, so `_super` needs a setter (and `_superIndex` a `value` accessor). + +class B { + get x(): any { return 0; } + set x(v: any) { } + static get x(): any { return 0; } + static set x(v: any) { } +} + +class C extends B { + async forOfProperty() { + for (super.x of [1]); + } + + async forInProperty() { + for (super.x in { k: 0 }); + } + + async forOfElement() { + for (super["x"] of [1]); + } + + async forInElement() { + for (super["x"] in { k: 0 }); + } + + async forOfParenthesized() { + for ((super.x) of [1]); + } + + async forOfArrayPattern() { + for ([super.x] of [[1]]); + } + + async forOfObjectPattern() { + for ({ a: super.x } of [{ a: 1 }]); + } + + async forInAsyncArrow() { + return async () => { for (super.x in { k: 0 }); }; + } + + async *forOfAsyncGenerator() { + for (super.x of [1]); + } + + async *forInElementAsyncGenerator() { + for (super["x"] in { k: 0 }); + } + + static async forOfStatic() { + for (super.x of [1]); + } +} + + +//// [asyncMethodSuperPropertyForInOfTarget.js] +"use strict"; +// https://github.com/microsoft/TypeScript/issues/65655 +// A `super` property in the head of a `for-of` or `for-in` statement is an assignment +// target, so `_super` needs a setter (and `_superIndex` a `value` accessor). +class B { + get x() { return 0; } + set x(v) { } + static get x() { return 0; } + static set x(v) { } +} +class C extends B { + forOfProperty() { + const _super = Object.create(null, { + x: { get: () => super.x, set: v => super.x = v } + }); + return __awaiter(this, void 0, void 0, function* () { + for (_super.x of [1]) + ; + }); + } + forInProperty() { + const _super = Object.create(null, { + x: { get: () => super.x, set: v => super.x = v } + }); + return __awaiter(this, void 0, void 0, function* () { + for (_super.x in { k: 0 }) + ; + }); + } + forOfElement() { + 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* () { + for (_superIndex("x").value of [1]) + ; + }); + } + forInElement() { + 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* () { + for (_superIndex("x").value in { k: 0 }) + ; + }); + } + forOfParenthesized() { + const _super = Object.create(null, { + x: { get: () => super.x, set: v => super.x = v } + }); + return __awaiter(this, void 0, void 0, function* () { + for ((_super.x) of [1]) + ; + }); + } + forOfArrayPattern() { + const _super = Object.create(null, { + x: { get: () => super.x, set: v => super.x = v } + }); + return __awaiter(this, void 0, void 0, function* () { + for ([_super.x] of [[1]]) + ; + }); + } + forOfObjectPattern() { + const _super = Object.create(null, { + x: { get: () => super.x, set: v => super.x = v } + }); + return __awaiter(this, void 0, void 0, function* () { + for ({ a: _super.x } of [{ a: 1 }]) + ; + }); + } + forInAsyncArrow() { + const _super = Object.create(null, { + x: { get: () => super.x, set: v => super.x = v } + }); + return __awaiter(this, void 0, void 0, function* () { + return () => __awaiter(this, void 0, void 0, function* () { for (_super.x in { k: 0 }) + ; }); + }); + } + forOfAsyncGenerator() { + const _super = Object.create(null, { + x: { get: () => super.x, set: v => super.x = v } + }); + return __asyncGenerator(this, arguments, function* forOfAsyncGenerator_1() { + for (_super.x of [1]) + ; + }); + } + forInElementAsyncGenerator() { + 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 __asyncGenerator(this, arguments, function* forInElementAsyncGenerator_1() { + for (_superIndex("x").value in { k: 0 }) + ; + }); + } + static forOfStatic() { + const _super = Object.create(null, { + x: { get: () => super.x, set: v => super.x = v } + }); + return __awaiter(this, void 0, void 0, function* () { + for (_super.x of [1]) + ; + }); + } +} diff --git a/tsc/testdata/baselines/reference/compiler/asyncMethodSuperPropertyForInOfTarget(target=es2017).js b/tsc/testdata/baselines/reference/compiler/asyncMethodSuperPropertyForInOfTarget(target=es2017).js new file mode 100644 index 0000000000000..5ae505a980099 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/asyncMethodSuperPropertyForInOfTarget(target=es2017).js @@ -0,0 +1,129 @@ +//// [tests/cases/compiler/asyncMethodSuperPropertyForInOfTarget.ts] //// + +//// [asyncMethodSuperPropertyForInOfTarget.ts] +// https://github.com/microsoft/TypeScript/issues/65655 +// A `super` property in the head of a `for-of` or `for-in` statement is an assignment +// target, so `_super` needs a setter (and `_superIndex` a `value` accessor). + +class B { + get x(): any { return 0; } + set x(v: any) { } + static get x(): any { return 0; } + static set x(v: any) { } +} + +class C extends B { + async forOfProperty() { + for (super.x of [1]); + } + + async forInProperty() { + for (super.x in { k: 0 }); + } + + async forOfElement() { + for (super["x"] of [1]); + } + + async forInElement() { + for (super["x"] in { k: 0 }); + } + + async forOfParenthesized() { + for ((super.x) of [1]); + } + + async forOfArrayPattern() { + for ([super.x] of [[1]]); + } + + async forOfObjectPattern() { + for ({ a: super.x } of [{ a: 1 }]); + } + + async forInAsyncArrow() { + return async () => { for (super.x in { k: 0 }); }; + } + + async *forOfAsyncGenerator() { + for (super.x of [1]); + } + + async *forInElementAsyncGenerator() { + for (super["x"] in { k: 0 }); + } + + static async forOfStatic() { + for (super.x of [1]); + } +} + + +//// [asyncMethodSuperPropertyForInOfTarget.js] +"use strict"; +// https://github.com/microsoft/TypeScript/issues/65655 +// A `super` property in the head of a `for-of` or `for-in` statement is an assignment +// target, so `_super` needs a setter (and `_superIndex` a `value` accessor). +class B { + get x() { return 0; } + set x(v) { } + static get x() { return 0; } + static set x(v) { } +} +class C extends B { + async forOfProperty() { + for (super.x of [1]) + ; + } + async forInProperty() { + for (super.x in { k: 0 }) + ; + } + async forOfElement() { + for (super["x"] of [1]) + ; + } + async forInElement() { + for (super["x"] in { k: 0 }) + ; + } + async forOfParenthesized() { + for ((super.x) of [1]) + ; + } + async forOfArrayPattern() { + for ([super.x] of [[1]]) + ; + } + async forOfObjectPattern() { + for ({ a: super.x } of [{ a: 1 }]) + ; + } + async forInAsyncArrow() { + return async () => { for (super.x in { k: 0 }) + ; }; + } + forOfAsyncGenerator() { + const _super = Object.create(null, { + x: { get: () => super.x, set: v => super.x = v } + }); + return __asyncGenerator(this, arguments, function* forOfAsyncGenerator_1() { + for (_super.x of [1]) + ; + }); + } + forInElementAsyncGenerator() { + 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 __asyncGenerator(this, arguments, function* forInElementAsyncGenerator_1() { + for (_superIndex("x").value in { k: 0 }) + ; + }); + } + static async forOfStatic() { + for (super.x of [1]) + ; + } +} diff --git a/tsc/testdata/tests/cases/compiler/asyncMethodSuperPropertyForInOfTarget.ts b/tsc/testdata/tests/cases/compiler/asyncMethodSuperPropertyForInOfTarget.ts new file mode 100644 index 0000000000000..7bd4388489db1 --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/asyncMethodSuperPropertyForInOfTarget.ts @@ -0,0 +1,61 @@ +// @target: es2015,es2017 +// @lib: esnext +// @noEmitHelpers: true +// @noTypesAndSymbols: true + +// https://github.com/microsoft/TypeScript/issues/65655 +// A `super` property in the head of a `for-of` or `for-in` statement is an assignment +// target, so `_super` needs a setter (and `_superIndex` a `value` accessor). + +class B { + get x(): any { return 0; } + set x(v: any) { } + static get x(): any { return 0; } + static set x(v: any) { } +} + +class C extends B { + async forOfProperty() { + for (super.x of [1]); + } + + async forInProperty() { + for (super.x in { k: 0 }); + } + + async forOfElement() { + for (super["x"] of [1]); + } + + async forInElement() { + for (super["x"] in { k: 0 }); + } + + async forOfParenthesized() { + for ((super.x) of [1]); + } + + async forOfArrayPattern() { + for ([super.x] of [[1]]); + } + + async forOfObjectPattern() { + for ({ a: super.x } of [{ a: 1 }]); + } + + async forInAsyncArrow() { + return async () => { for (super.x in { k: 0 }); }; + } + + async *forOfAsyncGenerator() { + for (super.x of [1]); + } + + async *forInElementAsyncGenerator() { + for (super["x"] in { k: 0 }); + } + + static async forOfStatic() { + for (super.x of [1]); + } +}