-
-
Notifications
You must be signed in to change notification settings - Fork 188
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
(fix) destructuring inside $ #445
(fix) destructuring inside $ #445
Conversation
Also
it would be nice if we can find the equivalent of this line in typescript I looked into |
Good point, forgot about them. If you like you can take over, not going to work in this today anymore. |
you can use periscopic#extract_names https://github.com/Rich-Harris/periscopic#extract_identifiers-and-extract_names to get all the identifier names within the assignment |
Oh that's a nice way of getting it, but we are using the typescript AST here so we cannot use this. |
I think we could just recreate that function for typescript AST. Another option would be using typescript-eslint parser to transform AST to estree compliant but that seems a little bit too overkill |
When looking into the solution, I found another issue. Because we're transforming this:
to
to infer type for a. So when one of the destructured identifiers is already defined but others don't, how do we transform it to do an assignment as well as declaration. like this:
|
This situation came to my mind, too, and my current solution for it is "that's such a corner case, just don't try to infer the type" 😄
|
Oh! I just thought like "what about this situation you wrote?" 😂 |
Oooh you mean a different situation, I get it now. What if the user has defined one of them ... well yes, we somehow would have to add the other User:
Gets transformed to:
Sorry for the confusion 😄 |
So to summarize the cases: No let x; by user Input $: ({a,b} = c); Output: let {a,b} = c; At least one let x; defined by user Input: let a: number;
$: ({a,b} = c); Output let a: number;
let b; // <- we don't infer the type, too much work for such an edge case
$: ({a,b} = c); Same goes for |
I added a ts version of Rich's |
Oh that's great 👍 I'll incorporate it into my changes. |
Thanks a lot for that adapted method @jasonlyu123 ! Tests are passing now for both object and array destructuring. Could you take one more look at the code if everything is okay? |
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.
Found some issue need some tweak
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.
That's a clever workaround 👍
#444
TODOs:
( )
around the expression if there is no transformation from$: ...
tolet ...
( )
in such a way that the inner transformations are preserved and not removed as well