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
93 changes: 87 additions & 6 deletions tsc/internal/transformers/estransforms/classfields.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,9 @@ func (tx *classFieldsTransformer) visit(node *ast.Node) *ast.Node {
return tx.visitTaggedTemplateExpression(node.AsTaggedTemplateExpression())
case ast.KindForStatement:
return tx.visitForStatement(node.AsForStatement())
case ast.KindForInStatement, ast.KindForOfStatement, ast.KindDoStatement, ast.KindWhileStatement:
case ast.KindForInStatement, ast.KindForOfStatement:
return tx.setInIterationStatementAnd(true, (*classFieldsTransformer).visitForInOrOfStatement, node)
case ast.KindDoStatement, ast.KindWhileStatement:
return tx.setInIterationStatementAnd(true, (*classFieldsTransformer).visitEachChildOfNode, node)
case ast.KindThisKeyword:
return tx.visitThisExpression(node)
Expand Down Expand Up @@ -1252,6 +1254,77 @@ func (tx *classFieldsTransformer) visitForStatement(node *ast.ForStatement) *ast
return tx.Factory().UpdateForStatement(node, initializer, condition, incrementor, body)
}

func (tx *classFieldsTransformer) visitForInOrOfStatement(node *ast.Node) *ast.Node {
statement := node.AsForInOrOfStatement()
if statement.Initializer != nil && !ast.IsVariableDeclarationList(statement.Initializer) &&
tx.forInOrOfTargetNeedsTransform(statement.Initializer) {
// The assignment target of a for-in/for-of statement is evaluated on each
// iteration, so a private name cannot be lowered to a helper call in the
// initializer. Instead, bind the iteration value to a temporary and perform
// the (transformed) assignment at the start of the loop body.
f := tx.Factory()
temp := f.NewTempVariable()
declarationList := f.NewVariableDeclarationList(
f.NewNodeList([]*ast.Node{f.NewVariableDeclaration(temp, nil, nil, nil)}),
ast.NodeFlagsLet,
)
declarationList.Loc = statement.Initializer.Loc

assignmentExpression := f.NewAssignmentExpression(statement.Initializer, temp)
assignmentExpression.Loc = statement.Initializer.Loc
assignment := f.NewExpressionStatement(assignmentExpression)
assignment.Loc = statement.Initializer.Loc

// Keep the original body block intact so that bindings declared in it
// cannot shadow identifiers used by the assignment target.
statements := []*ast.Node{assignment}
var bodyLocation core.TextRange
var statementsLocation core.TextRange
if statement.Statement != nil {
body := statement.Statement
if !ast.IsBlock(body) && ast.IsFunctionDeclaration(body) {
bodyStatements := f.NewNodeList([]*ast.Node{body})
bodyStatements.Loc = body.Loc
bodyBlock := f.NewBlock(bodyStatements, true)
bodyBlock.Loc = body.Loc
body = bodyBlock
}
statements = append(statements, body)
bodyLocation = statement.Statement.Loc
statementsLocation = statement.Statement.Loc
}

statementList := f.NewNodeList(statements)
statementList.Loc = statementsLocation
block := f.NewBlock(statementList, true)
block.Loc = bodyLocation

return f.UpdateForInOrOfStatement(
statement,
statement.AwaitModifier,
declarationList,
tx.Visitor().VisitNode(statement.Expression),
tx.EmitContext().VisitIterationBody(block, tx.Visitor()),
)
}
return tx.visitEachChildOfNode(node)
}

func (tx *classFieldsTransformer) forInOrOfTargetNeedsTransform(node *ast.Node) bool {
if node.SubtreeFacts()&ast.SubtreeContainsPrivateIdentifierInExpression == 0 {
return false
}
var find func(*ast.Node) bool
find = func(n *ast.Node) bool {
if ast.IsPropertyAccessExpression(n) && ast.IsPrivateIdentifier(n.AsPropertyAccessExpression().Name()) &&
tx.accessPrivateIdentifier(n.AsPropertyAccessExpression().Name()) != nil {
return true
}
return n.ForEachChild(find)
}
return find(node)
}

