Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[CS2] Destructuring object spreads#4493

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
GeoffreyBooth merged 90 commits intojashkenas:2fromGeoffreyBooth:destructuring_object
Jun 30, 2017

Conversation

GeoffreyBooth
Copy link
Collaborator

Replacing#4473 andGeoffreyBooth#4. From original descriptions by@zdenko:

Connected to#3894.

In CS2 emit

obj= {x:1,y:2}obj1= {obj...,z:3}

to

obj1= {...obj,z:3}

Improve destructuring assignment for object literal:

{a,b,x...}= {a:1,b:2,c:3,d:4,e:5}a=1b=2x= {c:3,d:4,e:5}
{c,x...,e}= {a:1,b:2,c:3,d:4,e:5}c=3e=5x= {a:1,b:2,d:4}
{c,x...,a:p}={a:1,b:2,c:3,d:4,e:5}c=3p=1x={b:2,d:4,e:5}
futurists=sculptor:"Umberto Boccioni"painter:"Vladimir Burliuk"poet:name:"F.T. Marinetti"address: ["Via Roma 42R""Bellagio, Italy 22021"    ]{poet: {name,addr1...}}= futuristsaddr1= {address: ["Via Roma 42R","Bellagio, Italy 22021"]}{poet: {addr2...,name:title}}= futuristsaddr2= {address: ["Via Roma 42R","Bellagio, Italy 22021"]}

Basically, implement proposal forrest properties for ECMAScript in CS:{a, b, c...} = x

Since this proposal isn't yet at stage-4, CS doesn't compile to ES.
In my PR I catch rest element and assign remaining values to it:
{a, b, c...} = x compiles to:
a = x.a, b = x.b, c = extractObjectWithoutKeys(x, ['a', 'b'])

Multiple rest elements are disallowed. ES also requires the rest element to be the last, so compiler currently throws an error.

IMHO, CS should allow rest element anywhere, just like for arrays.

Later, when proposal reaches stage-4 we could implement similar conversion as you did for the function parameters, and ensure rest element is the last in the compiled JS.

a-x-, maullerz, bbtfr, and MaxPleaner reacted with hooray emoji
GeoffreyBoothand others added25 commitsFebruary 4, 2017 22:26
…ture `wrapInBraces` that uses `{` and `wrapInBrackets` that uses `[`
This dramatically improves the appearance of destructured imports.
…ture `wrapInBraces` that uses `{` and `wrapInBrackets` that uses `[`
@GeoffreyBoothGeoffreyBooth added this to the2.0.0 milestoneApr 9, 2017
@GeoffreyBooth
Copy link
CollaboratorAuthor

GeoffreyBooth commentedApr 9, 2017
edited
Loading

@zdenko Can you please merge2 into thedestructuring_object branch and resolve conflicts? I took a crack at it, but there are a lot of conflicts to resolve. Basically thedestructuring branch in#4478 overlapped with much of what you were doing. You branch started from my original destructuring approach, which wasn’t nearly as clean or robust as@connec’s, so for#4478 we abandoned mine in favor of his; and his got merged into2. So now you need to work backwards a bit and start from this new starting point, where object destructuring is already implemented in2, and you only need to add the object spread destructuring. The2 branch also already has destructured objects in function parameters.

If it’s easier to start a new branch that branches off of2, and then cherry-picks some work from thedesctructuring_object branch, that works too. I can change the branch that this PR pulls from. Please put the new branch on my repo, though, so the rest of us can contribute. Thanks!

@GeoffreyBoothGeoffreyBooth mentioned this pull requestApr 9, 2017
@danielbayley
Copy link
Contributor

danielbayley commentedApr 9, 2017
edited
Loading

Multiple rest elements are disallowed.

What about when merging objects like this?

letobj={a:1,b:2,c:3,d:4}letoverride={b:'b',d:'d'}constmerged={...obj, ...override}

Whichworks with thestage-3 preset in babel…

@GeoffreyBooth
Copy link
CollaboratorAuthor

@connec was that last commit it? Does this now have your blessing?

@lydell and@zdenko, do either of you want to final pass at this, or can I merge it in?

zdenko reacted with thumbs up emoji

@connec
Copy link
Collaborator

OK, that's definitely me done now@GeoffreyBooth. My last concern was the grammar, which didn't account for things like{super()...} or{this...}. There might still be some missing cases since using anything broader likeValue orExpression leads to ambiguity (as I'm sure@zdenko can testify).

I didn't add tests for those things, and it's really late here now. Other than that, I think this is good to go (of course, as a contributor to the PR I now can't be trusted to be impartial 😄).

GeoffreyBooth, zdenko, and laurentpayot reacted with thumbs up emoji

@GeoffreyBooth
Copy link
CollaboratorAuthor

At this point I’d rather merge it in and handle additional edge cases as further PRs. This has become an epic.

Last call!

danielbayley reacted with hooray emoji

@GeoffreyBooth
Copy link
CollaboratorAuthor

So I was writing the documentation for this, and the example I came up with doesn’t compile:

user=name:'Werner Heisenberg'occupation:'theoretical physicist'currentUser=  user...status:'Uncertain'

[stdin]:6:6: error: unexpected ...user...    ^^^

However changingcurrentUser tocurrentUser = { user..., status: 'Uncertain' } makes it work as expected. A similar example using arrays works as expected. Is this a bug?

@zdenko
Copy link
Collaborator

@GeoffreyBooth this works

currentUser = {  user...  status: 'Uncertain'}

I've tried to make it work without the{} in the past but always ended with ambiguity issues.

@GeoffreyBooth
Copy link
CollaboratorAuthor

Okay, well at least it just throws a compiler error for now. So if we figure out how to support that syntax later, we can add it in a later PR.

@GeoffreyBoothGeoffreyBooth merged commita7a6006 intojashkenas:2Jun 30, 2017
@GeoffreyBoothGeoffreyBooth changed the title[WIP][CS2] Destructuring object spreads[CS2] Destructuring object spreadsJun 30, 2017
@connec
Copy link
Collaborator

That's probably related tolookObjectish in the rewriter, which seems to only accept implicit object literals when there's a: involved (which is consistent with being unable to use shorthand properties in implicit objects.

Tbh we probably want to leave it like this, or we'll need to face down ambiguities in expressions like

fk: v, rest...

Which, currently, will compile to two arguments (an object literal and a splat param) but would be ambiguous if we allowed splats to exist in implicit objects.

lydell reacted with thumbs up emoji

@GeoffreyBoothGeoffreyBooth deleted the destructuring_object branchJuly 5, 2017 01:48
@GeoffreyBoothGeoffreyBooth mentioned this pull requestAug 31, 2017
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@connecconnecconnec left review comments

@vendethielvendethielvendethiel left review comments

@subhero24subhero24subhero24 left review comments

@zdenkozdenkozdenko left review comments

@xixixaoxixixaoxixixao left review comments

@lydelllydelllydell left review comments

Assignees
No one assigned
Projects
None yet
Milestone
2.0.0
Development

Successfully merging this pull request may close these issues.

9 participants
@GeoffreyBooth@danielbayley@zdenko@connec@helixbass@lydell@vendethiel@subhero24@xixixao

[8]ページ先頭

©2009-2025 Movatter.jp