Skip to content

Default values in async function array destructuring miscompiled in ES5 target #11048

@Yeaseen

Description

@Yeaseen

Describe the bug

When using array destructuring with skipped elements and default values inside an async function, SWC’s ES5 output shifts elements instead of applying the default. This causes wrong bindings for subsequent variables. Node.js and Babel handle this correctly.

Input code

async function fetchResource() {
  let data = ["John", , 25, "Developer", "California"]; 
  await new Promise(resolve => setTimeout(resolve, 500));
  return data;
}

function func1() {
  return new Promise(async (resolve, reject) => {
    let resource = await fetchResource();
    const [firstName, middleName = "N/A", age, ...rest] = resource;
    
    const result = (function() {
      if(firstName) {
        return {firstName, middleName, age, occupation: rest[0], location: rest[1]};
      } else {
        return {error: "Failed to fetch resource"};
      }
    })();
    
    resolve(result);
  });
}

async function main() {
  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=H4sIAAAAAAAAA22SPWvDMBCGd%2F%2BKQ5MNxnUKWWJSKC0dOoRSugUPQj4nKrIU5HNCCf7vlSx%2FpCVeJO7jee89mbc%2FWkDdaUHSaKiRxPETW9NZgXEC1whAIUHFicMW9uzdHDVLIYXHdQrsFc%2BozAmtC7EXrmRtrJaclQW4Rn7hkkDjBT6saWSLsXVkdUbYPkGL9CUbNB1N0RTWeZ4kheu0SJ3Vg2oR9VG0zOcuq3GuseiWzwc3C9DiNwpKvJ7vCF7saM%2F5CRP%2BM10MlcLolmBfS9vSjjcO1siqUujvrpPtHp6daX5wiSzLHJNKF57YgXEDcolOkauIJy%2BjC%2F%2FJOp51lujs8Hp3iFHcCNGduAduvAjt8zIFZcRtaFX2xQjtAVWLdzTQWmM3wN64VFgBmbCW2RFbEMPZJ9OmooAZVh4Hn0OmT4a343%2F%2FsIbLyfq8meUlwvMWY9IozJQ5eGhghebiF4o7q3a1AgAA&config=H4sIAAAAAAAAA1WPSQ6DMAxF95wi8pptu%2BgdOIQVDArKpDiVGiHu3pCBll38%2F7Ofsg9CwMYSXmLPzzx4DEzhmnPCyUb85ARIGmQZlI8w9nbjs1pQM5XoqA1EDCvFssWPhoN2jqnjLTPKqiX9C6UzPhDzHTxRtKumu25oSjBufpeyfSQmT1X%2FhB%2FUZddhUDz1zXr2%2BAIDhbm2FQEAAA%3D%3D

SWC Info output

No response

Expected behavior

SWC's ES5 output should print:

{
  firstName: 'John',
  middleName: 'N/A',
  age: 25,
  occupation: 'Developer',
  location: 'California'
}

Actual behavior

But, it actually prints:

{
  firstName: 'John',
  middleName: 25,
  age: 'Developer',
  occupation: 'California',
  location: undefined
}

Version

1.13.5

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions