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 all 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,34 @@ const handle = { | |
return; | ||
} | ||
|
||
// { KEY: MEMBER } = OBJ -> { KEY: _destructureSet(MEMBER) } = OBJ | ||
// { KEY: MEMBER = _VALUE } = OBJ -> { KEY: _destructureSet(MEMBER) = _VALUE } = OBJ | ||
// {...MEMBER} -> {..._destructureSet(MEMBER)} | ||
// | ||
// [MEMBER] = ARR -> [_destructureSet(MEMBER)] = ARR | ||
// [MEMBER = _VALUE] = ARR -> [_destructureSet(MEMBER) = _VALUE] = ARR | ||
// [...MEMBER] -> [..._destructureSet(MEMBER)] | ||
if ( | ||
// { KEY: MEMBER } = OBJ | ||
(parentPath.isObjectProperty({ value: node }) && | ||
parentPath.parentPath.isObjectPattern()) || | ||
// { KEY: MEMBER = _VALUE } = OBJ | ||
(parentPath.isAssignmentPattern({ left: node }) && | ||
parentPath.parentPath.isObjectProperty({ value: parent }) && | ||
parentPath.parentPath.parentPath.isObjectPattern()) || | ||
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. Nit: I think we can actually join a few of the conditions here. 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. i guess so, it's just to make my intentions clearer, not sure will assignment pattern be a child of not ust 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. We can move these checks to be assertions: ( 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. @nicolo-ribaudo what is the rationale behind using 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. Just that with an assertion is clear that if this is an assignment expression, the parent will always be an object/array destructing 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 ok nevermind, we can't assert two different parent types. |
||
// [MEMBER] = ARR | ||
parentPath.isArrayPattern() || | ||
// [MEMBER = _VALUE] = ARR | ||
(parentPath.isAssignmentPattern({ left: node }) && | ||
parentPath.parentPath.isArrayPattern()) || | ||
// {...MEMBER} | ||
// [...MEMBER] | ||
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. Man, some of these syntaxes are crazy. |
||
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,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] = 1; | ||
[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,12 @@ | ||
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 = 5] = props); | ||
} | ||
|
||
getClient() { | ||
return this.#client; | ||
} | ||
} | ||
|
||
const foo = new Foo([]); | ||
expect(foo.getClient()).toEqual(5); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class Foo { | ||
#client | ||
|
||
constructor(props) { | ||
([this.#client = 5] = props); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
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] = 5] = 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,12 @@ | ||
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,17 @@ | ||
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,15 @@ | ||
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"); |
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?