Repository: prettier/prettier (JavaScript)
Date: 2021-08-21 07:51:20+00:00
Task type: Bug Fix

Issue:
--prose-wrap=preserve should preserve line breaks in Markdown inline code spans
**Prettier 2.3.2**
[Playground link](https://prettier.io/playground/#N4Igxg9gdgLgprEAuEAZCAnOBbABASwAcBnAVzwBMIAbTXY-GXAQ2zhgBpdIpi4x4MUhhYUi+YmHxQA5rjjVGXPhVxV5+UsWwRVAA3jZCmADpRpUsRVKw9uUk2rMARpjjymVWllzZmMqGYWRQBHUmYAOlwAVSYEfDxmVWxpBNwAN3jWLj0wiTMoCGIYDFIKOzgADzgMKRhmGHxoe2onbEhcJ1cMCVxzBnsmZlDSInlK+SDIbB11PR4+MIaIswARUeI7Zgd3fFKfLzppXCxCLAALBAoaxgIoDJpSQnr4DIVbuGI+bnxW8jUaG5cHo4KQzAAzUgyfANOxQUitIKEZg9BrCKIAUUqYDgz1BIgYsFwEDAYGY-Aa3Ce+AoDUphXuZwgNIQnHoNiY0jMYARyNweWJ4PB+CkQWufFKRJ0rVuzHMeBp8mKnRcmHIERAHBAEGeTV4yFAKIwEAA7gAFFEIYjIEDDE3MACe1q1zgwzDAAGt2ABlZFSWTIEqkOBanDOOAUa4UVBymThGRwABimD8MEaAZQ2xgEE1IHOMGw1AA6udGJ8-XBvVbGPh0owHTawF9c9IJTAzW6ZH5kODhnwtQArYiVABCbs9PtYcFQ0jgPb7IZAQ8q3ukMmocAAiqQIPB59R+yBkRgJTa-BgPVQTVBc2dpDAizSYOdkAAOAAMWqZfCLbsINrOT4akyXMwl3OAOx1a1M2IABaKA4AjCNcywPIsA7fxuyQXsD0XPgUkDUo8LXDdt3A-dD3qZxHwoZ9kAAJi1EpmF+NcAGEIBmZgbU+ABWXMtDgAAVFxoJww90mDABJKBrlgb0wB6Z4AEFZO9GAHQ3Ci4AAXx0oA)
<!-- prettier-ignore -->
```sh
--parser markdown
```

**Input:**
<!-- prettier-ignore -->
```markdown
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod `tempor
incididunt` ut labore et dolore magna aliqua. Ut enim ad minim veniam, `quis
nostrud` exercitation ullamco laboris nisi ut aliquip ex ea commodo `consequat.
Duis` aute irure dolor in reprehenderit in voluptate velit esse cillum dolore `eu
fugiat` nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum.
```

**Output:**
<!-- prettier-ignore -->
```markdown
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod `tempor incididunt` ut labore et dolore magna aliqua. Ut enim ad minim veniam, `quis nostrud` exercitation ullamco laboris nisi ut aliquip ex ea commodo `consequat. Duis` aute irure dolor in reprehenderit in voluptate velit esse cillum dolore `eu fugiat` nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum.

```

**Expected behavior:**

This input should not be changed. Even though the line breaks are not significant to the output, their removal results in extremely long source lines that make editing unpleasant.

One can of course manually rewrap paragraphs to avoid line breaks inside code spans, but that means fighting against the default behavior of the automatic rewrap function in most editors.

This isn’t an issue when Prettier is given charge of wrapping (`--prose-wrap=always` or `--prose-wrap=never`), but with the default `--prose-wrap=preserve`, Prettier should preserve the existing line breaks inside inline code spans.


Issue discussion:

Solution patch:
diff --git a/changelog_unreleased/markdown/11373.md b/changelog_unreleased/markdown/11373.md
new file mode 100644
index 000000000000..8eb18b15ed75
--- /dev/null
+++ b/changelog_unreleased/markdown/11373.md
@@ -0,0 +1,15 @@
+#### Preserve inline code line breaks if `--prose-wrap=preserve` (#11373 by @andersk)
+
+<!-- prettier-ignore -->
+```markdown
+<!-- Input -->
+Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod `tempor
+incididunt` ut labore et dolore magna aliqua.
+
+<!-- Prettier stable -->
+Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod `tempor incididunt` ut labore et dolore magna aliqua.
+
+<!-- Prettier main -->
+Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod `tempor
+incididunt` ut labore et dolore magna aliqua.
+```
diff --git a/src/language-markdown/print-preprocess.js b/src/language-markdown/print-preprocess.js
index eb177afcb242..57c366c022b4 100644
--- a/src/language-markdown/print-preprocess.js
+++ b/src/language-markdown/print-preprocess.js
@@ -9,7 +9,7 @@ const isSingleCharRegex = /^.$/su;
 function preprocess(ast, options) {
   ast = restoreUnescapedCharacter(ast, options);
   ast = mergeContinuousTexts(ast);
-  ast = transformInlineCode(ast);
+  ast = transformInlineCode(ast, options);
   ast = transformIndentedCodeblockAndMarkItsParentList(ast, options);
   ast = markAlignedList(ast, options);
   ast = splitTextIntoSentences(ast, options);
@@ -28,9 +28,9 @@ function transformImportExport(ast) {
   });
 }
 
