From d66c5f82daf9d395b89956d5e762a4dc2a054c42 Mon Sep 17 00:00:00 2001 From: Sosuke Suzuki Date: Thu, 8 Aug 2019 21:18:37 +0900 Subject: [PATCH] fix(HTML, Vue): don't break the template element included in a line shorter than print-width (#6284) --- CHANGELOG.unreleased.md | 25 +++++++++++++++++ src/language-html/utils.js | 2 +- .../__snapshots__/jsfmt.spec.js.snap | 28 +++++++++++++++++++ tests/html_whitespace/template.html | 7 +++++ 4 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 tests/html_whitespace/template.html diff --git a/CHANGELOG.unreleased.md b/CHANGELOG.unreleased.md index 6e793295..2b3031f8 100644 --- a/CHANGELOG.unreleased.md +++ b/CHANGELOG.unreleased.md @@ -315,6 +315,30 @@ This version updates the TypeScript parser to correctly handle JSX text with dou Flag used with `--write` to avoid re-checking files that were not changed since they were last written (with the same formatting configuration). +#### HTML, Vue: Don't break the template element included in a line shorter than print-width([#6284] by [@sosukesuzuki]) + +Previously, even if the line length is shorter than print-width is Prettier breaks the line with a template element. + + +```html +// Input + + +// Output (Prettier stable) + + +// Output (Prettier master) + +``` + [#5910]: https://github.com/prettier/prettier/pull/5910 [#6186]: https://github.com/prettier/prettier/pull/6186 [#6206]: https://github.com/prettier/prettier/pull/6206 @@ -325,6 +349,7 @@ Flag used with `--write` to avoid re-checking files that were not changed since [#6270]: https://github.com/prettier/prettier/pull/6270 [#6289]: https://github.com/prettier/prettier/pull/6289 [#6332]: https://github.com/prettier/prettier/pull/6332 +[#6284]: https://github.com/prettier/prettier/pull/6284 [@duailibe]: https://github.com/duailibe [@gavinjoyce]: https://github.com/gavinjoyce [@sosukesuzuki]: https://github.com/sosukesuzuki diff --git a/src/language-html/utils.js b/src/language-html/utils.js index b87c3140..b5417be1 100644 --- a/src/language-html/utils.js +++ b/src/language-html/utils.js @@ -272,7 +272,7 @@ function forceBreakContent(node) { forceBreakChildren(node) || (node.type === "element" && node.children.length !== 0 && - (["body", "template", "script", "style"].indexOf(node.name) !== -1 || + (["body", "script", "style"].indexOf(node.name) !== -1 || node.children.some(child => hasNonTextChild(child)))) || (node.firstChild && node.firstChild === node.lastChild && diff --git a/tests/html_whitespace/__snapshots__/jsfmt.spec.js.snap b/tests/html_whitespace/__snapshots__/jsfmt.spec.js.snap index 4e8a8a53..8d0eeb6c 100644 --- a/tests/html_whitespace/__snapshots__/jsfmt.spec.js.snap +++ b/tests/html_whitespace/__snapshots__/jsfmt.spec.js.snap @@ -332,3 +332,31 @@ printWidth: 80 ================================================================================ `; + +exports[`template.html 1`] = ` +====================================options===================================== +parsers: ["html"] +printWidth: 80 + | printWidth +=====================================input====================================== + + + + +=====================================output===================================== + + + + +================================================================================ +`; diff --git a/tests/html_whitespace/template.html b/tests/html_whitespace/template.html new file mode 100644 index 00000000..c1910c0e --- /dev/null +++ b/tests/html_whitespace/template.html @@ -0,0 +1,7 @@ + + +