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
destructuring private fields with array pattern / object pattern #10017
Changes from 9 commits
eccf5d0
3b4a973
b374e44
1772067
7673658
29b6f54
94f7d2f
d344e8d
692eee3
ab1e2e2
b9538c3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -100,6 +100,20 @@ const handle = { | |
return; | ||
} | ||
|
||
// { KEY: MEMBER } = OBJ -> { KEY: _destructureSet(MEMBER) } = OBJ | ||
// [MEMBER] = ARR -> [_destructureSet(MEMBER)] = ARR | ||
// [...MEMBER] -> [..._destructureSet(MEMBER)] | ||
// {...MEMBER} -> {..._destructureSet(MEMBER)} | ||
if ( | ||
(parentPath.isObjectProperty({ value: node }) && | ||
parentPath.parentPath.isObjectPattern()) || | ||
parentPath.isArrayPattern() || | ||
parentPath.isRestElement() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we also need to check for [this.#foo = 2] = [] There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. checked and added more test case |
||
) { | ||
member.replaceWith(this.destructureSet(member)); | ||
return; | ||
} | ||
|
||
// MEMBER -> _get(MEMBER) | ||
member.replaceWith(this.get(member)); | ||
}, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class Foo { | ||
#client | ||
|
||
constructor(props) { | ||
this.#client = 1; | ||
;([this.x = this.#client, this.#client, this.y = this.#client] = props); | ||
} | ||
|
||
getClient() { | ||
return this.#client; | ||
} | ||
} | ||
|
||
const foo = new Foo([undefined, 'bar']); | ||
expect(foo.getClient()).toBe('bar'); | ||
expect(foo.x).toBe(1); | ||
expect(foo.y).toBe('bar'); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class Foo { | ||
#client | ||
|
||
constructor(props) { | ||
this.#client = 1; | ||
;([this.x = this.#client, this.#client, this.y = this.#client] = props); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
var Foo = function Foo(props) { | ||
"use strict"; | ||
|
||
babelHelpers.classCallCheck(this, Foo); | ||
Object.defineProperty(this, _client, { | ||
writable: true, | ||
value: void 0 | ||
}); | ||
babelHelpers.classPrivateFieldLooseBase(this, _client)[_client] = 1; | ||
; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where does this extra semicolon come from? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh, it's due to an extra comma in the input file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. removed extra comma |
||
[this.x = babelHelpers.classPrivateFieldLooseBase(this, _client)[_client], babelHelpers.classPrivateFieldLooseBase(this, _client)[_client], this.y = babelHelpers.classPrivateFieldLooseBase(this, _client)[_client]] = props; | ||
}; | ||
|
||
var _client = babelHelpers.classPrivateFieldLooseKey("client"); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class Foo { | ||
#client | ||
|
||
constructor(props) { | ||
let x; | ||
;([x, ...this.#client] = props); | ||
} | ||
|
||
getClient() { | ||
return this.#client; | ||
} | ||
} | ||
|
||
const foo = new Foo(['foo', 'bar', 'baz', 'quu']); | ||
expect(foo.getClient()).toEqual(['bar', 'baz', 'quu']); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class Foo { | ||
#client | ||
|
||
constructor(props) { | ||
;([x, ...this.#client] = props); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
var Foo = function Foo(props) { | ||
"use strict"; | ||
|
||
babelHelpers.classCallCheck(this, Foo); | ||
Object.defineProperty(this, _client, { | ||
writable: true, | ||
value: void 0 | ||
}); | ||
; | ||
[x, ...babelHelpers.classPrivateFieldLooseBase(this, _client)[_client]] = props; | ||
}; | ||
|
||
var _client = babelHelpers.classPrivateFieldLooseKey("client"); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class Foo { | ||
#client | ||
|
||
constructor(props) { | ||
;([this.#client] = props); | ||
} | ||
|
||
getClient() { | ||
return this.#client; | ||
} | ||
} | ||
|
||
const foo = new Foo(['bar']); | ||
expect(foo.getClient()).toBe('bar'); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class Foo { | ||
#client | ||
|
||
constructor(props) { | ||
;([this.#client] = props); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
var Foo = function Foo(props) { | ||
"use strict"; | ||
|
||
babelHelpers.classCallCheck(this, Foo); | ||
Object.defineProperty(this, _client, { | ||
writable: true, | ||
value: void 0 | ||
}); | ||
; | ||
[babelHelpers.classPrivateFieldLooseBase(this, _client)[_client]] = props; | ||
}; | ||
|
||
var _client = babelHelpers.classPrivateFieldLooseKey("client"); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class Foo { | ||
#client | ||
|
||
constructor(props) { | ||
this.#client = 'foo'; | ||
;({ x: this.x = this.#client, y: this.#client, z: this.z = this.#client } = props) | ||
} | ||
|
||
getClient() { | ||
return this.#client; | ||
} | ||
} | ||
|
||
const foo = new Foo({ y: 'bar' }); | ||
expect(foo.getClient()).toBe('bar'); | ||
expect(foo.x).toBe('foo'); | ||
expect(foo.z).toBe('bar'); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class Foo { | ||
#client | ||
|
||
constructor(props) { | ||
this.#client = 'foo'; | ||
;({ x: this.x = this.#client, y: this.#client, z: this.z = this.#client } = props) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
var Foo = function Foo(props) { | ||
"use strict"; | ||
|
||
babelHelpers.classCallCheck(this, Foo); | ||
Object.defineProperty(this, _client, { | ||
writable: true, | ||
value: void 0 | ||
}); | ||
babelHelpers.classPrivateFieldLooseBase(this, _client)[_client] = 'foo'; | ||
; | ||
({ | ||
x: this.x = babelHelpers.classPrivateFieldLooseBase(this, _client)[_client], | ||
y: babelHelpers.classPrivateFieldLooseBase(this, _client)[_client], | ||
z: this.z = babelHelpers.classPrivateFieldLooseBase(this, _client)[_client] | ||
} = props); | ||
}; | ||
|
||
var _client = babelHelpers.classPrivateFieldLooseKey("client"); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class Foo { | ||
#client | ||
|
||
constructor(props) { | ||
let x; | ||
;({ x, ...this.#client } = props) | ||
} | ||
|
||
getClient() { | ||
return this.#client; | ||
} | ||
} | ||
|
||
const foo = new Foo({ x: 'foo', y: 'bar', z: 'baz' }); | ||
expect(foo.getClient()).toEqual({ y: 'bar', z: 'baz' }); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"plugins": [ | ||
"transform-destructuring", | ||
[ | ||
"external-helpers", | ||
{ | ||
"helperVersion": "7.4.4" | ||
} | ||
], | ||
"proposal-class-properties", | ||
"transform-classes", | ||
"transform-block-scoping", | ||
"syntax-class-properties" | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class Foo { | ||
#client | ||
|
||
constructor(props) { | ||
;({ x, ...this.#client } = props) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
var Foo = function Foo(props) { | ||
"use strict"; | ||
|
||
babelHelpers.classCallCheck(this, Foo); | ||
Object.defineProperty(this, _client, { | ||
writable: true, | ||
value: void 0 | ||
}); | ||
; | ||
({ | ||
x, | ||
...babelHelpers.classPrivateFieldLooseBase(this, _client)[_client] | ||
} = props); | ||
}; | ||
|
||
var _client = babelHelpers.classPrivateFieldLooseKey("client"); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class Foo { | ||
#client | ||
|
||
constructor(props) { | ||
;({ client: this.#client } = props) | ||
} | ||
|
||
getClient() { | ||
return this.#client; | ||
} | ||
} | ||
|
||
const foo = new Foo({ client: 'bar' }); | ||
expect(foo.getClient()).toBe('bar'); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class Foo { | ||
#client | ||
|
||
constructor(props) { | ||
;({ client: this.#client } = props) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
var Foo = function Foo(props) { | ||
"use strict"; | ||
|
||
babelHelpers.classCallCheck(this, Foo); | ||
Object.defineProperty(this, _client, { | ||
writable: true, | ||
value: void 0 | ||
}); | ||
; | ||
({ | ||
client: babelHelpers.classPrivateFieldLooseBase(this, _client)[_client] | ||
} = props); | ||
}; | ||
|
||
var _client = babelHelpers.classPrivateFieldLooseKey("client"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I remember correctly, this will affect
super.foo
expressions, too. Can you add a test for assigning[super.foo] = array
, too?