From 56bd05c62f9f33d70fc9bee27fea386c85f6a7cc Mon Sep 17 00:00:00 2001 From: fisker Date: Tue, 25 Aug 2020 23:29:23 +0800 Subject: [PATCH 1/7] Vue: Fix format on self closing blocks --- src/language-html/printer-html.js | 2 +- .../multiparser/__snapshots__/jsfmt.spec.js.snap | 14 ++++++++++++++ tests/vue/multiparser/void-element.vue | 1 + 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 tests/vue/multiparser/void-element.vue diff --git a/src/language-html/printer-html.js b/src/language-html/printer-html.js index fc8e99a13f06..112b59626f4a 100644 --- a/src/language-html/printer-html.js +++ b/src/language-html/printer-html.js @@ -72,7 +72,7 @@ function embed(path, print, textToDoc, options) { return; } - if (isVueNonHtmlBlock(node, options)) { + if (!node.isSelfClosing && isVueNonHtmlBlock(node, options)) { const parser = inferScriptParser(node, options); if (!parser) { return; diff --git a/tests/vue/multiparser/__snapshots__/jsfmt.spec.js.snap b/tests/vue/multiparser/__snapshots__/jsfmt.spec.js.snap index 4d1047328e9f..15beef5d56c7 100644 --- a/tests/vue/multiparser/__snapshots__/jsfmt.spec.js.snap +++ b/tests/vue/multiparser/__snapshots__/jsfmt.spec.js.snap @@ -164,6 +164,20 @@ printWidth: 80 ================================================================================ `; +exports[`void-element.vue format 1`] = ` +====================================options===================================== +parsers: ["vue"] +printWidth: 80 + | printWidth +=====================================input====================================== + + +=====================================output===================================== + + +================================================================================ +`; + exports[`vue-component.vue format 1`] = ` ====================================options===================================== parsers: ["vue"] diff --git a/tests/vue/multiparser/void-element.vue b/tests/vue/multiparser/void-element.vue new file mode 100644 index 000000000000..5bd25ee7d7be --- /dev/null +++ b/tests/vue/multiparser/void-element.vue @@ -0,0 +1 @@ + From 6556c716da0001c51c4e28344a04279691b1510f Mon Sep 17 00:00:00 2001 From: fisker Date: Tue, 25 Aug 2020 23:33:50 +0800 Subject: [PATCH 2/7] Changelog --- changelog_unreleased/vue/pr-9052.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/changelog_unreleased/vue/pr-9052.md b/changelog_unreleased/vue/pr-9052.md index 06cbc594b4f9..c7b8a6459f2d 100644 --- a/changelog_unreleased/vue/pr-9052.md +++ b/changelog_unreleased/vue/pr-9052.md @@ -1,10 +1,11 @@ -#### Fix format blocks with `src` attribute ([#9052](https://github.com/prettier/prettier/pull/9052) by [@fisker](https://github.com/fisker)) +#### Fix self-closing blocks and blocks with `src` attribute format ([#9052](https://github.com/prettier/prettier/pull/9052), [#9055](https://github.com/prettier/prettier/pull/9055) by [@fisker](https://github.com/fisker)) ```vue + @@ -12,9 +13,13 @@ + + ``` From 7e6d7764dffc81d366c51131085473631ba49b64 Mon Sep 17 00:00:00 2001 From: fisker Date: Tue, 25 Aug 2020 23:49:14 +0800 Subject: [PATCH 3/7] Add tests --- .../__snapshots__/jsfmt.spec.js.snap | 64 +++++++++++++++++++ tests/vue/multiparser/jsfmt.spec.js | 19 +++++- 2 files changed, 82 insertions(+), 1 deletion(-) diff --git a/tests/vue/multiparser/__snapshots__/jsfmt.spec.js.snap b/tests/vue/multiparser/__snapshots__/jsfmt.spec.js.snap index 15beef5d56c7..e082e313c5f2 100644 --- a/tests/vue/multiparser/__snapshots__/jsfmt.spec.js.snap +++ b/tests/vue/multiparser/__snapshots__/jsfmt.spec.js.snap @@ -102,6 +102,70 @@ export default { ================================================================================ `; +exports[`snippet: empty format 1`] = ` +====================================options===================================== +parsers: ["vue"] +printWidth: 80 + | printWidth +=====================================input====================================== + +=====================================output===================================== + + + + +================================================================================ +`; + +exports[`snippet: new line format 1`] = ` +====================================options===================================== +parsers: ["vue"] +printWidth: 80 + | printWidth +=====================================input====================================== + + + +=====================================output===================================== + + + + +================================================================================ +`; + +exports[`snippet: new line format 2`] = ` +====================================options===================================== +parsers: ["vue"] +printWidth: 80 + | printWidth +=====================================input====================================== + +   + +=====================================output===================================== + + + + +================================================================================ +`; + +exports[`snippet: spaces format 1`] = ` +====================================options===================================== +parsers: ["vue"] +printWidth: 80 + | printWidth +=====================================input====================================== + +=====================================output===================================== + + + + +================================================================================ +`; + exports[`template-bind.vue format 1`] = ` ====================================options===================================== parsers: ["vue"] diff --git a/tests/vue/multiparser/jsfmt.spec.js b/tests/vue/multiparser/jsfmt.spec.js index 2fd7eede986d..85705b90f4a2 100644 --- a/tests/vue/multiparser/jsfmt.spec.js +++ b/tests/vue/multiparser/jsfmt.spec.js @@ -1 +1,18 @@ -run_spec(__dirname, ["vue"]); +run_spec({dirname: __dirname, snippets: [ + { + name: "empty", + code: '' + }, + { + name: "spaces", + code: ' ' + }, + { + name: "new line", + code: '\n \n' + }, + { + name: "new line", + code: '\n \u2005 \n' + }, +]}, ["vue"]); From b44f7e09b78498f7427b221bbab5649388d4d3d3 Mon Sep 17 00:00:00 2001 From: fisker Date: Tue, 25 Aug 2020 23:57:35 +0800 Subject: [PATCH 4/7] Fix empty elements --- src/language-html/printer-html.js | 4 +- .../__snapshots__/jsfmt.spec.js.snap | 14 ++----- tests/vue/multiparser/jsfmt.spec.js | 38 +++++++++++-------- 3 files changed, 28 insertions(+), 28 deletions(-) diff --git a/src/language-html/printer-html.js b/src/language-html/printer-html.js index 112b59626f4a..9058c0ecbbf2 100644 --- a/src/language-html/printer-html.js +++ b/src/language-html/printer-html.js @@ -87,9 +87,9 @@ function embed(path, print, textToDoc, options) { return concat([ printOpeningTagPrefix(node, options), group(printOpeningTag(path, options, print)), - hardline, + doc === "" ? "" : hardline, doc, - hardline, + doc === "" ? "" : hardline, printClosingTag(node, options), printClosingTagSuffix(node, options), ]); diff --git a/tests/vue/multiparser/__snapshots__/jsfmt.spec.js.snap b/tests/vue/multiparser/__snapshots__/jsfmt.spec.js.snap index e082e313c5f2..612d6e95db8d 100644 --- a/tests/vue/multiparser/__snapshots__/jsfmt.spec.js.snap +++ b/tests/vue/multiparser/__snapshots__/jsfmt.spec.js.snap @@ -110,9 +110,7 @@ printWidth: 80 =====================================input====================================== =====================================output===================================== - - - + ================================================================================ `; @@ -127,14 +125,12 @@ printWidth: 80 =====================================output===================================== - - - + ================================================================================ `; -exports[`snippet: new line format 2`] = ` +exports[`snippet: non-space format 1`] = ` ====================================options===================================== parsers: ["vue"] printWidth: 80 @@ -159,9 +155,7 @@ printWidth: 80 =====================================input====================================== =====================================output===================================== - - - + ================================================================================ `; diff --git a/tests/vue/multiparser/jsfmt.spec.js b/tests/vue/multiparser/jsfmt.spec.js index 85705b90f4a2..ca3a7b5ccebe 100644 --- a/tests/vue/multiparser/jsfmt.spec.js +++ b/tests/vue/multiparser/jsfmt.spec.js @@ -1,18 +1,24 @@ -run_spec({dirname: __dirname, snippets: [ +run_spec( { - name: "empty", - code: '' + dirname: __dirname, + snippets: [ + { + name: "empty", + code: '', + }, + { + name: "spaces", + code: ' ', + }, + { + name: "new line", + code: '\n \n', + }, + { + name: "non-space", + code: '\n \u2005 \n', + }, + ], }, - { - name: "spaces", - code: ' ' - }, - { - name: "new line", - code: '\n \n' - }, - { - name: "new line", - code: '\n \u2005 \n' - }, -]}, ["vue"]); + ["vue"] +); From bd7eed8a00c7d8bdcf680ac90d37f9149282be07 Mon Sep 17 00:00:00 2001 From: fisker Date: Wed, 26 Aug 2020 00:54:01 +0800 Subject: [PATCH 5/7] Check empty --- src/document/doc-utils.js | 4 ++++ src/language-html/printer-html.js | 8 +++++--- tests/vue/multiparser/__snapshots__/jsfmt.spec.js.snap | 4 +--- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/document/doc-utils.js b/src/document/doc-utils.js index e31adbbca304..527d9868df6e 100644 --- a/src/document/doc-utils.js +++ b/src/document/doc-utils.js @@ -226,6 +226,10 @@ function normalizeParts(parts) { continue; } + if (part.type === "fill" && part.parts.length === 0) { + continue; + } + if (part.type === "concat") { restParts.unshift(...part.parts); continue; diff --git a/src/language-html/printer-html.js b/src/language-html/printer-html.js index 9058c0ecbbf2..ec2dc60afc2e 100644 --- a/src/language-html/printer-html.js +++ b/src/language-html/printer-html.js @@ -3,7 +3,7 @@ const assert = require("assert"); const { builders, - utils: { mapDoc, normalizeParts }, + utils: { mapDoc, normalizeParts, normalizeDoc }, } = require("../document"); const { replaceEndOfLineWith } = require("../common/util"); const { print: printFrontMatter } = require("../utils/front-matter"); @@ -84,12 +84,14 @@ function embed(path, print, textToDoc, options) { { stripTrailingHardline: true } ); + const isEmpty = doc === "" || normalizeDoc(doc).parts.length === 0; + return concat([ printOpeningTagPrefix(node, options), group(printOpeningTag(path, options, print)), - doc === "" ? "" : hardline, + isEmpty ? "" : hardline, doc, - doc === "" ? "" : hardline, + isEmpty ? "" : hardline, printClosingTag(node, options), printClosingTagSuffix(node, options), ]); diff --git a/tests/vue/multiparser/__snapshots__/jsfmt.spec.js.snap b/tests/vue/multiparser/__snapshots__/jsfmt.spec.js.snap index 612d6e95db8d..a921d3a0dbee 100644 --- a/tests/vue/multiparser/__snapshots__/jsfmt.spec.js.snap +++ b/tests/vue/multiparser/__snapshots__/jsfmt.spec.js.snap @@ -140,9 +140,7 @@ printWidth: 80   =====================================output===================================== - - - + ================================================================================ `; From 875b478b592a84da6596101eb818f6e8264296b5 Mon Sep 17 00:00:00 2001 From: fisker Date: Wed, 26 Aug 2020 00:59:37 +0800 Subject: [PATCH 6/7] Check empty before parsing --- src/language-html/printer-html.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/language-html/printer-html.js b/src/language-html/printer-html.js index ec2dc60afc2e..3ba98e697397 100644 --- a/src/language-html/printer-html.js +++ b/src/language-html/printer-html.js @@ -78,13 +78,17 @@ function embed(path, print, textToDoc, options) { return; } - const doc = textToDoc( - htmlTrimPreserveIndentation(getNodeContent(node, options)), - { parser }, - { stripTrailingHardline: true } - ); - - const isEmpty = doc === "" || normalizeDoc(doc).parts.length === 0; + const content = getNodeContent(node, options); + let isEmpty = /^\s*$/.test(content); + let doc = ""; + if (!isEmpty) { + doc = textToDoc( + htmlTrimPreserveIndentation(content), + { parser }, + { stripTrailingHardline: true } + ); + isEmpty = doc === "" || normalizeDoc(doc).parts.length === 0; + } return concat([ printOpeningTagPrefix(node, options), From d987146599cd43f8ecc7386e51adbb0da0a5449f Mon Sep 17 00:00:00 2001 From: fisker Date: Wed, 26 Aug 2020 01:12:17 +0800 Subject: [PATCH 7/7] Remove doc check --- src/document/doc-utils.js | 4 ---- src/language-html/printer-html.js | 4 ++-- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/document/doc-utils.js b/src/document/doc-utils.js index 527d9868df6e..e31adbbca304 100644 --- a/src/document/doc-utils.js +++ b/src/document/doc-utils.js @@ -226,10 +226,6 @@ function normalizeParts(parts) { continue; } - if (part.type === "fill" && part.parts.length === 0) { - continue; - } - if (part.type === "concat") { restParts.unshift(...part.parts); continue; diff --git a/src/language-html/printer-html.js b/src/language-html/printer-html.js index 3ba98e697397..0beeea66333c 100644 --- a/src/language-html/printer-html.js +++ b/src/language-html/printer-html.js @@ -3,7 +3,7 @@ const assert = require("assert"); const { builders, - utils: { mapDoc, normalizeParts, normalizeDoc }, + utils: { mapDoc, normalizeParts }, } = require("../document"); const { replaceEndOfLineWith } = require("../common/util"); const { print: printFrontMatter } = require("../utils/front-matter"); @@ -87,7 +87,7 @@ function embed(path, print, textToDoc, options) { { parser }, { stripTrailingHardline: true } ); - isEmpty = doc === "" || normalizeDoc(doc).parts.length === 0; + isEmpty = doc === ""; } return concat([