babel/babel

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

marionicolas opened this issue · 3 comments

💻

  • 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.

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.

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.

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.