Skip to content

Commit

Permalink
(fix) destructuring with default values
Browse files Browse the repository at this point in the history
- use expandNode for each context
- support array and object expressions in expandNode
sveltejs#265
  • Loading branch information
Simon Holthausen committed Dec 3, 2021
1 parent 3ea98f0 commit 29c7c8b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
14 changes: 10 additions & 4 deletions src/print/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,8 +453,8 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D
const def: Doc[] = [
'{#each ',
printSvelteBlockJS(path, print, 'expression'),
' as ',
printSvelteBlockJS(path, print, 'context'),
' as',
expandNode(node.context),
];

if (node.index) {
Expand Down Expand Up @@ -1044,7 +1044,7 @@ function printJS(
return path.call(print, name);
}

function expandNode(node: any): string {
function expandNode(node: any, parent?: any): string {
if (node === null) {
return '';
}
Expand All @@ -1055,6 +1055,7 @@ function expandNode(node: any): string {
}

switch (node.type) {
case 'ArrayExpression':
case 'ArrayPattern':
return ' [' + node.elements.map(expandNode).join(',').slice(1) + ']';
case 'AssignmentPattern':
Expand All @@ -1063,12 +1064,17 @@ function expandNode(node: any): string {
return ' ' + node.name;
case 'Literal':
return ' ' + node.raw;
case 'ObjectExpression':
return ' {' + node.properties.map((p: any) => expandNode(p, node)).join(',') + ' }';
case 'ObjectPattern':
return ' {' + node.properties.map(expandNode).join(',') + ' }';
case 'Property':
if (node.value.type === 'ObjectPattern' || node.value.type === 'ArrayPattern') {
return ' ' + node.key.name + ':' + expandNode(node.value);
} else if (node.value.type === 'Identifier' && node.key.name !== node.value.name) {
} else if (
(node.value.type === 'Identifier' && node.key.name !== node.value.name) ||
(parent && parent.type === 'ObjectExpression')
) {
return expandNode(node.key) + ':' + expandNode(node.value);
} else {
return expandNode(node.value);
Expand Down
16 changes: 16 additions & 0 deletions test/formatting/samples/each-await-block-destructuring/input.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{#each arr as { a,b =''}}
{a}
{b}
{/each}

{#await promise then { a,b =''}}
{a}
{b}
{/await}

{#await promise}
Loading
{:then { a,b =''}}
{a}
{b}
{/await}
16 changes: 16 additions & 0 deletions test/formatting/samples/each-await-block-destructuring/output.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{#each arr as { a, b = '' }}
{a}
{b}
{/each}

{#await promise then { a, b = '' }}
{a}
{b}
{/await}

{#await promise}
Loading
{:then { a, b = '' }}
{a}
{b}
{/await}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{#each animals as {key, value = []}}
{#each animals as { key, value = 1, arrayExpr = [{ a: true }], objectExpr = { a: true, b: [1] } }}
<p>{key}: {value}</p>
{/each}

0 comments on commit 29c7c8b

Please sign in to comment.