Skip to content

fix(compiler-core): compilation of slot v-if/else when preserve whitespace #9448

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,34 @@ return function render(_ctx, _cache) {
}"
`;

exports[`compiler: transform component slots > template named v-if/else and whitespace preserve 1`] = `
"const { toDisplayString: _toDisplayString, createTextVNode: _createTextVNode, resolveComponent: _resolveComponent, withCtx: _withCtx, createSlots: _createSlots, openBlock: _openBlock, createBlock: _createBlock } = Vue

return function render(_ctx, _cache) {
const _component_Comp = _resolveComponent(\\"Comp\\")

return (_openBlock(), _createBlock(_component_Comp, null, _createSlots({ _: 2 /* DYNAMIC */ }, [
true
? {
name: \\"one\\",
fn: _withCtx(() => [
_createTextVNode(_toDisplayString(_ctx.foo) + _toDisplayString(_ctx.bar), 1 /* TEXT */)
]),
key: \\"0\\"
}
: true
? {
name: \\"one\\",
fn: _withCtx(() => [
_createTextVNode(_toDisplayString(_ctx.foo) + _toDisplayString(_ctx.bar), 1 /* TEXT */)
]),
key: \\"1\\"
}
: undefined
]), 1024 /* DYNAMIC_SLOTS */))
}"
`;

exports[`compiler: transform component slots > with whitespace: 'preserve' > implicit default slot 1`] = `
"const { createElementVNode: _createElementVNode, resolveComponent: _resolveComponent, withCtx: _withCtx, openBlock: _openBlock, createBlock: _createBlock } = Vue

Expand Down
32 changes: 30 additions & 2 deletions packages/compiler-core/__tests__/transforms/vSlot.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,14 @@ import { createObjectMatcher, genFlagText } from '../testUtils'
import { PatchFlags } from '@vue/shared'
import { transformFor } from '../../src/transforms/vFor'
import { transformIf } from '../../src/transforms/vIf'
import { transformText } from '../../src/transforms/transformText'

function parseWithSlots(template: string, options: CompilerOptions = {}) {
function parseWithSlots(
template: string,
options: CompilerOptions & {
appendNodeTransforms?: CompilerOptions['nodeTransforms']
} = {}
) {
const ast = parse(template, {
whitespace: options.whitespace
})
Expand All @@ -42,7 +48,8 @@ function parseWithSlots(template: string, options: CompilerOptions = {}) {
: []),
transformSlotOutlet,
transformElement,
trackSlotScopes
trackSlotScopes,
...(options.appendNodeTransforms ?? [])
],
directiveTransforms: {
on: transformOn,
Expand Down Expand Up @@ -234,6 +241,27 @@ describe('compiler: transform component slots', () => {
expect(generate(root, { prefixIdentifiers: true }).code).toMatchSnapshot()
})

// #6063 should not throw error
test('template named v-if/else and whitespace preserve', () => {
const { root } = parseWithSlots(
`<Comp>
<template v-if="true" #one>
{{ foo }}{{ bar }}
</template>
<template v-else="true" #one>
{{ foo }}{{ bar }}
</template>
</Comp>`,
{
whitespace: 'preserve',
prefixIdentifiers: true,
appendNodeTransforms: [transformText]
}
)

expect(generate(root, { prefixIdentifiers: true }).code).toMatchSnapshot()
})

test('on component dynamically named slot', () => {
const { root, slots } = parseWithSlots(
`<Comp v-slot:[named]="{ foo }">{{ foo }}{{ bar }}</Comp>`,
Expand Down
7 changes: 7 additions & 0 deletions packages/compiler-core/src/transforms/vSlot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,13 @@ export function buildSlots(
let prev
while (j--) {
prev = children[j]
if (
prev.type === NodeTypes.TEXT_CALL &&
prev.content.type === NodeTypes.TEXT &&
prev.content.content === ' '
) {
continue
}
if (prev.type !== NodeTypes.COMMENT) {
break
}
Expand Down
Loading