func (tx *classFieldsTransformer) visitExpressionStatement(node *ast.ExpressionStatement) *ast.Node {
// Preserve private identifiers that appear directly as the expression of an
// ExpressionStatement (e.g., `#;`). This is error-recovery output from the parser
Expand Down Expand Up @@ -3221,6 +3294,7 @@ func (tx *classFieldsTransformer) wrapPrivateIdentifierForDestructuringTarget(no
return tx.Visitor().VisitEachChild(node)
}
receiver := prop.Expression
var receiverAssignment *ast.Expression
// We cannot copy `this` or `super` into the function because they will be bound
// differently inside the function.
isThisOrSuperProperty := prop.Expression.Kind == ast.KindThisKeyword || prop.Expression.Kind == ast.KindSuperKeyword
Expand All @@ -3229,13 +3303,20 @@ func (tx *classFieldsTransformer) wrapPrivateIdentifierForDestructuringTarget(no
Flags: printer.GeneratedIdentifierFlagsReservedInNestedScopes,
})
tx.EmitContext().AddVariableDeclaration(receiver)
tx.pendingExpressions = append(
tx.pendingExpressions,
tx.Factory().NewAssignmentExpression(receiver, tx.Visitor().VisitNode(prop.Expression)),
)
receiverAssignment = tx.Factory().NewAssignmentExpression(receiver, tx.Visitor().VisitNode(prop.Expression))
}
assignExpr := tx.createPrivateIdentifierAssignment(info, receiver, parameter, ast.KindEqualsToken, false)
return tx.Factory().NewAssignmentTargetWrapper(parameter, assignExpr)
wrapper := tx.Factory().NewAssignmentTargetWrapper(parameter, assignExpr).AsPropertyAccessExpression()
if receiverAssignment != nil {
return tx.Factory().UpdatePropertyAccessExpression(
wrapper,
tx.Factory().NewParenthesizedExpression(tx.Factory().NewCommaExpression(receiverAssignment, wrapper.Expression)),
wrapper.QuestionDotToken,
wrapper.Name(),
wrapper.Flags,
)
}
return wrapper.AsNode()
}