-function transformInlineCode(ast) {
+function transformInlineCode(ast, options) {
   return mapAst(ast, (node) => {
-    if (node.type !== "inlineCode") {
+    if (node.type !== "inlineCode" || options.proseWrap === "preserve") {
       return node;
     }
 


Test patch:
diff --git a/tests/format/markdown/prose-wrap-preserve/__snapshots__/jsfmt.spec.js.snap b/tests/format/markdown/prose-wrap-preserve/__snapshots__/jsfmt.spec.js.snap
new file mode 100644
index 000000000000..3e177ccb63b7
--- /dev/null
+++ b/tests/format/markdown/prose-wrap-preserve/__snapshots__/jsfmt.spec.js.snap
@@ -0,0 +1,26 @@
+// Jest Snapshot v1, https://goo.gl/fbAQLP
+
+exports[`inline-code-newline.md - {"proseWrap":"preserve"} format 1`] = `
+====================================options=====================================
+parsers: ["markdown"]
+printWidth: 80
+proseWrap: "preserve"
+                                                                                | printWidth
+=====================================input======================================
+Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod \`tempor
+incididunt\` ut labore et dolore magna aliqua. Ut enim ad minim veniam, \`quis
+nostrud\` exercitation ullamco laboris nisi ut aliquip ex ea commodo \`consequat.
+Duis\` aute irure dolor in reprehenderit in voluptate velit esse cillum dolore \`eu
+fugiat\` nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
+culpa qui officia deserunt mollit anim id est laborum.
+
+=====================================output=====================================
+Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod \`tempor
+incididunt\` ut labore et dolore magna aliqua. Ut enim ad minim veniam, \`quis
+nostrud\` exercitation ullamco laboris nisi ut aliquip ex ea commodo \`consequat.
+Duis\` aute irure dolor in reprehenderit in voluptate velit esse cillum dolore \`eu
+fugiat\` nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
+culpa qui officia deserunt mollit anim id est laborum.
+
+================================================================================
+`;
diff --git a/tests/format/markdown/prose-wrap-preserve/inline-code-newline.md b/tests/format/markdown/prose-wrap-preserve/inline-code-newline.md
new file mode 100644
index 000000000000..c2b9f6b8cf82
--- /dev/null
+++ b/tests/format/markdown/prose-wrap-preserve/inline-code-newline.md
@@ -0,0 +1,6 @@
+Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod `tempor
+incididunt` ut labore et dolore magna aliqua. Ut enim ad minim veniam, `quis
+nostrud` exercitation ullamco laboris nisi ut aliquip ex ea commodo `consequat.
+Duis` aute irure dolor in reprehenderit in voluptate velit esse cillum dolore `eu
+fugiat` nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
+culpa qui officia deserunt mollit anim id est laborum.
diff --git a/tests/format/markdown/prose-wrap-preserve/jsfmt.spec.js b/tests/format/markdown/prose-wrap-preserve/jsfmt.spec.js
new file mode 100644
index 000000000000..9080881c6d92
--- /dev/null
+++ b/tests/format/markdown/prose-wrap-preserve/jsfmt.spec.js
@@ -0,0 +1,1 @@
+run_spec(__dirname, ["markdown"], { proseWrap: "preserve" });