Commit d39b0780 by kzc Committed by Marijn Haverbeke

fix object destructuring with computed properties (#146)

parent 85ab44ec
......@@ -113,7 +113,9 @@ export default class AssignmentExpression extends Node {
else if ( pattern.type === 'ObjectPattern' ) {
const props = pattern.properties;
if ( props.length == 1 ) {
destructure ( props[0].value, `${ref}.${props[0].key.name}`, false );
const prop = props[0];
const value = prop.computed || prop.key.type !== 'Identifier' ? `${ref}[${code.original.substring(prop.key.start, prop.key.end)}]` : `${ref}.${prop.key.name}`;
destructure( prop.value, value, false );
}
else {
if ( !mayDuplicate ) {
......@@ -123,7 +125,8 @@ export default class AssignmentExpression extends Node {
ref = temp;
}
props.forEach( prop => {
destructure( prop.value, `${ref}.${prop.key.name}`, false );
const value = prop.computed || prop.key.type !== 'Identifier' ? `${ref}[${code.original.substring(prop.key.start, prop.key.end)}]` : `${ref}.${prop.key.name}`;
destructure( prop.value, value, false );
} );
}
}
......
......@@ -53,7 +53,7 @@ function destructureObjectPattern ( code, scope, node, ref, inline, statementGen
let c = node.start;
node.properties.forEach( prop => {
let value = prop.key.type === 'Literal' ? `${ref}[${prop.key.raw}]` : `${ref}.${prop.key.name}`;
let value = prop.computed || prop.key.type !== 'Identifier' ? `${ref}[${code.original.substring(prop.key.start, prop.key.end)}]` : `${ref}.${prop.key.name}`;
handleProperty( code, scope, c, prop.value, value, inline, statementGenerators );
c = prop.end;
});
......@@ -122,12 +122,14 @@ function handleProperty ( code, scope, c, node, value, inline, statementGenerato
});
node.properties.forEach( prop => {
handleProperty( code, scope, c, prop.value, `${ref}.${prop.key.name}`, inline, statementGenerators );
const value = prop.computed || prop.key.type !== 'Identifier' ? `${ref}[${code.original.substring(prop.key.start, prop.key.end)}]` : `${ref}.${prop.key.name}`;
handleProperty( code, scope, c, prop.value, value, inline, statementGenerators );
c = prop.end;
});
} else {
const prop = node.properties[0];
handleProperty( code, scope, c, prop.value, `${value}.${prop.key.name}`, inline, statementGenerators );
const value_suffix = prop.computed || prop.key.type !== 'Identifier' ? `[${code.original.substring(prop.key.start, prop.key.end)}]` : `.${prop.key.name}`;
handleProperty( code, scope, c, prop.value, `${value}${value_suffix}`, inline, statementGenerators );
c = prop.end;
}
......
......@@ -333,6 +333,74 @@ module.exports = [
},
{
description: 'destructured object assignment with computed properties',
input: `
let one, two, three, four;
({ [FirstProp]: one, [SecondProp]: two = 'Too', 3: three, Fore: four } = x);
`,
output: `
var one, two, three, four;
var assign;
((assign = x, one = assign[FirstProp], two = assign[SecondProp], two = two === void 0 ? 'Too' : two, three = assign[3], four = assign.Fore));
`
},
{
description: 'destructured object declaration with computed properties',
input: `
var { [FirstProp]: one, [SecondProp]: two = 'Too', 3: three, Fore: four } = x;
`,
output: `
var one = x[FirstProp];
var two = x[SecondProp]; if ( two === void 0 ) two = 'Too';
var three = x[3];
var four = x.Fore;
`
},
{
description: 'destructured object with computed properties in parameters',
input: `
function foo({ [FirstProp]: one, [SecondProp]: two = 'Too', 3: three, Fore: four } = x) {
console.log(one, two, three, four);
}
`,
output: `
function foo(ref) {
if ( ref === void 0 ) ref = x;
var one = ref[FirstProp];
var two = ref[SecondProp]; if ( two === void 0 ) two = 'Too';
var three = ref[3];
var four = ref.Fore;
console.log(one, two, three, four);
}
`
},
{
description: 'deep matching in parameters with computed properties',
input: `
function foo ({ [a]: { [b]: c }, d: { 'e': f, [g]: h }, [i + j]: { [k + l]: m, n: o } }) {
console.log( c, f, h, m, o );
}`,
output: `
function foo (ref) {
var c = ref[a][b];
var ref_d = ref.d;
var f = ref_d['e'];
var h = ref_d[g];
var ref_i_j = ref[i + j];
var m = ref_i_j[k + l];
var o = ref_i_j.n;
console.log( c, f, h, m, o );
}`
},
{
description: 'transpiles destructuring assignment of an array',
input: `
[x, y] = [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 sign in to comment