Repository: sveltejs/prettier-plugin-svelte
Pull request #77 (2020-01-30 14:06:43)

Support await with only pending block
#47 was a step in the right direction but an `await` without a `catch` or `then` block, only a pending block are also valid. This PR adds support for that as well as restructures the code to be more DRY.

Edit: This change would also fix #76.


Patch:
diff --git a/src/print/index.ts b/src/print/index.ts
index bb4f4d5..e6b5ac0 100644
--- a/src/print/index.ts
+++ b/src/print/index.ts
@@ -16,7 +16,7 @@ const {
     hardline,
     fill,
     breakParent,
-    literalline
+    literalline,
 } = doc.builders;
 
 export type PrintFn = (path: FastPath) => Doc;
@@ -313,56 +313,13 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D
         }
         case 'AwaitBlock': {
             const hasPendingBlock = node.pending.children.some((n) => !isEmptyNode(n));
+            const hasThenBlock = node.then.children.some((n) => !isEmptyNode(n));
             const hasCatchBlock = node.catch.children.some((n) => !isEmptyNode(n));
 
-            if (hasPendingBlock && hasCatchBlock) {
-                return group(
-                    concat([
-                        group(concat(['{#await ', printJS(path, print, 'expression'), '}'])),
-                        indent(path.call(print, 'pending')),
-                        group(concat(['{:then', expandNode(node.value), '}'])),
-                        indent(path.call(print, 'then')),
-                        group(concat(['{:catch', expandNode(node.error), '}'])),
-                        indent(path.call(print, 'catch')),
-                        '{/await}',
-                    ]),
-                );
-            }
+            let block = [];
 
-            if (hasCatchBlock) {
-                return group(
-                    concat([
-                        group(
-                            concat([
-                                '{#await ',
-                                printJS(path, print, 'expression'),
-                                ' then',
-                                expandNode(node.value),
-                                '}',
-                            ]),
-                        ),
-                        indent(path.call(print, 'then')),
-                        group(concat(['{:catch', expandNode(node.error), '}'])),
-                        indent(path.call(print, 'catch')),
-                        '{/await}',
-                    ]),
-                );
-            }
-
-            if (hasPendingBlock) {
-                return group(
-                    concat([
-                        group(concat(['{#await ', printJS(path, print, 'expression'), '}'])),
-                        indent(path.call(print, 'pending')),
-                        group(concat(['{:then', expandNode(node.value), '}'])),
-                        indent(path.call(print, 'then')),
-                        '{/await}',
-                    ]),
-                );
-            }
-
-            return group(
-                concat([
+            if (!hasPendingBlock && hasThenBlock) {
+                block.push(
                     group(
                         concat([
                             '{#await ',
@@ -373,9 +330,32 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D
                         ]),
                     ),
                     indent(path.call(print, 'then')),
-                    '{/await}',
-                ]),
-            );
+                );
+            } else {
+                block.push(group(concat(['{#await ', printJS(path, print, 'expression'), '}'])));
+
+                if (hasPendingBlock) {
+                    block.push(indent(path.call(print, 'pending')));
+                }
+
+                if (hasThenBlock) {
+                    block.push(
+                        group(concat(['{:then', expandNode(node.value), '}'])),
+                        indent(path.call(print, 'then')),
+                    );
+                }
+            }
+
+            if (hasCatchBlock) {
+                block.push(
+                    group(concat(['{:catch', expandNode(node.error), '}'])),
+                    indent(path.call(print, 'catch')),
+                );
+            }
+
+            block.push('{/await}');
+
+            return group(concat(block));
         }
         case 'ThenBlock':
         case 'PendingBlock':
@@ -434,7 +414,7 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D
             return concat([line, 'ref:', node.name]);
         case 'Comment': {
             let text = node.data;
-            ignoreNext = text.trim() === 'prettier-ignore'
+            ignoreNext = text.trim() === 'prettier-ignore';
             if (hasSnippedContent(text)) {
                 text = unsnipContent(text);
             }


Test patch:
diff --git a/test/printer/samples/await-block-with-only-pending.html b/test/printer/samples/await-block-with-only-pending.html
new file mode 100644
index 0000000..8cf74b5
--- /dev/null
+++ b/test/printer/samples/await-block-with-only-pending.html
@@ -0,0 +1,3 @@
+{#await thePromise}
+    <p>loading...</p>
+{/await}