Skip to content

Commit 10ebcef

Browse files
authored
fix(compiler-core): ignore whitespace when matching adjacent v-if (#12321)
close #9173
1 parent f05a8d6 commit 10ebcef

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

packages/compiler-core/__tests__/transforms/__snapshots__/vSlot.spec.ts.snap

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,28 @@ return function render(_ctx, _cache) {
246246
}"
247247
`;
248248

249+
exports[`compiler: transform component slots > with whitespace: 'preserve' > named slot with v-if + v-else 1`] = `
250+
"const { resolveComponent: _resolveComponent, withCtx: _withCtx, createSlots: _createSlots, openBlock: _openBlock, createBlock: _createBlock } = Vue
251+
252+
return function render(_ctx, _cache) {
253+
const _component_Comp = _resolveComponent("Comp")
254+
255+
return (_openBlock(), _createBlock(_component_Comp, null, _createSlots({ _: 2 /* DYNAMIC */ }, [
256+
ok
257+
? {
258+
name: "one",
259+
fn: _withCtx(() => ["foo"]),
260+
key: "0"
261+
}
262+
: {
263+
name: "two",
264+
fn: _withCtx(() => ["baz"]),
265+
key: "1"
266+
}
267+
]), 1024 /* DYNAMIC_SLOTS */))
268+
}"
269+
`;
270+
249271
exports[`compiler: transform component slots > with whitespace: 'preserve' > should not generate whitespace only default slot 1`] = `
250272
"const { resolveComponent: _resolveComponent, withCtx: _withCtx, openBlock: _openBlock, createBlock: _createBlock } = Vue
251273

packages/compiler-core/__tests__/transforms/vSlot.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -988,5 +988,19 @@ describe('compiler: transform component slots', () => {
988988

989989
expect(generate(root, { prefixIdentifiers: true }).code).toMatchSnapshot()
990990
})
991+
992+
test('named slot with v-if + v-else', () => {
993+
const source = `
994+
<Comp>
995+
<template #one v-if="ok">foo</template>
996+
<template #two v-else>baz</template>
997+
</Comp>
998+
`
999+
const { root } = parseWithSlots(source, {
1000+
whitespace: 'preserve',
1001+
})
1002+
1003+
expect(generate(root, { prefixIdentifiers: true }).code).toMatchSnapshot()
1004+
})
9911005
})
9921006
})

packages/compiler-core/src/transforms/vSlot.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ export function buildSlots(
222222
let prev
223223
while (j--) {
224224
prev = children[j]
225-
if (prev.type !== NodeTypes.COMMENT) {
225+
if (prev.type !== NodeTypes.COMMENT && isNonWhitespaceContent(prev)) {
226226
break
227227
}
228228
}

0 commit comments

Comments
 (0)