Skip to content

Commit 1364162

Browse files
joeywattsmheiber
authored andcommitted
Add private named instance field transformation (#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 ef5b1bd commit 1364162

File tree

68 files changed

+1805
-135
lines changed

Some content is hidden

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

68 files changed

+1805
-135
lines changed

src/compiler/binder.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3931,6 +3931,10 @@ namespace ts {
39313931
case SyntaxKind.BreakStatement:
39323932
transformFlags |= TransformFlags.ContainsHoistedDeclarationOrCompletion;
39333933
break;
3934+
3935+
case SyntaxKind.PrivateName:
3936+
transformFlags |= TransformFlags.ContainsClassFields;
3937+
break;
39343938
}
39353939

39363940
node.transformFlags = transformFlags | TransformFlags.HasComputedFlags;

src/compiler/transformers/classFields.ts

Lines changed: 431 additions & 23 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
@@ -249,6 +249,28 @@ namespace ts {
249249
isWellKnownSymbolSyntactically(expression);
250250
}
251251

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

src/compiler/types.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,11 @@ namespace ts {
957957
initializer?: Expression; // Optional initializer
958958
}
959959

960+
/*@internal*/
961+
export interface PrivateNamedPropertyDeclaration extends PropertyDeclaration {
962+
name: PrivateName;
963+
}
964+
960965
export interface ObjectLiteralElement extends NamedDeclaration {
961966
_objectLiteralBrand: any;
962967
name?: PropertyName;
@@ -1788,6 +1793,11 @@ namespace ts {
17881793
name: Identifier | PrivateName;
17891794
}
17901795

1796+
/*@internal*/
1797+
export interface PrivateNamedPropertyAccessExpression extends PropertyAccessExpression {
1798+
name: PrivateName;
1799+
}
1800+
17911801
export interface SuperPropertyAccessExpression extends PropertyAccessExpression {
17921802
expression: SuperExpression;
17931803
}

src/compiler/utilities.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6423,6 +6423,16 @@ namespace ts {
64236423
|| kind === SyntaxKind.ArrayBindingPattern;
64246424
}
64256425

6426+
/*@internal*/
6427+
export function isPrivateNamedPropertyDeclaration(node: Node): node is PrivateNamedPropertyDeclaration {
6428+
return isPropertyDeclaration(node) && isPrivateName(node.name);
6429+
}
6430+
6431+
/*@internal*/
6432+
export function isPrivateNamedPropertyAccessExpression(node: Node): node is PrivateNamedPropertyAccessExpression {
6433+
return isPropertyAccessExpression(node) && isPrivateName(node.name);
6434+
}
6435+
64266436
// Functions
64276437

64286438
export function isFunctionLike(node: Node): node is SignatureDeclaration {
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/cases/conformance/classes/members/privateNames/privateNameConstructorReserved.ts(4,5): error TS1811: '#constructor' is a reserved word.
1+
tests/cases/conformance/classes/members/privateNames/privateNameConstructorReserved.ts(4,5): error TS18012: '#constructor' is a reserved word.
22

33

44
==== tests/cases/conformance/classes/members/privateNames/privateNameConstructorReserved.ts (1 errors) ====
@@ -7,6 +7,6 @@ tests/cases/conformance/classes/members/privateNames/privateNameConstructorReser
77
class A {
88
#constructor() {} // Error: `#constructor` is a reserved word.
99
~~~~~~~~~~~~
10-
!!! error TS1811: '#constructor' is a reserved word.
10+
!!! error TS18012: '#constructor' is a reserved word.
1111
}
1212

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+

0 commit comments

Comments
 (0)