bublejs / buble

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

Miscompilation with spread operator and lack of semicolons. #210

Open Dirbaio opened 5 years ago

Dirbaio commented 5 years ago

https://buble.surge.sh/#function%20foobar(a%2C%20b)%20%7B%0A%20%20return%20%7B%0A%20%20%20%20foo%3A%20%5Ba%2C%20b%5D%2C%0A%20%20%20%20bar%3A%20%5Bb%2C%20a%5D%2C%0A%20%20%7D%3B%0A%7D%0A%0Afunction%20foo()%20%7B%0A%20%20%09const%20res%20%3D%20%7B%0A%20%20%20%20%09foo%3A%20%5B%5D%2C%0A%20%20%20%20%20%20%09bar%3A%20%5B%5D%2C%0A%20%20%20%20%7D%0A%20%20%20%20const%20r%20%3D%20foobar(1%2C%202)%0A%20%20%20%20res.foo.push(...r.foo)%0A%20%20%20%20res.bar.push(...r.bar)%0A%20%20%09return%20res%0A%7D%0A

Original code works, compiled code fails with foobar(...) is not a function.

This is because lack of semicolons makes the resulting code be executed like this:

    var r = foobar(1, 2)(ref = res.foo).push.apply(ref, r.foo)

while it should be like this

    var r = foobar(1, 2);
    (ref = res.foo).push.apply(ref, r.foo)
mourner commented 5 years ago

Yep, that's a bug — thanks for reporting! Minimal repro: https://buble.surge.sh/#a()%0Ab.c.d(...e)