complex destructuring defaults generate incorrect code
Expected result:
$ echo 'var c = "foo", [{ p = {[c]: 1} }] = [ {} ]; console.log(p);' | node
{ foo: 1 }
buble@0.15.1 generates incorrect code:
$ echo 'var c = "foo", [{ p = {[c]: 1} }] = [ {} ]; console.log(p);' | bin/buble
var c = "foo";
var ref = [ {} ];
var p = ref[0].p; if ( p === void 0 ) p = {};;
p[c] = 1 console.log(p);
$ echo 'var c = "foo", [{ p = {[c]: 1} }] = [ {} ]; console.log(p);' | bin/buble | node
[stdin]:4
p[c] = 1 console.log(p);
^^^^^^^
SyntaxError: Unexpected identifier
The if
should have surrounded its body with curly braces:
var p = ref[0].p; if ( p === void 0 ) { p = {}; p[c] = 1; }
console.log(p);
A workaround for this bug in some cases is to wrap parens around the destructuring default:
$ echo 'var c = "foo", [{ p = ({[c]: 1}) }] = [ {} ]; console.log(p);' | bin/buble
var c = "foo";
var ref = [ {} ];
var p = ref[0].p; if ( p === void 0 ) p = (( obj = {}, obj[c] = 1, obj ));
var obj; console.log(p);
$ echo 'var c = "foo", [{ p = ({[c]: 1}) }] = [ {} ]; console.log(p);' | bin/buble | node
{ foo: 1 }
Some existing buble tests have incorrect generated code:
This was a known bug at the time of writing those tests but I forgot to wrap the defaults in parens.