Commit 2208dbec by kzc Committed by Marijn Haverbeke

basic destructuring defaults functionality for VariableDeclarator. (#145)

parent 72432f96
......@@ -56,6 +56,9 @@ export default class ObjectExpression extends Node {
} else if ( this.parent.type === 'AssignmentExpression' && this.parent.parent.type === 'ExpressionStatement' && this.parent.left.type === 'Identifier' ) {
isSimpleAssignment = true;
name = this.parent.left.alias || this.parent.left.name; // TODO is this right?
} else if ( this.parent.type === 'AssignmentPattern' && this.parent.left.type === 'Identifier' ) {
isSimpleAssignment = true;
name = this.parent.left.alias || this.parent.left.name; // TODO is this right?
}
// handle block scoping
......
......@@ -29,6 +29,7 @@ export default class TemplateLiteral extends Node {
const parenthesise = ( this.quasis.length !== 1 || this.expressions.length !== 0 ) &&
this.parent.type !== 'AssignmentExpression' &&
this.parent.type !== 'AssignmentPattern' &&
this.parent.type !== 'VariableDeclarator' &&
( this.parent.type !== 'BinaryExpression' || this.parent.operator !== '+' );
......
......@@ -19,6 +19,7 @@ export default class VariableDeclarator extends Node {
}
}
if ( this.id ) this.id.transpile( code, transforms );
if ( this.init ) this.init.transpile( code, transforms );
}
......
......@@ -644,5 +644,54 @@ module.exports = [
var assign;
(assign = [1, 2], x = assign[0], y = assign[1], assign);
}`
}
},
{
description: 'array destructuring default with template string (#145)',
input: 'const [ foo = `${a}` ] = bar;',
output: `var foo = bar[0]; if ( foo === void 0 ) foo = "" + a;`
},
{
description: 'object destructuring default with template string (#145)',
input: 'const { foo = `${a}` } = bar;',
output: `var foo = bar.foo; if ( foo === void 0 ) foo = "" + a;`
},
{
description: 'array destructuring with multiple defaults with hole',
// FIXME: unnecessary parens sometimes needed around defaults to work around buble bugs
input: `
let [ a = \`A${1+2}\`, , c = (x => -x), d = { [1+1]: 2 } ] = [ "ok" ];
`,
output: `
var ref = [ "ok" ];
var a = ref[0]; if ( a === void 0 ) a = "A3";
var c = ref[2]; if ( c === void 0 ) c = (function (x) { return -x; });
var d = ref[3]; if ( d === void 0 ) d = {};;
d[1+1] = 2
`
},
{
description: 'object destructuring with multiple defaults',
// FIXME: unnecessary parens sometimes needed around defaults to work around buble bugs
input: `
let { a = \`A${1+2}\`, c = (x => -x), d = { [1+1]: 2 } } = { b: 3 };
`,
output: `
var ref = { b: 3 };
var a = ref.a; if ( a === void 0 ) a = "A3";
var c = ref.c; if ( c === void 0 ) c = (function (x) { return -x; });
var d = ref.d; if ( d === void 0 ) d = {};;
d[1+1] = 2
`
},
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment