Skip to content

Commit

Permalink
fix(compiler-core): allow spaces between if-else branches (#2305)
Browse files Browse the repository at this point in the history
fix #2299
  • Loading branch information
unbyte authored Oct 8, 2020
1 parent 25d53f0 commit 89c5909
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packages/compiler-core/__tests__/transforms/vIf.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,27 @@ describe('compiler: v-if', () => {
expect(branch1.props).toMatchObject(createObjectMatcher({ key: `[0]` }))
})

test('with spaces between branches', () => {
const {
node: { codegenNode }
} = parseWithIfTransform(
`<div v-if="ok"/> <div v-else-if="no"/> <div v-else/>`
)
expect(codegenNode.consequent).toMatchObject({
tag: `"div"`,
props: createObjectMatcher({ key: `[0]` })
})
const branch = codegenNode.alternate as ConditionalExpression
expect(branch.consequent).toMatchObject({
tag: `"div"`,
props: createObjectMatcher({ key: `[1]` })
})
expect(branch.alternate).toMatchObject({
tag: `"div"`,
props: createObjectMatcher({ key: `[2]` })
})
})

test('with comments', () => {
const { node } = parseWithIfTransform(`
<template v-if="ok">
Expand Down
10 changes: 10 additions & 0 deletions packages/compiler-core/src/transforms/vIf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,16 @@ export function processIf(
comments.unshift(sibling)
continue
}

if (
sibling &&
sibling.type === NodeTypes.TEXT &&
!sibling.content.trim().length
) {
context.removeNode(sibling)
continue
}

if (sibling && sibling.type === NodeTypes.IF) {
// move the node to the if node's branches
context.removeNode()
Expand Down

0 comments on commit 89c5909

Please sign in to comment.