Repository: prettier/prettier (JavaScript) — Bug Fix

Issue:
Extra empty line added after tables without data rows
<!--

BEFORE SUBMITTING AN ISSUE:

1.  Search for your issue on GitHub: https://github.com/prettier/prettier/issues
    A large number of opened issues are duplicates of existing issues.
    If someone has already opened an issue for what you are experiencing,
    you do not need to open a new issue — please add a 👍 reaction to the
    existing issue instead.

2.  We get a lot of requests for adding options, but Prettier is
    built on the principle of being opinionated about code formatting.
    This means we have a very high bar for adding new options.
    Find out more: https://prettier.io/docs/en/option-philosophy.html

Tip! Don't write this stuff manually.

1. Go to https://prettier.io/playground
2. Paste your code and set options
3. Press the "Report issue" button in the lower right

-->

**Prettier 2.1.2**
[Playground link](https://prettier.io/playground/#N4Igxg9gdgLgprEAuEAVOBnGACANgSyjmwEYAdKCgH2wGUAHOMfAMwE9sYALYjOXJvAAm2CPRj5o2JNhoB5cZKilZ1bAFpNW7Tt171sjXtWUo6LHkLEAbBRAAaEGInQMyUAEMATl4gB3AAVvBDcUD1w-DzY3RwAjLw8wAGs4GAZEwgBzZBgvAFc4RzgAW1i4ISFygBkPKEy8j0y4ADEIL2KPGAk65BAPPJgIBxAuGGLcAHUufHgMekS4WhCZ-AA3GbZesAwYkEI+LxgAhMyO5BZwvkcAKwwADwAhBOTU2g9iuCqrc8vCkFu7rQsgIAIp5CDwH64K4geZeA69DpeJJCfxQYb0LyEGATfBCbjIAAcAAZHJiIHwJgl6L1MZg4F5VnBhgBHcHwY5iUJ9DDqIjlcrDLxwNn4YXHRpnJAXaF-PjFfA5fJy4FwMEQ5nS36OGAeWK4-FcZAAJh1CXwBDqAGEIMUpSBMABWYZ5PioPWhGUw1YFACSUEqsFoYCx4gAggHaDA2AIoXwAL7xoA)

```sh
# Options (if any):
--parser markdown
```

**Input:**

```md
Test line 1

| Specify the selected option : | Option 1 |
| ----------------------------- | -------- |

Test line 6

```

**Output:**

```md
Test line 1

| Specify the selected option : | Option 1 |
| ----------------------------- | -------- |


Test line 6

```

**Expected behavior:**

```md
Test line 1

| Specify the selected option : | Option 1 |
| ----------------------------- | -------- |

Test line 6

```


Discussion:

Resolving patch:
diff --git a/changelog_unreleased/markdown/pr-9654.md b/changelog_unreleased/markdown/pr-9654.md
new file mode 100644
index 000000000000..5028ba8b99fb
--- /dev/null
+++ b/changelog_unreleased/markdown/pr-9654.md
@@ -0,0 +1,29 @@
+#### Fix extra empty line added after empty table (#9654 by @fisker)
+
+<!-- prettier-ignore -->
+```markdown
+<!-- Input -->
+Test line 1
+
+| Specify the selected option : | Option 1 |
+| ----------------------------- | -------- |
+
+Test line 6
+
+<!-- Prettier stable -->
+Test line 1
+
+| Specify the selected option : | Option 1 |
+| ----------------------------- | -------- |
+
+
+Test line 6
+
+<!-- Prettier master -->
+Test line 1
+
+| Specify the selected option : | Option 1 |
+| ----------------------------- | -------- |
+
+Test line 6
+```
diff --git a/src/language-markdown/printer-markdown.js b/src/language-markdown/printer-markdown.js
index e95006c58e01..dad7bbec8bb5 100644
--- a/src/language-markdown/printer-markdown.js
+++ b/src/language-markdown/printer-markdown.js
@@ -595,33 +595,32 @@ function printTable(path, options, print) {
       ),
     contents[0].map(() => 3) // minimum width = 3 (---, :--, :-:, --:)
   );
-  const alignedTable = join(hardlineWithoutBreakParent, [
-    printRow(contents[0]),
-    printSeparator(),
-    join(
-      hardlineWithoutBreakParent,
-      contents.slice(1).map((rowContents) => printRow(rowContents))
-    ),
-  ]);
+  const alignedTable = printTableContents(/* isCompact */ false);
 
   if (options.proseWrap !== "never") {
     return concat([breakParent, alignedTable]);
   }
 
   // Only if the --prose-wrap never is set and it exceeds the print width.
-  const compactTable = join(hardlineWithoutBreakParent, [
-    printRow(contents[0], /* isCompact */ true),
-    printSeparator(/* isCompact */ true),
-    join(
-      hardlineWithoutBreakParent,
-      contents
-        .slice(1)
-        .map((rowContents) => printRow(rowContents, /* isCompact */ true))
-    ),
-  ]);
+  const compactTable = printTableContents(/* isCompact */ true);
 
   return concat([breakParent, group(ifBreak(compactTable, alignedTable))]);
 
