swc-project / swc

Rust-based platform for the Web

Home Page:https://swc.rs

Repository from Github https://github.comswc-project/swcRepository from Github https://github.comswc-project/swc

ES5 destructuring transform drops array hole and shifts default value assignment

Yeaseen opened this issue · comments

Describe the bug

When transpiling array destructuring with holes and default values inside async functions, SWC’s ES5 output fails to preserve the hole. As a result, the default value is skipped and the wrong element is bound. Node.js and Babel output the expected result, but SWC assigns the next array element instead.

Input code

const timeoutPromise = (timeout) => new Promise(resolve => setTimeout(resolve, timeout));

const processArray = async (array) => {
    const [first, , third = 'default', ...rest] = array;
    
    if (!first || !third) {
        return; 
    }
    await timeoutPromise(10);
    return {first, third, rest};
}

const func1 = async () => {
    return await processArray(['value1', 'value2', , 'value4', 'value5', 'value6', 'value7']);
}

const main = async () => {
    const res = await func1();
    console.log(res);
}

main();

Config

Link to the code that reproduces this issue

https://play.swc.rs/?version=1.13.5&code=H4sIAAAAAAAAA22Rz2rDMAzG73kK9RQbQljG%2FhxCB3uDHXorPZhU2QyOXWynpbR591q2k4UxH4ws6ftJX9IZ7Tx4OaAZ%2FZc1g3QIW2A5w2H7ARovkEvMojPqjJR26Hepa85WM4jztii6iD5Z06Fzn9aKawALd9UdMEHPCL8VEE7q3ffSOl9B4PxIewzt5RF7MSpfVlDXdRjjDwQhdRuF8ZI9sE3Uwv0OmyjmmUzHoh%2BtblPzFG9xEfKvb9Y88URNArjlfSKwAho%2FtcU0W%2BtH3TW%2FnlZ2sj4NWX8Bti%2FPQo3YBEMpei7Jb4pfluzrEr0t0Xt54Kvpg5D63%2BGpHJalatwgLsqyNyobhbUy3%2FTfEpJgoeEBgTp5Lg4CAAA%3D&config=H4sIAAAAAAAAA1WPSQ6DMAxF95wi8pptu%2BgdOIQVDArKpDiVGiHu3pCBll38%2F7Ofsg9CwMYSXmLPzzx4DEzhmnPCyUb85ARIGmQZlI8w9nbjs1pQM5XoqA1EDCvFssWPhoN2jqnjLTPKqiX9C6UzPhDzHTxRtKumu25oSjBufpeyfSQmT1X%2FhB%2FUZddhUDz1zXr2%2BAIDhbm2FQEAAA%3D%3D

SWC Info output

No response

Expected behavior

SWC's ES5 output should print this:

{ first: 'value1', third: 'default', rest: [ 'value4', 'value5', 'value6', 'value7' ] }

Actual behavior

But, it prints: { first: 'value1', third: 'value4', rest: [ 'value5', 'value6', 'value7' ] }

Version

1.13.5

Additional context

No response