fix each block destructuring can't have default values refered to destructured params

pull/5230/head
M. Habib Rosyad 5 years ago
parent a0ed7040f1
commit 15650dd7e1
No known key found for this signature in database
GPG Key ID: D5C899B22EB5EA25

@ -25,7 +25,15 @@ export function unpack_destructuring(contexts: Context[], node: Node, modifier:
if (element && element.type === 'RestElement') {
unpack_destructuring(contexts, element, node => x`${modifier(node)}.slice(${i})` as Node);
} else if (element && element.type === 'AssignmentPattern') {
unpack_destructuring(contexts, element.left, node => x`${modifier(node)}[${i}] !== undefined ? ${modifier(node)}[${i}] : ${element.right}` as Node);
const n = contexts.length;
unpack_destructuring(contexts, element.left, node => {
const alternate = JSON.parse(JSON.stringify(element.right));
update_reference(contexts.slice(0, n), node, alternate);
return x`${modifier(node)}[${i}] !== undefined ? ${modifier(node)}[${i}] : ${alternate.replacement || alternate}` as Node
});
} else {
unpack_destructuring(contexts, element, node => x`${modifier(node)}[${i}]` as Node);
}
@ -44,9 +52,17 @@ export function unpack_destructuring(contexts: Context[], node: Node, modifier:
const key = property.key as Identifier;
const value = property.value;
used_properties.push(x`"${(key as Identifier).name}"`);
used_properties.push(x`"${key.name}"`);
if (value.type === 'AssignmentPattern') {
unpack_destructuring(contexts, value.left, node => x`${modifier(node)}.${key.name} !== undefined ? ${modifier(node)}.${key.name} : ${value.right}` as Node);
const n = contexts.length;
unpack_destructuring(contexts, value.left, node => {
const alternate = JSON.parse(JSON.stringify(value.right));
update_reference(contexts.slice(0, n), node, alternate);
return x`${modifier(node)}.${key.name} !== undefined ? ${modifier(node)}.${key.name} : ${alternate.replacement || alternate}` as Node
});
} else {
unpack_destructuring(contexts, value, node => x`${modifier(node)}.${key.name}` as Node);
}
@ -54,3 +70,37 @@ export function unpack_destructuring(contexts: Context[], node: Node, modifier:
});
}
}
function update_reference(contexts: Context[], node: Node, parent: any, property?: any) {
const current_node = (property !== undefined && parent[property] || parent);
if (!current_node || !contexts.length) return;
if (current_node.type === 'Identifier') {
contexts.forEach((context) => {
const { key, modifier } = context;
if (current_node.name === key.name) {
const replacement = modifier(node);
if (property === undefined) {
current_node.replacement = replacement;
} else {
parent[property] = replacement;
}
}
});
} else if (current_node.type === 'BinaryExpression') {
update_reference(contexts, node, current_node, 'left');
update_reference(contexts, node, current_node, 'right');
} else if (current_node.type === 'CallExpression') {
for (let i = 0; i < current_node.arguments.length; i += 1) {
update_reference(contexts, node, current_node.arguments, i);
}
update_reference(contexts, node, current_node, 'callee');
} else if (current_node.type === 'MemberExpression') {
update_reference(contexts, node, current_node, 'object');
update_reference(contexts, node, current_node, 'property');
}
}

@ -7,16 +7,14 @@ export default {
},
html: `
<p class="mammal">raccoon - P. lotor - 25kg</p>
<p class="bird">eagle - unknown - 5.4kg</p>
<p class="mammal">raccoon - P. lotor - 25kg (55 lb)</p>
<p class="bird">eagle - unknown - 5.4kg (12 lb)</p>
`,
test({ assert, component, target }) {
component.animalEntries = [{ animal: 'cow', class: 'mammal', species: 'B. taurus' }];
assert.htmlEqual(target.innerHTML, `
<p class="mammal">cow - B. taurus - 50kg</p>
<p class="mammal">cow - B. taurus - 50kg (110 lb)</p>
`);
},
};

@ -2,6 +2,6 @@
export let animalEntries;
</script>
{#each animalEntries as { animal, species = 'unknown', kilogram: weight = 50 , ...props } }
<p {...props}>{animal} - {species} - {weight}kg</p>
{#each animalEntries as { animal, species = 'unknown', kilogram: weight = 50, pound = (weight * 2.2).toFixed(0), ...props } }
<p {...props}>{animal} - {species} - {weight}kg ({pound} lb)</p>
{/each}

Loading…
Cancel
Save