From 4d1e71fdd5aea2f2af271b5dcae284573754a12f Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 29 Apr 2018 17:08:24 -0400 Subject: [PATCH] sparse array patterns --- src/parse/read/context.ts | 9 +++++++-- src/utils/unpackDestructuring.ts | 2 ++ .../_config.js | 20 +++++++++++++++++++ .../main.html | 3 +++ 4 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 test/runtime/samples/each-block-destructured-array-sparse/_config.js create mode 100644 test/runtime/samples/each-block-destructured-array-sparse/main.html diff --git a/src/parse/read/context.ts b/src/parse/read/context.ts index feb31d150e..3ab18695bd 100644 --- a/src/parse/read/context.ts +++ b/src/parse/read/context.ts @@ -46,8 +46,13 @@ export default function readContext(parser: Parser) { do { parser.allowWhitespace(); - context.elements.push(readContext(parser)); - parser.allowWhitespace(); + + if (parser.template[parser.index] === ',') { + context.elements.push(null); + } else { + context.elements.push(readContext(parser)); + parser.allowWhitespace(); + } } while (parser.eat(',')); errorOnAssignmentPattern(parser); diff --git a/src/utils/unpackDestructuring.ts b/src/utils/unpackDestructuring.ts index 2c48d5c762..749d26b34b 100644 --- a/src/utils/unpackDestructuring.ts +++ b/src/utils/unpackDestructuring.ts @@ -3,6 +3,8 @@ export default function unpackDestructuring( node: Node, tail: string ) { + if (!node) return; + if (node.type === 'Identifier') { contexts.push({ key: node, diff --git a/test/runtime/samples/each-block-destructured-array-sparse/_config.js b/test/runtime/samples/each-block-destructured-array-sparse/_config.js new file mode 100644 index 0000000000..f504cfe377 --- /dev/null +++ b/test/runtime/samples/each-block-destructured-array-sparse/_config.js @@ -0,0 +1,20 @@ +export default { + data: { + animalPawsEntries: [ + ['raccoon', 'hands'], + ['eagle', 'wings'] + ] + }, + + html: ` +

hands

+

wings

+ `, + + test ( assert, component, target ) { + component.set({ animalPawsEntries: [['foo', 'bar']] }); + assert.htmlEqual( target.innerHTML, ` +

bar

+ `); + }, +}; diff --git a/test/runtime/samples/each-block-destructured-array-sparse/main.html b/test/runtime/samples/each-block-destructured-array-sparse/main.html new file mode 100644 index 0000000000..7ca8e403d8 --- /dev/null +++ b/test/runtime/samples/each-block-destructured-array-sparse/main.html @@ -0,0 +1,3 @@ +{#each animalPawsEntries as [, pawType]} +

{pawType}

+{/each}