bublejs / buble

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

fix: returning from nested loop only exits function if return value is truthy #266

Closed alanclarke closed 2 years ago

alanclarke commented 3 years ago

Normally, if a function contains an inner loop and an outer loop, a return statement will exit the whole function.

Since buble transforms loops into functions, in order to preserve this behaviour there is a test for whether there is a returned value from the inner loop, and if so, we break out of the outer loop as well.

However, the test for whether there is a returned value is whether the return value is truthy. So if you return false, or undefined or zero, the outer loop will continue, causing some very had to diagnose and unintuitive bugs, especially when hidden away by sourcemaps.

This PR improves on the current implementation by testing whether the return value is defined. It isn't a perfect solution since you could have a return statement with no return value, and that would also fail to exit the function. However, I don't really understand the codebase enough yet to suggest a better solution and this is probably better than nothing.

alanclarke commented 3 years ago

If I get some indication that this is likely to be merged I will spend the time to fix up the tests 😄