Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Evaluate object and initializer when setting a private method (#12707)
* fix: evaluate initializer when a private method is set as a field * make legacy node happy * add accessor test cases * fix: evaluate object before RHS * fix: evaluate object before throwing writeOnlyError
- Loading branch information
Showing
20 changed files
with
253 additions
and
16 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
13 changes: 13 additions & 0 deletions
13
.../babel-plugin-proposal-private-methods/test/fixtures/accessors-loose/reassignment/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 @@ | ||
let counter = 0; | ||
class Foo { | ||
constructor() { | ||
this.#privateFieldValue = ++counter; | ||
} | ||
|
||
get #privateFieldValue() { | ||
return 42; | ||
} | ||
} | ||
|
||
expect(() => new Foo).toThrow(); | ||
expect(counter).toBe(1); |
10 changes: 10 additions & 0 deletions
10
...babel-plugin-proposal-private-methods/test/fixtures/accessors-loose/reassignment/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,10 @@ | ||
let counter = 0; | ||
class Foo { | ||
constructor() { | ||
this.#privateFieldValue = ++counter; | ||
} | ||
|
||
get #privateFieldValue() { | ||
return 42; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...abel-plugin-proposal-private-methods/test/fixtures/accessors-loose/reassignment/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 counter = 0; | ||
|
||
var _privateFieldValue = babelHelpers.classPrivateFieldLooseKey("privateFieldValue"); | ||
|
||
class Foo { | ||
constructor() { | ||
Object.defineProperty(this, _privateFieldValue, { | ||
get: _get_privateFieldValue, | ||
set: void 0 | ||
}); | ||
babelHelpers.classPrivateFieldLooseBase(this, _privateFieldValue)[_privateFieldValue] = ++counter; | ||
} | ||
|
||
} | ||
|
||
var _get_privateFieldValue = function () { | ||
return 42; | ||
}; |
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
18 changes: 18 additions & 0 deletions
18
packages/babel-plugin-proposal-private-methods/test/fixtures/accessors/reassignment/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,18 @@ | ||
let results = []; | ||
class Foo { | ||
constructor() { | ||
this.self.#privateFieldValue = results.push(2); | ||
} | ||
|
||
get self() { | ||
results.push(1); | ||
return this; | ||
} | ||
|
||
get #privateFieldValue() { | ||
return 42; | ||
} | ||
} | ||
|
||
expect(() => new Foo).toThrow(TypeError); | ||
expect(results).toStrictEqual([1, 2]); |
15 changes: 15 additions & 0 deletions
15
packages/babel-plugin-proposal-private-methods/test/fixtures/accessors/reassignment/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,15 @@ | ||
let results = []; | ||
class Foo { | ||
constructor() { | ||
this.self.#privateFieldValue = results.push(2); | ||
} | ||
|
||
get self() { | ||
results.push(1); | ||
return this; | ||
} | ||
|
||
get #privateFieldValue() { | ||
return 42; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...ages/babel-plugin-proposal-private-methods/test/fixtures/accessors/reassignment/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,24 @@ | ||
var results = []; | ||
|
||
var _privateFieldValue = new WeakMap(); | ||
|
||
class Foo { | ||
constructor() { | ||
_privateFieldValue.set(this, { | ||
get: _get_privateFieldValue, | ||
set: void 0 | ||
}); | ||
|
||
this.self, results.push(2), babelHelpers.readOnlyError("#privateFieldValue"); | ||
} | ||
|
||
get self() { | ||
results.push(1); | ||
return this; | ||
} | ||
|
||
} | ||
|
||
var _get_privateFieldValue = function () { | ||
return 42; | ||
}; |
9 changes: 8 additions & 1 deletion
9
...ges/babel-plugin-proposal-private-methods/test/fixtures/accessors/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
10 changes: 9 additions & 1 deletion
10
...es/babel-plugin-proposal-private-methods/test/fixtures/accessors/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 |
---|---|---|
@@ -1,11 +1,19 @@ | ||
class Cl { | ||
#privateField = 0; | ||
counter = 0; | ||
|
||
get #privateFieldValue() { | ||
return this.#privateField; | ||
} | ||
|
||
get self() { | ||
this.counter++; | ||
return this; | ||
} | ||
|
||
constructor() { | ||
this.#privateFieldValue = 1; | ||
this.self.#privateFieldValue = 1 | ||
} | ||
} | ||
|
||
const cl = new Cl(); |
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
13 changes: 13 additions & 0 deletions
13
...l-plugin-proposal-private-methods/test/fixtures/private-method-loose/reassignment/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 @@ | ||
let counter = 0; | ||
class Foo { | ||
constructor() { | ||
this.#privateMethod = ++counter; | ||
} | ||
|
||
#privateMethod() { | ||
return 42; | ||
} | ||
} | ||
|
||
expect(() => new Foo).toThrow(); | ||
expect(counter).toBe(1); |
10 changes: 10 additions & 0 deletions
10
...-plugin-proposal-private-methods/test/fixtures/private-method-loose/reassignment/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,10 @@ | ||
let counter = 0; | ||
class Foo { | ||
constructor() { | ||
this.#privateMethod = ++counter; | ||
} | ||
|
||
#privateMethod() { | ||
return 42; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...plugin-proposal-private-methods/test/fixtures/private-method-loose/reassignment/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 counter = 0; | ||
|
||
var _privateMethod = babelHelpers.classPrivateFieldLooseKey("privateMethod"); | ||
|
||
class Foo { | ||
constructor() { | ||
Object.defineProperty(this, _privateMethod, { | ||
value: _privateMethod2 | ||
}); | ||
babelHelpers.classPrivateFieldLooseBase(this, _privateMethod)[_privateMethod] = ++counter; | ||
} | ||
|
||
} | ||
|
||
var _privateMethod2 = function _privateMethod2() { | ||
return 42; | ||
}; |
12 changes: 9 additions & 3 deletions
12
...ages/babel-plugin-proposal-private-methods/test/fixtures/private-method/read-only/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 |
---|---|---|
@@ -1,9 +1,15 @@ | ||
class A { | ||
counter = 0; | ||
#method() {} | ||
self() { | ||
this.counter++; | ||
return this; | ||
} | ||
|
||
run() { | ||
this.#method = 2; | ||
constructor() { | ||
expect(() => this.self().#method = 2).toThrow(TypeError); | ||
expect(this.counter).toBe(1); | ||
} | ||
} | ||
|
||
expect(() => new A().run()).toThrow(TypeError); | ||
new A; |
9 changes: 7 additions & 2 deletions
9
...ges/babel-plugin-proposal-private-methods/test/fixtures/private-method/read-only/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 |
---|---|---|
@@ -1,7 +1,12 @@ | ||
class A { | ||
counter = 0; | ||
#method() {} | ||
self() { | ||
this.counter++; | ||
return this; | ||
} | ||
|
||
run() { | ||
this.#method = 2; | ||
constructor() { | ||
this.self().#method = 2; | ||
} | ||
} |
10 changes: 7 additions & 3 deletions
10
...es/babel-plugin-proposal-private-methods/test/fixtures/private-method/read-only/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
18 changes: 18 additions & 0 deletions
18
...s/babel-plugin-proposal-private-methods/test/fixtures/private-method/reassignment/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,18 @@ | ||
let results = []; | ||
class Foo { | ||
constructor() { | ||
this.self.#privateFieldValue = results.push(2); | ||
} | ||
|
||
get self() { | ||
results.push(1); | ||
return this; | ||
} | ||
|
||
#privateFieldValue() { | ||
return 42; | ||
} | ||
} | ||
|
||
expect(() => new Foo).toThrow(TypeError); | ||
expect(results).toStrictEqual([1, 2]); |
15 changes: 15 additions & 0 deletions
15
.../babel-plugin-proposal-private-methods/test/fixtures/private-method/reassignment/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,15 @@ | ||
let results = []; | ||
class Foo { | ||
constructor() { | ||
this.self.#privateFieldValue = results.push(2); | ||
} | ||
|
||
get self() { | ||
results.push(1); | ||
return this; | ||
} | ||
|
||
#privateFieldValue() { | ||
return 42; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...babel-plugin-proposal-private-methods/test/fixtures/private-method/reassignment/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,21 @@ | ||
var results = []; | ||
|
||
var _privateFieldValue = new WeakSet(); | ||
|
||
class Foo { | ||
constructor() { | ||
_privateFieldValue.add(this); | ||
|
||
this.self, results.push(2), babelHelpers.readOnlyError("#privateFieldValue"); | ||
} | ||
|
||
get self() { | ||
results.push(1); | ||
return this; | ||
} | ||
|
||
} | ||
|
||
var _privateFieldValue2 = function _privateFieldValue2() { | ||
return 42; | ||
}; |