func (tx *classFieldsTransformer) visitAssignmentElement(node *ast.Node) *ast.Node {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class A {
var _a;
this.exposedField = exposedField;
_A_privateField.set(this, void 0);
(_a = this, { key: ({ set value(_b) { __classPrivateFieldSet(_a, _A_privateField, _b, "f"); } }).value } = arg);
({ key: (_a = this, ({ set value(_b) { __classPrivateFieldSet(_a, _A_privateField, _b, "f"); } })).value } = arg);
}
log() {
console.log(__classPrivateFieldGet(this, _A_privateField, "f"));
Expand All @@ -65,7 +65,7 @@ class B {
var _a;
this.exposedField = exposedField;
_B_privateField.set(this, void 0);
(_a = this, { key: ({ set value(_b) { __classPrivateFieldSet(_a, _B_privateField, _b, "f"); } }).value } = arg);
({ key: (_a = this, ({ set value(_b) { __classPrivateFieldSet(_a, _B_privateField, _b, "f"); } })).value } = arg);
}
log() {
console.log(__classPrivateFieldGet(this, _B_privateField, "f"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,16 @@ _C_x = { value: void 0 };
(() => {
var _b, _c, _d, _e, _f, _g, _h, _j, _k;
// Invalid array targets must still visit private and super accesses.
_b = _a, [(void 0).x, 0, ({ set value(_l) { __classPrivateFieldSet(_b, _a, _l, "f", _C_x); } }).value] = value;
_c = _a, [(void 0).x, 1 + 2, ({ set value(_l) { __classPrivateFieldSet(_c, _a, _l, "f", _C_x); } }).value] = value;
_d = _a, [(void 0).x, (() => { }), ({ set value(_l) { __classPrivateFieldSet(_d, _a, _l, "f", _C_x); } }).value] = value;
_e = _a, [(void 0).x, value(), ({ set value(_l) { __classPrivateFieldSet(_e, _a, _l, "f", _C_x); } }).value] = value;
_f = _a, [(void 0).x, , ...({ set value(_l) { __classPrivateFieldSet(_f, _a, _l, "f", _C_x); } }).value] = value;
[(void 0).x, 0, (_b = _a, ({ set value(_l) { __classPrivateFieldSet(_b, _a, _l, "f", _C_x); } })).value] = value;
[(void 0).x, 1 + 2, (_c = _a, ({ set value(_l) { __classPrivateFieldSet(_c, _a, _l, "f", _C_x); } })).value] = value;
[(void 0).x, (() => { }), (_d = _a, ({ set value(_l) { __classPrivateFieldSet(_d, _a, _l, "f", _C_x); } })).value] = value;
[(void 0).x, value(), (_e = _a, ({ set value(_l) { __classPrivateFieldSet(_e, _a, _l, "f", _C_x); } })).value] = value;
[(void 0).x, , ...(_f = _a, ({ set value(_l) { __classPrivateFieldSet(_f, _a, _l, "f", _C_x); } })).value] = value;
// Object methods/accessors are not destructuring assignment elements.
(_g = _a, { x: (void 0).x, m() { }, private: ({ set value(_l) { __classPrivateFieldSet(_g, _a, _l, "f", _C_x); } }).value } = value);
(_h = _a, { x: (void 0).x, get g() { return 1; }, private: ({ set value(_l) { __classPrivateFieldSet(_h, _a, _l, "f", _C_x); } }).value } = value);
(_j = _a, { x: (void 0).x, set s(v) { }, private: ({ set value(_l) { __classPrivateFieldSet(_j, _a, _l, "f", _C_x); } }).value } = value);
(_k = _a, { x: [(void 0).x, 0, ({ set value(_l) { __classPrivateFieldSet(_k, _a, _l, "f", _C_x); } }).value], m() { } } = value);
({ x: (void 0).x, m() { }, private: (_g = _a, ({ set value(_l) { __classPrivateFieldSet(_g, _a, _l, "f", _C_x); } })).value } = value);
({ x: (void 0).x, get g() { return 1; }, private: (_h = _a, ({ set value(_l) { __classPrivateFieldSet(_h, _a, _l, "f", _C_x); } })).value } = value);
({ x: (void 0).x, set s(v) { }, private: (_j = _a, ({ set value(_l) { __classPrivateFieldSet(_j, _a, _l, "f", _C_x); } })).value } = value);
({ x: [(void 0).x, 0, (_k = _a, ({ set value(_l) { __classPrivateFieldSet(_k, _a, _l, "f", _C_x); } })).value], m() { } } = value);
})();
C = __decorate([
dec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Example {
}
update(source) {
var _a;
(_a = this, { value: ({ set value(_b) { tslib_1.__classPrivateFieldSet(_a, _Example_state, _b, "f"); } }).value } = source);
({ value: (_a = this, ({ set value(_b) { tslib_1.__classPrivateFieldSet(_a, _Example_state, _b, "f"); } })).value } = source);
}
}
_Example_state = new WeakMap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ class A {
_A_field.set(this, 1);
this.otherObject = new A();
let y;
(_b = this, { x: ({ set value(_m) { __classPrivateFieldSet(_b, _A_field, _m, "f"); } }).value, y } = this.testObject());
(_c = this, [({ set value(_m) { __classPrivateFieldSet(_c, _A_field, _m, "f"); } }).value, y] = this.testArray());
(_d = this, _e = this, { a: ({ set value(_m) { __classPrivateFieldSet(_d, _A_field, _m, "f"); } }).value, b: [({ set value(_m) { __classPrivateFieldSet(_e, _A_field, _m, "f"); } }).value] } = { a: 1, b: [2] });
_f = this, _g = this, [({ set value(_m) { __classPrivateFieldSet(_f, _A_field, _m, "f"); } }).value, [({ set value(_m) { __classPrivateFieldSet(_g, _A_field, _m, "f"); } }).value]] = [1, [2]];
(_h = this, _j = this, { a: ({ set value(_m) { __classPrivateFieldSet(_h, _A_field, _m, "f"); } }).value = 1, b: [({ set value(_m) { __classPrivateFieldSet(_j, _A_field, _m, "f"); } }).value = 1] } = { b: [] });
_k = this, [({ set value(_m) { __classPrivateFieldSet(_k, _A_field, _m, "f"); } }).value = 2] = [];
_l = this.otherObject, [({ set value(_m) { __classPrivateFieldSet(_l, _A_field, _m, "f"); } }).value = 2] = [];
({ x: (_b = this, ({ set value(_m) { __classPrivateFieldSet(_b, _A_field, _m, "f"); } })).value, y } = this.testObject());
([(_c = this, ({ set value(_m) { __classPrivateFieldSet(_c, _A_field, _m, "f"); } })).value, y] = this.testArray());
({ a: (_d = this, ({ set value(_m) { __classPrivateFieldSet(_d, _A_field, _m, "f"); } })).value, b: [(_e = this, ({ set value(_m) { __classPrivateFieldSet(_e, _A_field, _m, "f"); } })).value] } = { a: 1, b: [2] });
[(_f = this, ({ set value(_m) { __classPrivateFieldSet(_f, _A_field, _m, "f"); } })).value, [(_g = this, ({ set value(_m) { __classPrivateFieldSet(_g, _A_field, _m, "f"); } })).value]] = [1, [2]];
({ a: (_h = this, ({ set value(_m) { __classPrivateFieldSet(_h, _A_field, _m, "f"); } })).value = 1, b: [(_j = this, ({ set value(_m) { __classPrivateFieldSet(_j, _A_field, _m, "f"); } })).value = 1] } = { b: [] });
[(_k = this, ({ set value(_m) { __classPrivateFieldSet(_k, _A_field, _m, "f"); } })).value = 2] = [];
[(_l = this.otherObject, ({ set value(_m) { __classPrivateFieldSet(_l, _A_field, _m, "f"); } })).value = 2] = [];
}
static test(_a) {
[({ set value(_b) { __classPrivateFieldSet(_a, _A_field, _b, "f"); } }).value] = [2];
Expand Down
Loading