Which behavior is correct (so where should I fill bug report)?
It looks like Babel is taking some invalid shortcuts here. The
DestructuringAssignmentEvaluation runtime semantics[1] require the same
source object (the parameter value) to be used throughout the
destructuring operation. Babel compiles the first destructuring into
var bar = foo.bar; var qaz = foo.qaz;
So foo gets looked up twice, with the second lookup resulting in a
different value, the one that the bar getter assigned to foo. In
SpiderMonkey, both getters get called (you can check this with console.log
calls in the getters) because the destructuring operates on the same value
throughout.
Curiously, the inline destructuring in the console.log call at the end
fares better here: it assigns foo to a temporary variable _foo, so
changing the value of foo doesn't influence results. (I do wonder if that
causes foo to be leaked forever, though: _foo is a global variable
defined at the script's top.)
Following code:
var foo = { get bar() { foo = { qaz: 'bar-r' }; return 'bar-c'; }, get qaz() { foo = { bar: 'qaz-r' } return 'qaz-c'; } };
let {bar, qaz} = foo; console.log(({bar, qaz} = foo));
Firefox: Object { bar: "qaz-r" } Babel: Object { qaz: "bar-r" }
Following code: var foo = { get bar() { foo = { qaz: 'bar-r' }; return 'bar-c'; }, get qaz() { foo = { bar: 'qaz-r' } return 'qaz-c'; } }; let {bar, qaz} = foo; console.log(({bar, qaz} = foo)); Firefox: Object { bar: "qaz-r" } Babel: Object { qaz: "bar-r" }