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 target: object assignment with rest evaluates rest before listed properties

Yeaseen opened this issue · comments

Describe the bug

When targeting ES5, SWC lowers object rest destructuring incorrectly for assignment patterns.
It constructs the ...rest object before evaluating the listed properties (here aa), but per spec the listed properties must be resolved first (including getter side effects) before the rest is built. This causes observable divergences with getters or proxies.

Input code

let aa, rest;
const src = {
  get aa() {
    console.log('getter aa (deleting zz)');
    delete this.zz;
    return 1;
  },
  yy: 2,
  zz: 3
};

({ aa, ...rest } = src);
console.log('rest keys:', Object.keys(rest));

Config

Link to the code that reproduces this issue

https://play.swc.rs/?version=1.13.5&code=H4sIAAAAAAAAAz2OQQrDIBBF957i76IgQttdQs%2FQMyR2SNNKBJ0uNHj3qoFuhvn%2FDcxzxJhnjUCRJ2H9HhkxWNxxCGDtVKoegIa9I%2BP8KofKmELFkE9yxNu%2BImc1qKnf9o7Ary2anM8uEH%2FDjktLRdeR0ohrW3IecRNlEkIe3ccY05RQqkn1Uafb%2F3lnH0pxHDQey5ssmxZlA6pe%2FwC1lxL92AAAAA%3D%3D&config=H4sIAAAAAAAAA1WPSQ6DMAxF95wi8pptu%2BgdOIQVDArKpDiVGiHu3pCBll38%2F7Ofsg9CwMYSXmLPzzx4DEzhmnPCyUb85ARIGmQZlI8w9nbjs1pQM5XoqA1EDCvFssWPhoN2jqnjLTPKqiX9C6UzPhDzHTxRtKumu25oSjBufpeyfSQmT1X%2FhB%2FUZddhUDz1zXr2%2BAIDhbm2FQEAAA%3D%3D

SWC Info output

No response

Expected behavior

It should output:

$ node test.js 
getter aa (deleting zz)
rest keys: [ 'yy' ]

Actual behavior

SWC playground produced JS code gives the following:

$ node out.js 
getter aa (deleting zz)
rest keys: [ 'yy', 'zz' ]

Version

1.13.5

Additional context

No response