-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement
privateFieldsAsSymbols
assumption for classes (#15435)
Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
- Loading branch information
1 parent
79e1452
commit 3e60843
Showing
119 changed files
with
1,931 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...posal-class-properties/both-privateFieldsAsProperties-and-privateFieldsAsSymbols/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
; |
8 changes: 8 additions & 0 deletions
8
...l-class-properties/both-privateFieldsAsProperties-and-privateFieldsAsSymbols/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"plugins": ["proposal-class-properties"], | ||
"assumptions": { | ||
"privateFieldsAsProperties": true, | ||
"privateFieldsAsSymbols": true | ||
}, | ||
"throws": "Cannot enable both the \"privateFieldsAsProperties\" and \"privateFieldsAsSymbols\" assumptions as the same time." | ||
} |
2 changes: 1 addition & 1 deletion
2
...ugin/test/fixtures/plugin-proposal-class-properties/warn-loose-and-assumptions/stderr.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
[proposal-class-properties]: You are using the "loose: true" option and you are explicitly setting a value for the "setPublicClassFields" assumption. The "loose" option can cause incompatibilities with the other class features plugins, so it's recommended that you replace it with the following top-level option: | ||
"assumptions": { | ||
"setPublicClassFields": true, | ||
"privateFieldsAsProperties": true | ||
"privateFieldsAsSymbols": true | ||
} |
2 changes: 1 addition & 1 deletion
2
...ss-properties/test/fixtures/assumption-setPublicClassFields/static-super-loose/stderr.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
[proposal-class-properties]: You are using the "loose: true" option and you are explicitly setting a value for the "setPublicClassFields" assumption. The "loose" option can cause incompatibilities with the other class features plugins, so it's recommended that you replace it with the following top-level option: | ||
"assumptions": { | ||
"setPublicClassFields": true, | ||
"privateFieldsAsProperties": true | ||
"privateFieldsAsSymbols": true | ||
} |
31 changes: 31 additions & 0 deletions
31
...gin-proposal-private-methods/test/fixtures/accessors-privateFieldsAsSymbols/basic/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
class Cl { | ||
#privateField = "top secret string"; | ||
|
||
constructor() { | ||
this.publicField = "not secret string"; | ||
} | ||
|
||
get #privateFieldValue() { | ||
return this.#privateField; | ||
} | ||
|
||
set #privateFieldValue(newValue) { | ||
this.#privateField = newValue; | ||
} | ||
|
||
publicGetPrivateField() { | ||
return this.#privateFieldValue; | ||
} | ||
|
||
publicSetPrivateField(newValue) { | ||
this.#privateFieldValue = newValue; | ||
} | ||
} | ||
|
||
const cl = new Cl(); | ||
|
||
expect(cl.publicGetPrivateField()).toEqual("top secret string"); | ||
|
||
cl.publicSetPrivateField("new secret string"); | ||
expect(cl.publicGetPrivateField()).toEqual("new secret string"); | ||
|
23 changes: 23 additions & 0 deletions
23
...in-proposal-private-methods/test/fixtures/accessors-privateFieldsAsSymbols/basic/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class Cl { | ||
#privateField = "top secret string"; | ||
|
||
constructor() { | ||
this.publicField = "not secret string"; | ||
} | ||
|
||
get #privateFieldValue() { | ||
return this.#privateField; | ||
} | ||
|
||
set #privateFieldValue(newValue) { | ||
this.#privateField = newValue; | ||
} | ||
|
||
publicGetPrivateField() { | ||
return this.#privateFieldValue; | ||
} | ||
|
||
publicSetPrivateField(newValue) { | ||
this.#privateFieldValue = newValue; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...n-proposal-private-methods/test/fixtures/accessors-privateFieldsAsSymbols/basic/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
var _privateField = /*#__PURE__*/Symbol("privateField"); | ||
var _privateFieldValue = /*#__PURE__*/Symbol("privateFieldValue"); | ||
class Cl { | ||
constructor() { | ||
Object.defineProperty(this, _privateFieldValue, { | ||
get: _get_privateFieldValue, | ||
set: _set_privateFieldValue | ||
}); | ||
Object.defineProperty(this, _privateField, { | ||
writable: true, | ||
value: "top secret string" | ||
}); | ||
this.publicField = "not secret string"; | ||
} | ||
publicGetPrivateField() { | ||
return babelHelpers.classPrivateFieldLooseBase(this, _privateFieldValue)[_privateFieldValue]; | ||
} | ||
publicSetPrivateField(newValue) { | ||
babelHelpers.classPrivateFieldLooseBase(this, _privateFieldValue)[_privateFieldValue] = newValue; | ||
} | ||
} | ||
function _get_privateFieldValue() { | ||
return babelHelpers.classPrivateFieldLooseBase(this, _privateField)[_privateField]; | ||
} | ||
function _set_privateFieldValue(newValue) { | ||
babelHelpers.classPrivateFieldLooseBase(this, _privateField)[_privateField] = newValue; | ||
} |
13 changes: 13 additions & 0 deletions
13
...al-private-methods/test/fixtures/accessors-privateFieldsAsSymbols/get-only-setter/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class Cl { | ||
#privateField = 0; | ||
|
||
set #privateFieldValue(newValue) { | ||
this.#privateField = newValue; | ||
} | ||
|
||
constructor() { | ||
expect(this.#privateFieldValue).toBeUndefined(); | ||
} | ||
} | ||
|
||
const cl = new Cl(); |
11 changes: 11 additions & 0 deletions
11
...l-private-methods/test/fixtures/accessors-privateFieldsAsSymbols/get-only-setter/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class Cl { | ||
#privateField = 0; | ||
|
||
set #privateFieldValue(newValue) { | ||
this.#privateField = newValue; | ||
} | ||
|
||
constructor() { | ||
this.publicField = this.#privateFieldValue; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...-private-methods/test/fixtures/accessors-privateFieldsAsSymbols/get-only-setter/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
var _privateField = /*#__PURE__*/Symbol("privateField"); | ||
var _privateFieldValue = /*#__PURE__*/Symbol("privateFieldValue"); | ||
class Cl { | ||
constructor() { | ||
Object.defineProperty(this, _privateFieldValue, { | ||
get: void 0, | ||
set: _set_privateFieldValue | ||
}); | ||
Object.defineProperty(this, _privateField, { | ||
writable: true, | ||
value: 0 | ||
}); | ||
this.publicField = babelHelpers.classPrivateFieldLooseBase(this, _privateFieldValue)[_privateFieldValue]; | ||
} | ||
} | ||
function _set_privateFieldValue(newValue) { | ||
babelHelpers.classPrivateFieldLooseBase(this, _privateField)[_privateField] = newValue; | ||
} |
11 changes: 11 additions & 0 deletions
11
...in-proposal-private-methods/test/fixtures/accessors-privateFieldsAsSymbols/helper/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
let foo; | ||
class Cl { | ||
set #foo(v) { return 1 } | ||
test() { | ||
foo = this.#foo = 2; | ||
} | ||
} | ||
|
||
new Cl().test(); | ||
|
||
expect(foo).toBe(2); |
6 changes: 6 additions & 0 deletions
6
...ugin-proposal-private-methods/test/fixtures/accessors-privateFieldsAsSymbols/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"plugins": ["proposal-private-methods", "proposal-class-properties"], | ||
"assumptions": { | ||
"privateFieldsAsSymbols": true | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...private-methods/test/fixtures/accessors-privateFieldsAsSymbols/preserve-comments/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class C { | ||
/* before get a */ | ||
get #a() { return 42 }; | ||
/* after get a */ | ||
|
||
/* before set a */ | ||
set #a(v) {} | ||
/* after set a */ | ||
} |
17 changes: 17 additions & 0 deletions
17
...rivate-methods/test/fixtures/accessors-privateFieldsAsSymbols/preserve-comments/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
var _a = /*#__PURE__*/Symbol("a"); | ||
class C { | ||
constructor() { | ||
/* before get a */ | ||
Object.defineProperty(this, _a, { | ||
get: _get_a, | ||
set: _set_a | ||
}); | ||
} | ||
/* after set a */ | ||
} | ||
function _get_a() { | ||
return 42; | ||
} | ||
/* after get a */ | ||
/* before set a */ | ||
function _set_a(v) {} |
14 changes: 14 additions & 0 deletions
14
...al-private-methods/test/fixtures/accessors-privateFieldsAsSymbols/set-only-getter/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class Cl { | ||
#privateField = 0; | ||
|
||
get #privateFieldValue() { | ||
return this.#privateField; | ||
} | ||
|
||
constructor() { | ||
expect(() => this.#privateFieldValue = 1).toThrow(TypeError); | ||
expect(() => ([this.#privateFieldValue] = [1])).toThrow(TypeError); | ||
} | ||
} | ||
|
||
const cl = new Cl(); |
12 changes: 12 additions & 0 deletions
12
...l-private-methods/test/fixtures/accessors-privateFieldsAsSymbols/set-only-getter/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class Cl { | ||
#privateField = 0; | ||
|
||
get #privateFieldValue() { | ||
return this.#privateField; | ||
} | ||
|
||
constructor() { | ||
this.#privateFieldValue = 1; | ||
([this.#privateFieldValue] = [1]); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...-private-methods/test/fixtures/accessors-privateFieldsAsSymbols/set-only-getter/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
var _privateField = /*#__PURE__*/Symbol("privateField"); | ||
var _privateFieldValue = /*#__PURE__*/Symbol("privateFieldValue"); | ||
class Cl { | ||
constructor() { | ||
Object.defineProperty(this, _privateFieldValue, { | ||
get: _get_privateFieldValue, | ||
set: void 0 | ||
}); | ||
Object.defineProperty(this, _privateField, { | ||
writable: true, | ||
value: 0 | ||
}); | ||
babelHelpers.classPrivateFieldLooseBase(this, _privateFieldValue)[_privateFieldValue] = 1; | ||
[babelHelpers.classPrivateFieldLooseBase(this, _privateFieldValue)[_privateFieldValue]] = [1]; | ||
} | ||
} | ||
function _get_privateFieldValue() { | ||
return babelHelpers.classPrivateFieldLooseBase(this, _privateField)[_privateField]; | ||
} |
Oops, something went wrong.