Skip to content

Private named static fields #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
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
238 changes: 185 additions & 53 deletions src/compiler/transformers/classProperties.ts

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//// [classConstructorParametersCommentPlacement.ts]
// some comment
class A {
#a = "private hello";
#b = "another private name";
a = "public property";
constructor(private b = "something") { }
}


//// [classConstructorParametersCommentPlacement.js]
var _classPrivateFieldSet = function (receiver, privateMap, value) { if (!privateMap.has(receiver)) { throw new TypeError("attempted to set private field on non-instance"); } privateMap.set(receiver, value); return value; };
var _aWeakMap_1, _bWeakMap_1;
// some comment
var A = /** @class */ (function () {
function A(b) {
if (b === void 0) { b = "something"; }
// some comment
_aWeakMap_1.set(this, void 0);
// some comment
_bWeakMap_1.set(this, void 0);
this.b = b;
_classPrivateFieldSet(this, _aWeakMap_1, "private hello");
_classPrivateFieldSet(this, _bWeakMap_1, "another private name");
this.a = "public property";
}
return A;
}());
_aWeakMap_1 = new WeakMap(), _bWeakMap_1 = new WeakMap();
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
=== tests/cases/conformance/classes/constructorDeclarations/classConstructorParametersCommentPlacement.ts ===
// some comment
class A {
>A : Symbol(A, Decl(classConstructorParametersCommentPlacement.ts, 0, 0))

#a = "private hello";
>#a : Symbol(A.#a, Decl(classConstructorParametersCommentPlacement.ts, 1, 9))

#b = "another private name";
>#b : Symbol(A.#b, Decl(classConstructorParametersCommentPlacement.ts, 2, 25))

a = "public property";
>a : Symbol(A.a, Decl(classConstructorParametersCommentPlacement.ts, 3, 32))

constructor(private b = "something") { }
>b : Symbol(A.b, Decl(classConstructorParametersCommentPlacement.ts, 5, 16))
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
=== tests/cases/conformance/classes/constructorDeclarations/classConstructorParametersCommentPlacement.ts ===
// some comment
class A {
>A : A

#a = "private hello";
>#a : string
>"private hello" : "private hello"

#b = "another private name";
>#b : string
>"another private name" : "another private name"

a = "public property";
>a : string
>"public property" : "public property"

constructor(private b = "something") { }
>b : string
>"something" : "something"
}

4 changes: 3 additions & 1 deletion tests/baselines/reference/privateNameConstructorReserved.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ class A {

//// [privateNameConstructorReserved.js]
// @target es6
var _constructor_1, _constructorWeakSet_1;
var A = /** @class */ (function () {
function A() {
_constructorWeakSet_1.add(this);
}
A.prototype.#constructor = function () { }; // Error: `#constructor` is a reserved word.
return A;
}());
_constructorWeakSet_1 = new WeakSet(), _constructor_1 = function _constructor_1() { };
6 changes: 3 additions & 3 deletions tests/baselines/reference/privateNameDeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ class A {


//// [privateNameDeclaration.js]
var _name;
var _nameWeakMap_1;
var A = /** @class */ (function () {
function A() {
_name.set(this, void 0);
_nameWeakMap_1.set(this, void 0);
}
return A;
}());
_name = new WeakMap();
_nameWeakMap_1 = new WeakMap();
8 changes: 4 additions & 4 deletions tests/baselines/reference/privateNameDuplicateField.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ class A {

//// [privateNameDuplicateField.js]
// @target es6
var _foo, _foo_1;
var _fooWeakMap_1, _fooWeakMap_2;
"use strict";
var A = /** @class */ (function () {
function A() {
_foo_1.set(this, "foo");
_foo_1.set(this, "foo");
_fooWeakMap_2.set(this, "foo");
_fooWeakMap_2.set(this, "foo");
}
return A;
}());
_foo = new WeakMap(), _foo_1 = new WeakMap();
_fooWeakMap_1 = new WeakMap(), _fooWeakMap_2 = new WeakMap();
15 changes: 7 additions & 8 deletions tests/baselines/reference/privateNameField.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
//// [privateNameField.ts]
// @target es6

class A {
#name: string;
static #staticName: string;
constructor(name: string) {
this.#name = name;
A.#staticName = name;
}
}

//// [privateNameField.js]
// @target es6
var _classPrivateFieldSet = function (receiver, privateMap, value) { if (!privateMap.has(receiver)) { throw new TypeError("attempted to set private field on non-instance"); } privateMap.set(receiver, value); return value; };
var _name;
"use strict";
var _nameWeakMap_1, _staticNameWeakMap_1;
var A = /** @class */ (function () {
function A(name) {
_name.set(this, void 0);
_classPrivateFieldSet(this, _name, name);
_nameWeakMap_1.set(this, void 0);
_classPrivateFieldSet(this, _nameWeakMap_1, name);
_classPrivateFieldSet(A, _staticNameWeakMap_1, name);
}
return A;
}());
_name = new WeakMap();
_nameWeakMap_1 = new WeakMap(), _staticNameWeakMap_1 = new WeakMap();
18 changes: 12 additions & 6 deletions tests/baselines/reference/privateNameField.symbols
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
=== tests/cases/conformance/classes/members/privateNames/privateNameField.ts ===
// @target es6

class A {
>A : Symbol(A, Decl(privateNameField.ts, 0, 0))

#name: string;
>#name : Symbol(A.#name, Decl(privateNameField.ts, 2, 9))
>#name : Symbol(A.#name, Decl(privateNameField.ts, 0, 9))

static #staticName: string;
>#staticName : Symbol(A.#staticName, Decl(privateNameField.ts, 1, 18))

constructor(name: string) {
>name : Symbol(name, Decl(privateNameField.ts, 4, 16))
>name : Symbol(name, Decl(privateNameField.ts, 3, 16))

this.#name = name;
>this.#name : Symbol(A.#name, Decl(privateNameField.ts, 2, 9))
>this.#name : Symbol(A.#name, Decl(privateNameField.ts, 0, 9))
>this : Symbol(A, Decl(privateNameField.ts, 0, 0))
>name : Symbol(name, Decl(privateNameField.ts, 4, 16))
>name : Symbol(name, Decl(privateNameField.ts, 3, 16))

A.#staticName = name;
>A.#staticName : Symbol(A.#staticName, Decl(privateNameField.ts, 1, 18))
>A : Symbol(A, Decl(privateNameField.ts, 0, 0))
>name : Symbol(name, Decl(privateNameField.ts, 3, 16))
}
}
11 changes: 9 additions & 2 deletions tests/baselines/reference/privateNameField.types
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
=== tests/cases/conformance/classes/members/privateNames/privateNameField.ts ===
// @target es6

class A {
>A : A

#name: string;
>#name : string

static #staticName: string;
>#staticName : string

constructor(name: string) {
>name : string

this.#name = name;
>this.#name = name : string
>this.#name : string
>this : this
>name : string

A.#staticName = name;
>A.#staticName = name : string
>A.#staticName : string
>A : typeof A
>name : string
}
}
12 changes: 8 additions & 4 deletions tests/baselines/reference/privateNameFieldAccess.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
//// [privateNameFieldAccess.ts]
class A {
#myField = "hello world";
static #myStaticField = "hello world";
constructor() {
console.log(this.#myField);
console.log(A.#myStaticField);
}
}


//// [privateNameFieldAccess.js]
var _classPrivateFieldGet = function (receiver, privateMap) { if (!privateMap.has(receiver)) { throw new TypeError("attempted to get private field on non-instance"); } return privateMap.get(receiver); };
var _myField;
var A = /** @class */ (function () {
function A() {
_myField.set(this, "hello world");
console.log(_classPrivateFieldGet(this, _myField));
_myFieldWeakMap_1.set(this, "hello world");
console.log(_classPrivateFieldGet(this, _myFieldWeakMap_1));
console.log(_classPrivateFieldGet(A, _myStaticFieldWeakMap_1));
}
var _myFieldWeakMap_1, _myStaticFieldWeakMap_1;
_myFieldWeakMap_1 = new WeakMap(), _myStaticFieldWeakMap_1 = new WeakMap();
_myStaticFieldWeakMap_1.set(A, "hello world");
return A;
}());
_myField = new WeakMap();
10 changes: 10 additions & 0 deletions tests/baselines/reference/privateNameFieldAccess.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,23 @@ class A {
#myField = "hello world";
>#myField : Symbol(A.#myField, Decl(privateNameFieldAccess.ts, 0, 9))

static #myStaticField = "hello world";
>#myStaticField : Symbol(A.#myStaticField, Decl(privateNameFieldAccess.ts, 1, 29))

constructor() {
console.log(this.#myField);
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>this.#myField : Symbol(A.#myField, Decl(privateNameFieldAccess.ts, 0, 9))
>this : Symbol(A, Decl(privateNameFieldAccess.ts, 0, 0))

console.log(A.#myStaticField);
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>A.#myStaticField : Symbol(A.#myStaticField, Decl(privateNameFieldAccess.ts, 1, 29))
>A : Symbol(A, Decl(privateNameFieldAccess.ts, 0, 0))
}
}

12 changes: 12 additions & 0 deletions tests/baselines/reference/privateNameFieldAccess.types
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ class A {

#myField = "hello world";
>#myField : string
>"hello world" : "hello world"

static #myStaticField = "hello world";
>#myStaticField : string
>"hello world" : "hello world"

constructor() {
Expand All @@ -14,6 +18,14 @@ class A {
>log : (message?: any, ...optionalParams: any[]) => void
>this.#myField : string
>this : this

console.log(A.#myStaticField);
>console.log(A.#myStaticField) : void
>console.log : (message?: any, ...optionalParams: any[]) => void
>console : Console
>log : (message?: any, ...optionalParams: any[]) => void
>A.#myStaticField : string
>A : typeof A
}
}

Loading