Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Wrong transpilling @babel/standalone@7.24.3 when destructuring to a new object #16378

Open
1 task
marionicolas opened this issue Mar 24, 2024 · 3 comments

Comments

@marionicolas
Copy link

馃捇

  • Would you like to work on a fix?

How are you using Babel?

Programmatic API (babel.transform, babel.parse)

Input code

Using https://unpkg.com/@babel/standalone@7.24.3/babel.min.js
...
(sorry for jQuery use here, its just a test case)
const c = $('script[type="text/babel"]').text()
var js = Babel.transform(c, { presets: ["react","es2015-no-commonjs"] }).code;
$("body").append($('<script type="module">').text(js))
...

<script type="text/babel" data-type="module" data-presets="react"> ....... .....Some react app stuff here .... console.log({...tags,[newTag.current.key]:newTag.current.val}) //This is the important line, the thing here is to construct a new object from tags props adding a new key:value to the new object .... </script>

This line is transpilled to:
console.log(_defineProperty({}, newTag.current.key, newTag.current.val));
loosing the prev tags object props...

I had to change this line to this in order to be well transpilled:
setTags({...tags,...{[newTag.current.key]:newTag.current.val}}) //Notice that now the new prop is encapsulated in a seconly destructured object, and now this is well interpreted as:
setTags({
...tags,
..._defineProperty({}, newTag.current.key, newTag.current.val)
});

Configuration file name

No response

Configuration

No response

Current and expected behavior

Tested the code widthout transpilling and works well so I supouse it is well formed.

Environment

Chrome Versi贸n 122.0.6261.129 (Build oficial) (64 bits)

Possible solution

No response

Additional context

Sorry if i dont make the ticket well, I just worked around, but think this could be a problem for someon else and too dificult to describe in a search engine.
Thnx in advance.

@babel-bot
Copy link
Collaborator

Hey @marionicolas! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite.

@nicolo-ribaudo
Copy link
Member

Why do you want to transpile es2015 features, sich as computed keys, but no newer features, such as object spread?

I don't think this case matters much, because all browser that supports ... in objects has full support for es2015.

@marionicolas
Copy link
Author

I dont, maybe Im doing something wrong?
Makes me think to ways, is there a way of setting a prest for no-commonjs but without es2015? (if there is, my fault)
But, if this pereset exists (the one i麓m using now), shouldnt it work ok? I mean, this error in transpilling could be weeeery difficult to debug because, it changes the way the result is affected but no error thrown and no alert of the situation, if one dont imagine something like this could happen in a more complex scenario... could be much of a problem to detect.
Sorry for my english thoug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants