Skip to content

ES5 object destructuring assignment in conditional miscompiled returns wrong property value #11051

@Yeaseen

Description

@Yeaseen

Describe the bug

When using object destructuring assignment inside an if condition, SWC’s ES5 output evaluates the property incorrectly. In Node.js the destructuring works and isEven is true, but SWC transforms it in a way that makes the check fail.

Input code

let myObject = {value: 5, isEven: true};

function checkIfEven() {
  if (({isEven} = myObject).isEven) {
    return "Number is even";
  } 
  else {
    return "Number is odd";
  }
}

function func1() {
  return checkIfEven();
}

function main() {
  let res = func1();
  console.log(res);
}

main();

Config

Link to the code that reproduces this issue

https://play.swc.rs/?version=1.13.5&code=H4sIAAAAAAAAA3WPuwqDMBSG9zzFj1OEInToonTs0KV9Bo3H1jYmkItQJO%2FehCjUocshnP9yvjBJDtPn3r1IOJyxzK30VON0wGgvM6kazngKDWODV8KNWkE8SbyvQ1J5iYUB4wDOlxwIsWUrLKu8yy7AkPNGobj5qSMTL4CiWjRRDIiDpKW%2FVt332cnCD0x6HFeMNbPja3buqR035vRxQzbSrhWpW2hltaRK6gePYk7nUPMFQCnKfisBAAA%3D&config=H4sIAAAAAAAAA1WPSQ6DMAxF95wi8pptu%2BgdOIQVDArKpDiVGiHu3pCBll38%2F7Ofsg9CwMYSXmLPzzx4DEzhmnPCyUb85ARIGmQZlI8w9nbjs1pQM5XoqA1EDCvFssWPhoN2jqnjLTPKqiX9C6UzPhDzHTxRtKumu25oSjBufpeyfSQmT1X%2FhB%2FUZddhUDz1zXr2%2BAIDhbm2FQEAAA%3D%3D

SWC Info output

No response

Expected behavior

SWC's ES5 should print: Number is even

Actual behavior

But it prints: Number is odd

Version

1.13.5

Additional context

I think the if condition adds an extra unnecessary check here: if ((isEven = myObject.isEven).isEven), making it (isEven = myObject.isEven) evaluates to a boolean (true), and true.isEven is undefined --> falsy --> “Number is odd”

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions