Repository: prettier/prettier (JavaScript)
Date: 2017-12-05 12:29:08+00:00
Task type: Bug Fix

Issue:
1.9: Trailing comma not adding in code blocks in markdown
**Prettier 1.9.0**
[Playground link](https://prettier.io/playground/#N4Igxg9gdgLgprEAuEBidACAyhAtnDACzgEMATASygHMAdKegA2YCsBnetvOAMQFcoYGBWgBhEgBsJACnoYMAMwgQANHIwAjEgCc1UeUohbd6rvniEq1AEqkpATwAy0agEEoZABIkK2+3vkzOBhLGgBRCTY4AM1tUgBrADkIAHd6AEoAbiZmehAVEAgAB2FoNmRQHW1UgAUdBHKUEgA3CAoyfJASNhhkBUkogo1tEjB44Kwi0atkGG0+aJAyCDA+gcWqKO0YGpHqXBI1yMX2AA8AIRGxiZJ8Ryo4I8HCvhgi14AmJ8Wp7S3kEAHbTxZYpKCdIraKgwADq7RCyAAHAAGAqQiBRGEjIoAyFwLbNR4FOIARz4vjguxI+0OSH6xwKUVwFFm80WbCsEjgAEU+BB4N8CjASBo4WQEUgPkKRhQJFZRHgDgDJBJOnwogAVEWNelRAC+eqAA)
```sh
--parser markdown
--trailing-comma all
```

**Input:**
````markdown
### Some heading

```js
someFunctionCall(
  foo,
  bar,
  foobar,
  sometehingReallyLongAndHairy,
  somethingElse,
  breakNow,
);
```

````

**Output:**
````markdown
### Some heading

```js
someFunctionCall(
  foo,
  bar,
  foobar,
  sometehingReallyLongAndHairy,
  somethingElse,
  breakNow
);
```

````

**Expected behavior:**

Same as 1.8.2, namely adding trailing comma


Issue discussion:
https://github.com/prettier/prettier/blob/e09359d2428bdac40c30cd6a82c92a627d1e1d05/src/multiparser.js#L32

I think it's using the wrong object here @lydell (should fall back to parent options)

Solution patch:
diff --git a/src/multiparser.js b/src/multiparser.js
index 70a770cbb234..7476186a7261 100644
--- a/src/multiparser.js
+++ b/src/multiparser.js
@@ -26,12 +26,11 @@ function printSubtree(path, print, options) {
 function parseAndPrint(text, partialNextOptions, parentOptions) {
   const nextOptions = Object.assign({}, parentOptions, partialNextOptions, {
     parentParser: parentOptions.parser,
-    trailingComma:
-      partialNextOptions.parser === "json"
-        ? "none"
-        : partialNextOptions.trailingComma,
     originalText: text
   });
+  if (nextOptions.parser === "json") {
+    nextOptions.trailingComma = "none";
+  }
   const ast = require("./parser").parse(text, nextOptions);
   const astComments = ast.comments;
   delete ast.comments;


Test patch:
diff --git a/tests/multiparser_markdown_js/__snapshots__/jsfmt.spec.js.snap b/tests/multiparser_markdown_js/__snapshots__/jsfmt.spec.js.snap
new file mode 100644
index 000000000000..4c94e48eeed8
--- /dev/null
+++ b/tests/multiparser_markdown_js/__snapshots__/jsfmt.spec.js.snap
@@ -0,0 +1,30 @@
+// Jest Snapshot v1, https://goo.gl/fbAQLP
+
+exports[`trailing-comma.md 1`] = `
+### Some heading
+
+\`\`\`js
+someFunctionCall(
+  foo,
+  bar,
+  foobar,
+  sometehingReallyLongAndHairy,
+  somethingElse,
+  breakNow,
+);
+\`\`\`
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+### Some heading
+
+\`\`\`js
+someFunctionCall(
+  foo,
+  bar,
+  foobar,
+  sometehingReallyLongAndHairy,
+  somethingElse,
+  breakNow,
+);
+\`\`\`
+
+`;
diff --git a/tests/multiparser_markdown_js/jsfmt.spec.js b/tests/multiparser_markdown_js/jsfmt.spec.js
new file mode 100644
index 000000000000..6fafe58eacd6
--- /dev/null
+++ b/tests/multiparser_markdown_js/jsfmt.spec.js
@@ -0,0 +1,1 @@
+run_spec(__dirname, ["markdown"], { trailingComma: "all" });
diff --git a/tests/multiparser_markdown_js/trailing-comma.md b/tests/multiparser_markdown_js/trailing-comma.md
new file mode 100644
index 000000000000..8ea8ca7a9b3d
--- /dev/null
+++ b/tests/multiparser_markdown_js/trailing-comma.md
@@ -0,0 +1,12 @@
+### Some heading
+
+```js
+someFunctionCall(
+  foo,
+  bar,
+  foobar,
+  sometehingReallyLongAndHairy,
+  somethingElse,
+  breakNow,
+);
+```