Skip to content

Commit 46baad0

Browse files
authored
Fix vue print error with self-closing tags (#3705)
1 parent 0e0fa5c commit 46baad0

4 files changed

Lines changed: 57 additions & 12 deletions

File tree

src/language-vue/parser-vue.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -388,12 +388,16 @@ function parse(text /*, parsers, opts*/) {
388388
attrs,
389389
unary,
390390
start,
391-
contentStart: end,
392391
children: []
393392
};
394393
obj.children.push(newObj);
395-
objStack.push(newObj);
396-
obj = newObj;
394+
if (unary) {
395+
newObj.end = end;
396+
} else {
397+
newObj.contentStart = end;
398+
objStack.push(newObj);
399+
obj = newObj;
400+
}
397401
},
398402
end: function(tag, start, end) {
399403
objStack.pop();

src/language-vue/printer-vue.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,18 @@ function genericPrint(path, options, print) {
99
const n = path.getValue();
1010
const res = [];
1111
let index = n.start;
12-
let printParent = typeof n.end === "number";
1312

1413
path.each(childPath => {
1514
const child = childPath.getValue();
1615
res.push(options.originalText.slice(index, child.start));
1716
res.push(childPath.call(print));
18-
if (typeof child.end === "number") {
19-
index = child.end;
20-
} else {
21-
printParent = false;
22-
}
17+
index = child.end;
2318
}, "children");
2419

25-
if (printParent) {
26-
res.push(options.originalText.slice(index, n.end));
27-
}
20+
// If there are no children, we just print the node from start to end.
21+
// Otherwise, index should point to the end of the last child, and we
22+
// need to print the closing tag.
23+
res.push(options.originalText.slice(index, n.end));
2824

2925
// Only force a trailing newline if there were any contents.
3026
if (n.tag === "root" && n.children.length) {

tests/vue_examples/__snapshots__/jsfmt.spec.js.snap

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,44 @@ export default {
302302
303303
`;
304304

305+
exports[`self_closing.vue 1`] = `
306+
<template>
307+
<div />
308+
</template>
309+
310+
<script>
311+
foo( )
312+
</script>
313+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
314+
<template>
315+
<div />
316+
</template>
317+
318+
<script>
319+
foo();
320+
</script>
321+
322+
`;
323+
324+
exports[`self_closing.vue 2`] = `
325+
<template>
326+
<div />
327+
</template>
328+
329+
<script>
330+
foo( )
331+
</script>
332+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
333+
<template>
334+
<div />
335+
</template>
336+
337+
<script>
338+
foo();
339+
</script>
340+
341+
`;
342+
305343
exports[`test.vue 1`] = `
306344
<script>
307345
</script>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<template>
2+
<div />
3+
</template>
4+
5+
<script>
6+
foo( )
7+
</script>

0 commit comments

Comments
 (0)