Repository: sveltejs/prettier-plugin-svelte (ts)

Issue:
No formatting inside #each destructuring
Code inside `#each` destructuring doesn't appear to be formatted. Using

```json
"prettier": "^2.5.0",
"prettier-plugin-svelte": "^2.5.0",
"svelte": "^3.44.2",
```

**Expected after formatting**

```svelte
<script>
  let arr = [{ a: 1 }]
</script>

{#each arr as { a, b = '' }}
  {a}
  {b}
{/each}
```

**Actual**

```svelte
<script>
  let arr = [{a:1}]
</script>

{#each arr as {a,b =''}}
  {a}
  {b}
{/each}
```

Pull request:
(fix) destructuring with default values
- use expandNode for each context
- support array and object expressions in expandNode

Fixes #265

Patch:
diff --git a/src/print/index.ts b/src/print/index.ts
index 6fcb09d..fd852b9 100644
--- a/src/print/index.ts
+++ b/src/print/index.ts
@@ -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) {
@@ -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 '';
     }
@@ -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':
@@ -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);


Test patch:
diff --git a/test/formatting/samples/each-await-block-destructuring/input.html b/test/formatting/samples/each-await-block-destructuring/input.html
new file mode 100644
index 0000000..e98ab9c
--- /dev/null
+++ b/test/formatting/samples/each-await-block-destructuring/input.html
@@ -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}
diff --git a/test/formatting/samples/each-await-block-destructuring/output.html b/test/formatting/samples/each-await-block-destructuring/output.html
new file mode 100644
index 0000000..08be886
--- /dev/null
+++ b/test/formatting/samples/each-await-block-destructuring/output.html
@@ -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}
diff --git a/test/printer/samples/each-block-destructured-default-value.html b/test/printer/samples/each-block-destructured-default-value.html
index 81b2a42..087d1cb 100644
--- a/test/printer/samples/each-block-destructured-default-value.html
+++ b/test/printer/samples/each-block-destructured-default-value.html
@@ -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}