handle member expressions in destructuring assignments - fixes #3092

pull/3093/head
Rich Harris 5 years ago
parent 21987c4e57
commit 09eb8330a3

@ -2,6 +2,7 @@ import { walk } from 'estree-walker';
import is_reference from 'is-reference'; import is_reference from 'is-reference';
import { Node } from '../../interfaces'; import { Node } from '../../interfaces';
import { Node as ESTreeNode } from 'estree'; import { Node as ESTreeNode } from 'estree';
import get_object from './get_object';
export function create_scopes(expression: Node) { export function create_scopes(expression: Node) {
const map = new WeakMap(); const map = new WeakMap();
@ -114,6 +115,10 @@ const extractors = {
nodes.push(param); nodes.push(param);
}, },
MemberExpression(nodes: Node[], param: Node) {
nodes.push(get_object(param));
},
ObjectPattern(nodes: Node[], param: Node) { ObjectPattern(nodes: Node[], param: Node) {
param.properties.forEach((prop: Node) => { param.properties.forEach((prop: Node) => {
if (prop.type === 'RestElement') { if (prop.type === 'RestElement') {

@ -0,0 +1,23 @@
export default {
html: `
<ul>
<li>Gruyere</li>
<li>Compté</li>
<li>Beaufort</li>
<li>Abondance</li>
</ul>
`,
async test({ assert, component, target }) {
await component.swap(0, 1);
assert.htmlEqual(target.innerHTML, `
<ul>
<li>Compté</li>
<li>Gruyere</li>
<li>Beaufort</li>
<li>Abondance</li>
</ul>
`);
}
};

@ -0,0 +1,18 @@
<script>
let cheese = [
'Gruyere',
'Compté',
'Beaufort',
'Abondance',
];
export function swap(a, b) {
[cheese[a], cheese[b]] = [cheese[b], cheese[a]];
}
</script>
<ul>
{#each cheese as cheese}
<li>{cheese}</li>
{/each}
</ul>
Loading…
Cancel
Save