Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compiler-core): keep whitespaces with newline between interpolation and comment #6828

Merged
merged 1 commit into from Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/compiler-core/__tests__/parse.spec.ts
Expand Up @@ -1980,6 +1980,17 @@ foo
expect(ast.children[2].type).toBe(NodeTypes.INTERPOLATION)
})

it('should NOT remove whitespaces w/ newline between interpolation and comment', () => {
const ast = parse(`<!-- foo --> \n {{msg}}`)
expect(ast.children.length).toBe(3)
expect(ast.children[0].type).toBe(NodeTypes.COMMENT)
expect(ast.children[1]).toMatchObject({
type: NodeTypes.TEXT,
content: ' '
})
expect(ast.children[2].type).toBe(NodeTypes.INTERPOLATION)
})

it('should NOT remove whitespaces w/o newline between elements', () => {
const ast = parse(`<div/> <div/> <div/>`)
expect(ast.children.length).toBe(5)
Expand Down
11 changes: 8 additions & 3 deletions packages/compiler-core/src/parse.ts
Expand Up @@ -264,14 +264,19 @@ function parseChildren(
const next = nodes[i + 1]
// Remove if:
// - the whitespace is the first or last node, or:
// - (condense mode) the whitespace is adjacent to a comment, or:
// - (condense mode) the whitespace is between twos comments, or:
// - (condense mode) the whitespace is between comment and element, or:
// - (condense mode) the whitespace is between two elements AND contains newline
if (
!prev ||
!next ||
(shouldCondense &&
(prev.type === NodeTypes.COMMENT ||
next.type === NodeTypes.COMMENT ||
((prev.type === NodeTypes.COMMENT &&
next.type === NodeTypes.COMMENT) ||
(prev.type === NodeTypes.COMMENT &&
next.type === NodeTypes.ELEMENT) ||
(prev.type === NodeTypes.ELEMENT &&
next.type === NodeTypes.COMMENT) ||
(prev.type === NodeTypes.ELEMENT &&
next.type === NodeTypes.ELEMENT &&
/[\r\n]/.test(node.content))))
Expand Down