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
62 changes: 39 additions & 23 deletions tsc/internal/transformers/estransforms/classfields.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,20 +382,23 @@ func (tx *classFieldsTransformer) visitDestructuringAssignmentTarget(node *ast.N
if ast.IsPropertyAccessExpression(node) && ast.IsPrivateIdentifier(node.AsPropertyAccessExpression().Name()) {
return tx.wrapPrivateIdentifierForDestructuringTarget(node)
}
// A target wrapped in parentheses or in a type assertion, `!` or `satisfies` (all erased to partially emitted
// expressions by now) is still the same reference, so it is lowered like the bare target.
target := ast.SkipOuterExpressions(node, ast.OEKPartiallyEmittedExpressions|ast.OEKParentheses)
if tx.shouldTransformSuperInStaticInitializers && tx.currentClassElement != nil &&
ast.IsSuperProperty(node) &&
ast.IsSuperProperty(target) &&
isStaticPropertyDeclarationOrClassStaticBlock(tx.currentClassElement) &&
tx.lexicalEnvironment != nil && tx.lexicalEnvironment.data != nil {
data := tx.lexicalEnvironment.data
if data.facts&classFactsClassWasDecorated != 0 {
return tx.visitInvalidSuperProperty(node)
return tx.visitInvalidSuperProperty(target)
}
if data.classConstructor != nil && data.superClassReference != nil {
var name *ast.Expression
if ast.IsElementAccessExpression(node) {
name = tx.Visitor().VisitNode(node.AsElementAccessExpression().ArgumentExpression)
} else if ast.IsPropertyAccessExpression(node) && ast.IsIdentifier(node.AsPropertyAccessExpression().Name()) {
name = tx.Factory().NewStringLiteralFromNode(node.AsPropertyAccessExpression().Name())
if ast.IsElementAccessExpression(target) {
name = tx.Visitor().VisitNode(target.AsElementAccessExpression().ArgumentExpression)
} else if ast.IsPropertyAccessExpression(target) && ast.IsIdentifier(target.AsPropertyAccessExpression().Name()) {
name = tx.Factory().NewStringLiteralFromNode(target.AsPropertyAccessExpression().Name())
}
if name != nil {
temp := tx.Factory().NewTempVariable()
Expand Down Expand Up @@ -1149,7 +1152,7 @@ func (tx *classFieldsTransformer) visitPreOrPostfixUnaryExpression(node *ast.Nod
}

if operator == ast.KindPlusPlusToken || operator == ast.KindMinusMinusToken {
operandSkipped := ast.SkipParentheses(operand)
operandSkipped := ast.SkipOuterExpressions(operand, ast.OEKPartiallyEmittedExpressions|ast.OEKParentheses)

// Private identifier property access
if ast.IsPropertyAccessExpression(operandSkipped) && ast.IsPrivateIdentifier(operandSkipped.Name()) {
Expand Down Expand Up @@ -1315,18 +1318,30 @@ func (tx *classFieldsTransformer) visitCallExpression(node *ast.CallExpression)
}

if tx.shouldTransformSuperInStaticInitializers && tx.currentClassElement != nil &&
ast.IsSuperProperty(node.Expression) &&
ast.IsSuperProperty(expression) &&
isStaticPropertyDeclarationOrClassStaticBlock(tx.currentClassElement) &&
tx.lexicalEnvironment != nil && tx.lexicalEnvironment.data != nil &&
tx.lexicalEnvironment.data.classConstructor != nil {
// super.x()
// super[x]()

// converts `super.f(...)` into `Reflect.get(_baseTemp, "f", _classTemp).call(_classTemp, ...)`
invocation := tx.Factory().NewFunctionCallCall(
tx.Visitor().VisitNode(node.Expression),
tx.lexicalEnvironment.data.classConstructor,
tx.Visitor().VisitNodes(node.Arguments).Nodes,
// converts `super.f?.(...)` into `Reflect.get(_baseTemp, "f", _classTemp)?.call(_classTemp, ...)`
visitedTarget := tx.Factory().RestoreOuterExpressions(node.Expression, tx.Visitor().VisitNode(expression), ast.OEKAll)
visitedArgs := tx.Visitor().VisitNodes(node.Arguments)
allArgs := make([]*ast.Node, 0, 1+len(visitedArgs.Nodes))
allArgs = append(allArgs, tx.lexicalEnvironment.data.classConstructor)
allArgs = append(allArgs, visitedArgs.Nodes...)
flags := ast.NodeFlagsNone
if node.QuestionDotToken != nil {
flags = ast.NodeFlagsOptionalChain
}
invocation := tx.Factory().NewCallExpression(
tx.Factory().NewPropertyAccessExpression(visitedTarget, node.QuestionDotToken, tx.Factory().NewIdentifier("call"), flags),
nil, /*questionDotToken*/
nil, /*typeArguments*/
tx.Factory().NewNodeList(allArgs),
node.Flags,
)
tx.EmitContext().SetOriginal(invocation, node.AsNode())
invocation.Loc = node.Loc
Expand Down Expand Up @@ -1361,13 +1376,13 @@ func (tx *classFieldsTransformer) visitTaggedTemplateExpression(node *ast.Tagged
}

if tx.shouldTransformSuperInStaticInitializers && tx.currentClassElement != nil &&
ast.IsSuperProperty(node.Tag) &&
ast.IsSuperProperty(tag) &&
isStaticPropertyDeclarationOrClassStaticBlock(tx.currentClassElement) &&
tx.lexicalEnvironment != nil && tx.lexicalEnvironment.data != nil &&
tx.lexicalEnvironment.data.classConstructor != nil {
// converts `` super.f`x` `` into `` Reflect.get(_baseTemp, "f", _classTemp).bind(_classTemp)`x` ``
invocation := tx.Factory().NewFunctionBindCall(
tx.Visitor().VisitNode(node.Tag),
tx.Factory().RestoreOuterExpressions(node.Tag, tx.Visitor().VisitNode(tag), ast.OEKAll),
tx.lexicalEnvironment.data.classConstructor,
nil,
)
Expand Down Expand Up @@ -1521,7 +1536,7 @@ func (tx *classFieldsTransformer) visitBinaryExpression(node *ast.BinaryExpressi
return result
}
} else if tx.shouldTransformSuperInStaticInitializers && tx.currentClassElement != nil &&
ast.IsSuperProperty(node.Left) &&
ast.IsSuperProperty(left) &&
isStaticPropertyDeclarationOrClassStaticBlock(tx.currentClassElement) &&
tx.lexicalEnvironment != nil && tx.lexicalEnvironment.data != nil {
// super.x = ...
Expand All @@ -1533,18 +1548,18 @@ func (tx *classFieldsTransformer) visitBinaryExpression(node *ast.BinaryExpressi
return tx.Factory().UpdateBinaryExpression(
node,
nil,
tx.visitInvalidSuperProperty(node.Left),
tx.visitInvalidSuperProperty(left),
nil,
node.OperatorToken,
tx.Visitor().VisitNode(node.Right),
)
}
if data.classConstructor != nil && data.superClassReference != nil {
var setterName *ast.Expression
if ast.IsElementAccessExpression(node.Left) {
setterName = tx.Visitor().VisitNode(node.Left.AsElementAccessExpression().ArgumentExpression)
} else if ast.IsPropertyAccessExpression(node.Left) && ast.IsIdentifier(node.Left.AsPropertyAccessExpression().Name()) {
setterName = tx.Factory().NewStringLiteralFromNode(node.Left.AsPropertyAccessExpression().Name())
if ast.IsElementAccessExpression(left) {
setterName = tx.Visitor().VisitNode(left.AsElementAccessExpression().ArgumentExpression)
} else if ast.IsPropertyAccessExpression(left) && ast.IsIdentifier(left.AsPropertyAccessExpression().Name()) {
setterName = tx.Factory().NewStringLiteralFromNode(left.AsPropertyAccessExpression().Name())
}
if setterName != nil {
// converts `super.x = 1` into `(Reflect.set(_baseTemp, "x", _a = 1, _classTemp), _a)`
Expand All @@ -1565,8 +1580,8 @@ func (tx *classFieldsTransformer) visitBinaryExpression(node *ast.BinaryExpressi
getterName,
data.classConstructor,
)
tx.EmitContext().SetOriginal(superPropertyGet, node.Left)
superPropertyGet.Loc = node.Left.Loc
tx.EmitContext().SetOriginal(superPropertyGet, left)
superPropertyGet.Loc = left.Loc
expression = tx.Factory().NewBinaryExpression(
nil,
superPropertyGet,
Expand Down Expand Up @@ -3275,7 +3290,8 @@ func (tx *classFieldsTransformer) visitAssignmentRestElement(node *ast.Node) *as
}

func (tx *classFieldsTransformer) visitArrayAssignmentElement(node *ast.Node) *ast.Node {
if ast.IsArrayBindingOrAssignmentElement(node) {
// A wrapped element such as `(this.#x)` or `super.x!` is a target too.
if ast.IsArrayBindingOrAssignmentElement(node) || ast.IsOuterExpression(node, ast.OEKPartiallyEmittedExpressions|ast.OEKParentheses) {
if ast.IsSpreadElement(node) {
return tx.visitAssignmentRestElement(node)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
//// [tests/cases/compiler/superInStaticInitializerWrappedReference.ts] ////

//// [superInStaticInitializerWrappedReference.ts]
// A `super` property wrapped in parentheses, `!`, `as`, `satisfies` or a type assertion is still
// the same reference: calls must keep `this` and assignments must still go through `Reflect.set`.

class B {
static x: any = 0;
static m(): any { return this; }
static t(..._: any[]): any { return this; }
}

class D extends B {
static a = super.m();
static b = (super.m)();
static c = (super.m as any)();
static d = super.m!();
static e = (super.m satisfies any)();
static f = (<any>super.m)();
static g = (super.m)?.();
static h = super.m?.();
static i = (super["m"] as any)();
static j = (super.t)`x`;
static k = (super.t as any)`x`;
static l = ((super.x) = 1);
static {
(super.x as any) = 1;
super.x! = 2;
(super["x"] satisfies any) = 3;
(super.x as any) += 1;
(super.x as any)++;
--(super.x as any);
[(super.x)] = [1];
[super.x!] = [2];
({ a: (super.x as any) } = { a: 3 });
[(super.x) = 4] = [];
({ a: (super["x"] as any) = 5 } = {});
[, ...(super.x as any)] = [0, 1];
({ ...(super.x as any) } = {});
}
}


//// [superInStaticInitializerWrappedReference.js]
"use strict";
// A `super` property wrapped in parentheses, `!`, `as`, `satisfies` or a type assertion is still
// the same reference: calls must keep `this` and assignments must still go through `Reflect.set`.
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
var _a, _b;
var _c, _d, _e;
class B {
static m() { return this; }
static t(..._) { return this; }
}
Object.defineProperty(B, "x", {
enumerable: true,
configurable: true,
writable: true,
value: 0
});
class D extends (_d = B) {
}
_c = D;
Object.defineProperty(D, "a", {
enumerable: true,
configurable: true,
writable: true,
value: Reflect.get(_d, "m", _c).call(_c)
});
Object.defineProperty(D, "b", {
enumerable: true,
configurable: true,
writable: true,
value: (Reflect.get(_d, "m", _c)).call(_c)
});
Object.defineProperty(D, "c", {
enumerable: true,
configurable: true,
writable: true,
value: Reflect.get(_d, "m", _c).call(_c)
});
Object.defineProperty(D, "d", {
enumerable: true,
configurable: true,
writable: true,
value: Reflect.get(_d, "m", _c).call(_c)
});
Object.defineProperty(D, "e", {
enumerable: true,
configurable: true,
writable: true,
value: Reflect.get(_d, "m", _c).call(_c)
});
Object.defineProperty(D, "f", {
enumerable: true,
configurable: true,
writable: true,
value: Reflect.get(_d, "m", _c).call(_c)
});
Object.defineProperty(D, "g", {
enumerable: true,
configurable: true,
writable: true,
value: (_a = (Reflect.get(_d, "m", _c))) === null || _a === void 0 ? void 0 : _a.call(_c)
});
Object.defineProperty(D, "h", {
enumerable: true,
configurable: true,
writable: true,
value: (_b = Reflect.get(_d, "m", _c)) === null || _b === void 0 ? void 0 : _b.call(_c)
});
Object.defineProperty(D, "i", {
enumerable: true,
configurable: true,
writable: true,
value: Reflect.get(_d, "m", _c).call(_c)
});
Object.defineProperty(D, "j", {
enumerable: true,
configurable: true,
writable: true,
value: (Reflect.get(_d, "t", _c)).bind(_c) `x`
});
Object.defineProperty(D, "k", {
enumerable: true,
configurable: true,
writable: true,
value: Reflect.get(_d, "t", _c).bind(_c) `x`
});
Object.defineProperty(D, "l", {
enumerable: true,
configurable: true,
writable: true,
value: (Reflect.set(_d, "x", _e = 1, _c), _e)
});
(() => {
var _a, _b;
Reflect.set(_d, "x", 1, _c);
Reflect.set(_d, "x", 2, _c);
Reflect.set(_d, "x", 3, _c);
Reflect.set(_d, "x", Reflect.get(_d, "x", _c) + 1, _c);
Reflect.set(_d, "x", (_a = Reflect.get(_d, "x", _c), _a++, _a), _c);
Reflect.set(_d, "x", (_b = Reflect.get(_d, "x", _c), --_b), _c);
[({ set value(_a) { Reflect.set(_d, "x", _a, _c); } }).value] = [1];
[({ set value(_a) { Reflect.set(_d, "x", _a, _c); } }).value] = [2];
({ a: ({ set value(_a) { Reflect.set(_d, "x", _a, _c); } }).value } = { a: 3 });
[({ set value(_a) { Reflect.set(_d, "x", _a, _c); } }).value = 4] = [];
({ a: ({ set value(_a) { Reflect.set(_d, "x", _a, _c); } }).value = 5 } = {});
[, ...({ set value(_a) { Reflect.set(_d, "x", _a, _c); } }).value] = [0, 1];
(({ set value(_a) { Reflect.set(_d, "x", _a, _c); } }).value = __rest({}, []));
})();
Loading