+  function printTableContents(isCompact) {
+    const parts = [printRow(contents[0], isCompact), printSeparator(isCompact)];
+    if (contents.length > 1) {
+      parts.push(
+        join(
+          hardlineWithoutBreakParent,
+          contents
+            .slice(1)
+            .map((rowContents) => printRow(rowContents, isCompact))
+        )
+      );
+    }
+    return join(hardlineWithoutBreakParent, parts);
+  }
+
   function printSeparator(isCompact) {
     return concat([
       "| ",


Test patch:
diff --git a/tests/markdown/table/empty-table/__snapshots__/jsfmt.spec.js.snap b/tests/markdown/table/empty-table/__snapshots__/jsfmt.spec.js.snap
new file mode 100644
index 000000000000..747deca3fd2a
--- /dev/null
+++ b/tests/markdown/table/empty-table/__snapshots__/jsfmt.spec.js.snap
@@ -0,0 +1,140 @@
+// Jest Snapshot v1, https://goo.gl/fbAQLP
+
+exports[`empty-table.md - {"proseWrap":"always"} format 1`] = `
+====================================options=====================================
+parsers: ["markdown"]
+printWidth: 80
+proseWrap: "always"
+                                                                                | printWidth
+=====================================input======================================
+Text
+
+| Specify the selected option : | Option 1 |
+|:--| --- |
+
+Text
+
+|    Should print as compact table when --proseWrap=never|a long long long long long long long long long long long long long head|
+|---|--:|
+
+Text
+
+=====================================output=====================================
+Text
+
+| Specify the selected option : | Option 1 |
+| :---------------------------- | -------- |
+
+Text
+
+| Should print as compact table when --proseWrap=never | a long long long long long long long long long long long long long head |
+| ---------------------------------------------------- | ----------------------------------------------------------------------: |
+
+Text
+
+================================================================================
+`;
+
+exports[`empty-table.md - {"proseWrap":"never"} format 1`] = `
+====================================options=====================================
+parsers: ["markdown"]
+printWidth: 80
+proseWrap: "never"
+                                                                                | printWidth
+=====================================input======================================
+Text
+
+| Specify the selected option : | Option 1 |
+|:--| --- |
+
+Text
+
+|    Should print as compact table when --proseWrap=never|a long long long long long long long long long long long long long head|
+|---|--:|
+
+Text
+
+=====================================output=====================================
+Text
+
+| Specify the selected option : | Option 1 |
+| :---------------------------- | -------- |
+
+Text
+
+| Should print as compact table when --proseWrap=never | a long long long long long long long long long long long long long head |
+| --- | --: |
+
+Text
+
+================================================================================
+`;
+
+exports[`empty-table.md - {"proseWrap":"preserve"} format 1`] = `
+====================================options=====================================
+parsers: ["markdown"]
+printWidth: 80
+proseWrap: "preserve"
+                                                                                | printWidth
+=====================================input======================================
+Text
+
+| Specify the selected option : | Option 1 |
+|:--| --- |
+
+Text
+
+|    Should print as compact table when --proseWrap=never|a long long long long long long long long long long long long long head|
+|---|--:|
+
+Text
+
+=====================================output=====================================
+Text
+
+| Specify the selected option : | Option 1 |
+| :---------------------------- | -------- |
+
+Text
+
+| Should print as compact table when --proseWrap=never | a long long long long long long long long long long long long long head |
+| ---------------------------------------------------- | ----------------------------------------------------------------------: |
+
+Text
+
+================================================================================
+`;
+
+exports[`empty-table.md format 1`] = `
+====================================options=====================================
+parsers: ["markdown"]
+printWidth: 80
+                                                                                | printWidth
+=====================================input======================================
+Text
+
+| Specify the selected option : | Option 1 |
+|:--| --- |
+
+Text
+
+|    Should print as compact table when --proseWrap=never|a long long long long long long long long long long long long long head|
+|---|--:|
+
+Text
+
+=====================================output=====================================
+Text
+
+| Specify the selected option : | Option 1 |
+| :---------------------------- | -------- |
+
+Text
+
+| Should print as compact table when --proseWrap=never | a long long long long long long long long long long long long long head |
+| ---------------------------------------------------- | ----------------------------------------------------------------------: |
+
+Text
+
+================================================================================
+`;
diff --git a/tests/markdown/table/empty-table/empty-table.md b/tests/markdown/table/empty-table/empty-table.md
new file mode 100644
index 000000000000..3765cc4048a0
--- /dev/null
+++ b/tests/markdown/table/empty-table/empty-table.md
@@ -0,0 +1,11 @@
+Text
+
+| Specify the selected option : | Option 1 |
+|:--| --- |
+
+Text
+
+|    Should print as compact table when --proseWrap=never|a long long long long long long long long long long long long long head|
+|---|--:|
+
+Text
diff --git a/tests/markdown/table/empty-table/jsfmt.spec.js b/tests/markdown/table/empty-table/jsfmt.spec.js
new file mode 100644
index 000000000000..54dea461f5bd
--- /dev/null
+++ b/tests/markdown/table/empty-table/jsfmt.spec.js
@@ -0,0 +1,4 @@
+run_spec(__dirname, ["markdown"]);
+run_spec(__dirname, ["markdown"], { proseWrap: "never" });
+run_spec(__dirname, ["markdown"], { proseWrap: "always" });
+run_spec(__dirname, ["markdown"], { proseWrap: "preserve" });