bublejs / buble

https://buble.surge.sh
MIT License
869 stars 67 forks source link

Object.assign extra empty object when using spread operator #203

Closed manferlo81 closed 4 years ago

manferlo81 commented 5 years ago

I believe this is not the intended behavior, if it is, please just close this issue.

I setup this example to show my point, this is not a real life example.

const obj0 = { key: "value" };
const obj1 = { name: "john", age: 30, ...obj0 }
const obj2 = { ...obj1 }

I am getting...

var obj0 = { key: "value" };
var obj1 = Object.assign({}, {name: "name"}, obj0)
var obj2 = Object.assign({}, obj1)

I believe obj1 should be...

var obj1 = Object.assign({name: "name"}, obj0)

see here...

nathancahill commented 5 years ago

This is intended behavior. The spread operator will create a new object.

mourner commented 5 years ago

I think this is a valid issue — the second line should allocate exactly one object, but it allocates two ({} and {name: "john"}).

mourner commented 5 years ago

Or at least the behavior is correct, but we could avoid redundant allocation to improve performance of generated code.

nathancahill commented 5 years ago

Ah, I see, I was looking at line 3. Line 2 could definitely be optimized.