Skip to content

Commit 5f22f66

Browse files
joeywattsJoey Watts
authored andcommitted
Add private named instance field transformation (bloomberg#31)
Implements private instance fields on top of the class properties refactor. Signed-off-by: Joseph Watts <[email protected]> Signed-off-by: Max Heiber <[email protected]>
1 parent bdef38a commit 5f22f66

File tree

57 files changed

+1739
-112
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+1739
-112
lines changed

src/compiler/binder.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3878,6 +3878,10 @@ namespace ts {
38783878
case SyntaxKind.BreakStatement:
38793879
transformFlags |= TransformFlags.ContainsHoistedDeclarationOrCompletion;
38803880
break;
3881+
3882+
case SyntaxKind.PrivateName:
3883+
transformFlags |= TransformFlags.ContainsClassFields;
3884+
break;
38813885
}
38823886

38833887
node.transformFlags = transformFlags | TransformFlags.HasComputedFlags;

src/compiler/transformers/classFields.ts

Lines changed: 431 additions & 24 deletions
Large diffs are not rendered by default.

src/compiler/transformers/generators.ts

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -667,28 +667,6 @@ namespace ts {
667667
}
668668
}
669669

670-
function isCompoundAssignment(kind: BinaryOperator): kind is CompoundAssignmentOperator {
671-
return kind >= SyntaxKind.FirstCompoundAssignment
672-
&& kind <= SyntaxKind.LastCompoundAssignment;
673-
}
674-
675-
function getOperatorForCompoundAssignment(kind: CompoundAssignmentOperator): BitwiseOperatorOrHigher {
676-
switch (kind) {
677-
case SyntaxKind.PlusEqualsToken: return SyntaxKind.PlusToken;
678-
case SyntaxKind.MinusEqualsToken: return SyntaxKind.MinusToken;
679-
case SyntaxKind.AsteriskEqualsToken: return SyntaxKind.AsteriskToken;
680-
case SyntaxKind.AsteriskAsteriskEqualsToken: return SyntaxKind.AsteriskAsteriskToken;
681-
case SyntaxKind.SlashEqualsToken: return SyntaxKind.SlashToken;
682-
case SyntaxKind.PercentEqualsToken: return SyntaxKind.PercentToken;
683-
case SyntaxKind.LessThanLessThanEqualsToken: return SyntaxKind.LessThanLessThanToken;
684-
case SyntaxKind.GreaterThanGreaterThanEqualsToken: return SyntaxKind.GreaterThanGreaterThanToken;
685-
case SyntaxKind.GreaterThanGreaterThanGreaterThanEqualsToken: return SyntaxKind.GreaterThanGreaterThanGreaterThanToken;
686-
case SyntaxKind.AmpersandEqualsToken: return SyntaxKind.AmpersandToken;
687-
case SyntaxKind.BarEqualsToken: return SyntaxKind.BarToken;
688-
case SyntaxKind.CaretEqualsToken: return SyntaxKind.CaretToken;
689-
}
690-
}
691-
692670
/**
693671
* Visits a right-associative binary expression containing `yield`.
694672
*

src/compiler/transformers/utilities.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,28 @@ namespace ts {
250250
isWellKnownSymbolSyntactically(expression);
251251
}
252252

253+
export function isCompoundAssignment(kind: BinaryOperator): kind is CompoundAssignmentOperator {
254+
return kind >= SyntaxKind.FirstCompoundAssignment
255+
&& kind <= SyntaxKind.LastCompoundAssignment;
256+
}
257+
258+
export function getOperatorForCompoundAssignment(kind: CompoundAssignmentOperator): BitwiseOperatorOrHigher {
259+
switch (kind) {
260+
case SyntaxKind.PlusEqualsToken: return SyntaxKind.PlusToken;
261+
case SyntaxKind.MinusEqualsToken: return SyntaxKind.MinusToken;
262+
case SyntaxKind.AsteriskEqualsToken: return SyntaxKind.AsteriskToken;
263+
case SyntaxKind.AsteriskAsteriskEqualsToken: return SyntaxKind.AsteriskAsteriskToken;
264+
case SyntaxKind.SlashEqualsToken: return SyntaxKind.SlashToken;
265+
case SyntaxKind.PercentEqualsToken: return SyntaxKind.PercentToken;
266+
case SyntaxKind.LessThanLessThanEqualsToken: return SyntaxKind.LessThanLessThanToken;
267+
case SyntaxKind.GreaterThanGreaterThanEqualsToken: return SyntaxKind.GreaterThanGreaterThanToken;
268+
case SyntaxKind.GreaterThanGreaterThanGreaterThanEqualsToken: return SyntaxKind.GreaterThanGreaterThanGreaterThanToken;
269+
case SyntaxKind.AmpersandEqualsToken: return SyntaxKind.AmpersandToken;
270+
case SyntaxKind.BarEqualsToken: return SyntaxKind.BarToken;
271+
case SyntaxKind.CaretEqualsToken: return SyntaxKind.CaretToken;
272+
}
273+
}
274+
253275
/**
254276
* Adds super call and preceding prologue directives into the list of statements.
255277
*

src/compiler/types.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -953,6 +953,11 @@ namespace ts {
953953
initializer?: Expression; // Optional initializer
954954
}
955955

956+
/*@internal*/
957+
export interface PrivateNamedPropertyDeclaration extends PropertyDeclaration {
958+
name: PrivateName;
959+
}
960+
956961
export interface ObjectLiteralElement extends NamedDeclaration {
957962
_objectLiteralBrand: any;
958963
name?: PropertyName;
@@ -1777,6 +1782,11 @@ namespace ts {
17771782
name: Identifier | PrivateName;
17781783
}
17791784

1785+
/*@internal*/
1786+
export interface PrivateNamedPropertyAccessExpression extends PropertyAccessExpression {
1787+
name: PrivateName;
1788+
}
1789+
17801790
export interface SuperPropertyAccessExpression extends PropertyAccessExpression {
17811791
expression: SuperExpression;
17821792
}

src/compiler/utilities.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6287,6 +6287,16 @@ namespace ts {
62876287
|| kind === SyntaxKind.ArrayBindingPattern;
62886288
}
62896289

6290+
/*@internal*/
6291+
export function isPrivateNamedPropertyDeclaration(node: Node): node is PrivateNamedPropertyDeclaration {
6292+
return isPropertyDeclaration(node) && isPrivateName(node.name);
6293+
}
6294+
6295+
/*@internal*/
6296+
export function isPrivateNamedPropertyAccessExpression(node: Node): node is PrivateNamedPropertyAccessExpression {
6297+
return isPropertyAccessExpression(node) && isPrivateName(node.name);
6298+
}
6299+
62906300
// Functions
62916301

62926302
export function isFunctionLike(node: Node): node is SignatureDeclaration {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//// [privateNameDeclaration.ts]
2+
class A {
3+
#name: string;
4+
}
5+
6+
7+
//// [privateNameDeclaration.js]
8+
var _name;
9+
var A = /** @class */ (function () {
10+
function A() {
11+
_name.set(this, void 0);
12+
}
13+
return A;
14+
}());
15+
_name = new WeakMap();
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
=== tests/cases/conformance/classes/members/privateNames/privateNameDeclaration.ts ===
2+
class A {
3+
>A : Symbol(A, Decl(privateNameDeclaration.ts, 0, 0))
4+
5+
#name: string;
6+
>#name : Symbol(A.#name, Decl(privateNameDeclaration.ts, 0, 9))
7+
}
8+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
=== tests/cases/conformance/classes/members/privateNames/privateNameDeclaration.ts ===
2+
class A {
3+
>A : A
4+
5+
#name: string;
6+
>#name : string
7+
}
8+

tests/baselines/reference/privateNameDuplicateField.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ class A {
88

99

1010
//// [privateNameDuplicateField.js]
11-
"use strict";
1211
// @target es6
12+
var _foo, _foo_1;
13+
"use strict";
1314
var A = /** @class */ (function () {
1415
function A() {
15-
this.#foo = "foo";
16-
this.#foo = "foo";
16+
_foo_1.set(this, "foo");
17+
_foo_1.set(this, "foo");
1718
}
1819
return A;
1920
}());
21+
_foo = new WeakMap(), _foo_1 = new WeakMap();

0 commit comments

Comments
 (0)