Skip to content

Commit 4887618

Browse files
authoredNov 8, 2022
fix(compiler-core): keep whitespaces between interpolation and comment (#6828)
fix #6352
1 parent 28a4fc0 commit 4887618

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed
 

‎packages/compiler-core/__tests__/parse.spec.ts

+11
Original file line numberDiff line numberDiff line change
@@ -1980,6 +1980,17 @@ foo
19801980
expect(ast.children[2].type).toBe(NodeTypes.INTERPOLATION)
19811981
})
19821982

1983+
it('should NOT remove whitespaces w/ newline between interpolation and comment', () => {
1984+
const ast = parse(`<!-- foo --> \n {{msg}}`)
1985+
expect(ast.children.length).toBe(3)
1986+
expect(ast.children[0].type).toBe(NodeTypes.COMMENT)
1987+
expect(ast.children[1]).toMatchObject({
1988+
type: NodeTypes.TEXT,
1989+
content: ' '
1990+
})
1991+
expect(ast.children[2].type).toBe(NodeTypes.INTERPOLATION)
1992+
})
1993+
19831994
it('should NOT remove whitespaces w/o newline between elements', () => {
19841995
const ast = parse(`<div/> <div/> <div/>`)
19851996
expect(ast.children.length).toBe(5)

‎packages/compiler-core/src/parse.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -264,14 +264,19 @@ function parseChildren(
264264
const next = nodes[i + 1]
265265
// Remove if:
266266
// - the whitespace is the first or last node, or:
267-
// - (condense mode) the whitespace is adjacent to a comment, or:
267+
// - (condense mode) the whitespace is between twos comments, or:
268+
// - (condense mode) the whitespace is between comment and element, or:
268269
// - (condense mode) the whitespace is between two elements AND contains newline
269270
if (
270271
!prev ||
271272
!next ||
272273
(shouldCondense &&
273-
(prev.type === NodeTypes.COMMENT ||
274-
next.type === NodeTypes.COMMENT ||
274+
((prev.type === NodeTypes.COMMENT &&
275+
next.type === NodeTypes.COMMENT) ||
276+
(prev.type === NodeTypes.COMMENT &&
277+
next.type === NodeTypes.ELEMENT) ||
278+
(prev.type === NodeTypes.ELEMENT &&
279+
next.type === NodeTypes.COMMENT) ||
275280
(prev.type === NodeTypes.ELEMENT &&
276281
next.type === NodeTypes.ELEMENT &&
277282
/[\r\n]/.test(node.content))))

0 commit comments

Comments
 (0)
Please sign in to comment.