Skip to content

Commit ebe9e07

Browse files
authored
Vue: Fix format on self-closing and empty blocks (#9055)
1 parent 5a12f52 commit ebe9e07

File tree

5 files changed

+115
-10
lines changed

5 files changed

+115
-10
lines changed

changelog_unreleased/vue/pr-9052.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
1-
#### Fix format blocks with `src` attribute ([#9052](https://github.com/prettier/prettier/pull/9052) by [@fisker](https://github.com/fisker))
1+
#### 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))
22

33
<!-- prettier-ignore -->
44
```vue
55
<!-- Input -->
66
<custom lang="markdown" src="./foo.md"></custom>
77
<custom lang="markdown" src="./foo.md" />
8+
<custom lang="markdown" />
89
910
<!-- Prettier stable -->
1011
<custom lang="markdown" src="./foo.md">
1112
1213
</custom>
1314
<custom lang="markdown" src="./foo.md"
1415
16+
/>
17+
<custom lang="markdown"
18+
1519
/>
1620
1721
<!-- Prettier master -->
1822
<custom lang="markdown" src="./foo.md"></custom>
1923
<custom lang="markdown" src="./foo.md" />
24+
<custom lang="markdown" />
2025
```

src/language-html/printer-html.js

+14-8
Original file line numberDiff line numberDiff line change
@@ -72,24 +72,30 @@ function embed(path, print, textToDoc, options) {
7272
return;
7373
}
7474

75-
if (isVueNonHtmlBlock(node, options)) {
75+
if (!node.isSelfClosing && isVueNonHtmlBlock(node, options)) {
7676
const parser = inferScriptParser(node, options);
7777
if (!parser) {
7878
return;
7979
}
8080

81-
const doc = textToDoc(
82-
htmlTrimPreserveIndentation(getNodeContent(node, options)),
83-
{ parser },
84-
{ stripTrailingHardline: true }
85-
);
81+
const content = getNodeContent(node, options);
82+
let isEmpty = /^\s*$/.test(content);
83+
let doc = "";
84+
if (!isEmpty) {
85+
doc = textToDoc(
86+
htmlTrimPreserveIndentation(content),
87+
{ parser },
88+
{ stripTrailingHardline: true }
89+
);
90+
isEmpty = doc === "";
91+
}
8692

8793
return concat([
8894
printOpeningTagPrefix(node, options),
8995
group(printOpeningTag(path, options, print)),
90-
hardline,
96+
isEmpty ? "" : hardline,
9197
doc,
92-
hardline,
98+
isEmpty ? "" : hardline,
9399
printClosingTag(node, options),
94100
printClosingTagSuffix(node, options),
95101
]);

tests/vue/multiparser/__snapshots__/jsfmt.spec.js.snap

+70
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,62 @@ export default {
102102
================================================================================
103103
`;
104104
105+
exports[`snippet: empty format 1`] = `
106+
====================================options=====================================
107+
parsers: ["vue"]
108+
printWidth: 80
109+
| printWidth
110+
=====================================input======================================
111+
<custom lang="markdown"></custom>
112+
=====================================output=====================================
113+
<custom lang="markdown"></custom>
114+
115+
================================================================================
116+
`;
117+
118+
exports[`snippet: new line format 1`] = `
119+
====================================options=====================================
120+
parsers: ["vue"]
121+
printWidth: 80
122+
| printWidth
123+
=====================================input======================================
124+
<custom lang="markdown">
125+
126+
</custom>
127+
=====================================output=====================================
128+
<custom lang="markdown"></custom>
129+
130+
================================================================================
131+
`;
132+
133+
exports[`snippet: non-space format 1`] = `
134+
====================================options=====================================
135+
parsers: ["vue"]
136+
printWidth: 80
137+
| printWidth
138+
=====================================input======================================
139+
<custom lang="markdown">
140+
141+
</custom>
142+
=====================================output=====================================
143+
<custom lang="markdown"></custom>
144+
145+
================================================================================
146+
`;
147+
148+
exports[`snippet: spaces format 1`] = `
149+
====================================options=====================================
150+
parsers: ["vue"]
151+
printWidth: 80
152+
| printWidth
153+
=====================================input======================================
154+
<custom lang="markdown"> </custom>
155+
=====================================output=====================================
156+
<custom lang="markdown"></custom>
157+
158+
================================================================================
159+
`;
160+
105161
exports[`template-bind.vue format 1`] = `
106162
====================================options=====================================
107163
parsers: ["vue"]
@@ -164,6 +220,20 @@ printWidth: 80
164220
================================================================================
165221
`;
166222
223+
exports[`void-element.vue format 1`] = `
224+
====================================options=====================================
225+
parsers: ["vue"]
226+
printWidth: 80
227+
| printWidth
228+
=====================================input======================================
229+
<custom lang="markdown" />
230+
231+
=====================================output=====================================
232+
<custom lang="markdown" />
233+
234+
================================================================================
235+
`;
236+
167237
exports[`vue-component.vue format 1`] = `
168238
====================================options=====================================
169239
parsers: ["vue"]

tests/vue/multiparser/jsfmt.spec.js

+24-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,24 @@
1-
run_spec(__dirname, ["vue"]);
1+
run_spec(
2+
{
3+
dirname: __dirname,
4+
snippets: [
5+
{
6+
name: "empty",
7+
code: '<custom lang="markdown"></custom>',
8+
},
9+
{
10+
name: "spaces",
11+
code: '<custom lang="markdown"> </custom>',
12+
},
13+
{
14+
name: "new line",
15+
code: '<custom lang="markdown">\n \n</custom>',
16+
},
17+
{
18+
name: "non-space",
19+
code: '<custom lang="markdown">\n \u2005 \n</custom>',
20+
},
21+
],
22+
},
23+
["vue"]
24+
);
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<custom lang="markdown" />

0 commit comments

Comments
 (0)