Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -324,7 +324,7 @@ export function render(_ctx) {
const n1 = _createComponentWithFallback(_component_Comp, null, {
"foo": _withVaporCtx((_slotProps0) => {
const n0 = t0()
_renderEffect(() => _setText(n0, _toDisplayString(_slotProps0["a"] + _slotProps0["b"])))
_renderEffect(() => _setText(n0, _toDisplayString(_slotProps0.a + _slotProps0.b)))
return n0
})
}, true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export function render(_ctx) {
const n4 = _createComponentWithFallback(_component_Child, null, {
"foo": _withVaporCtx((_slotProps0) => {
const n0 = t0()
_renderEffect(() => _setText(n0, _toDisplayString(_slotProps0["msg"])))
_renderEffect(() => _setText(n0, _toDisplayString(_slotProps0.msg)))
return n0
}),
"bar": _withVaporCtx(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function render(_ctx) {
name: item,
fn: _withVaporCtx((_slotProps0) => {
const n0 = t0()
_renderEffect(() => _setText(n0, _toDisplayString(_slotProps0["bar"])))
_renderEffect(() => _setText(n0, _toDisplayString(_slotProps0.bar)))
return n0
})
})))
Expand Down Expand Up @@ -275,12 +275,12 @@ export function render(_ctx) {
const n1 = _createComponentWithFallback(_component_Inner, null, {
"default": _withVaporCtx((_slotProps1) => {
const n0 = t0()
_renderEffect(() => _setText(n0, _toDisplayString(_slotProps0["foo"] + _slotProps1["bar"] + _ctx.baz)))
_renderEffect(() => _setText(n0, _toDisplayString(_slotProps0.foo + _slotProps1.bar + _ctx.baz)))
return n0
})
})
const n3 = t0()
_renderEffect(() => _setText(n3, " " + _toDisplayString(_slotProps0["foo"] + _ctx.bar + _ctx.baz)))
_renderEffect(() => _setText(n3, " " + _toDisplayString(_slotProps0.foo + _ctx.bar + _ctx.baz)))
return [n1, n3]
})
}, true)
Expand All @@ -300,7 +300,7 @@ export function render(_ctx) {
name: _ctx.named,
fn: _withVaporCtx((_slotProps0) => {
const n0 = t0()
_renderEffect(() => _setText(n0, _toDisplayString(_slotProps0["foo"] + _ctx.bar)))
_renderEffect(() => _setText(n0, _toDisplayString(_slotProps0.foo + _ctx.bar)))
return n0
})
})
Expand All @@ -319,7 +319,7 @@ export function render(_ctx) {
const n1 = _createComponentWithFallback(_component_Comp, null, {
"named": _withVaporCtx((_slotProps0) => {
const n0 = t0()
_renderEffect(() => _setText(n0, _toDisplayString(_slotProps0["foo"] + _ctx.bar)))
_renderEffect(() => _setText(n0, _toDisplayString(_slotProps0.foo + _ctx.bar)))
return n0
})
}, true)
Expand All @@ -336,7 +336,7 @@ export function render(_ctx) {
const n1 = _createComponentWithFallback(_component_Comp, null, {
"default": _withVaporCtx((_slotProps0) => {
const n0 = t0()
_renderEffect(() => _setText(n0, _toDisplayString(_slotProps0["foo"] + _ctx.bar)))
_renderEffect(() => _setText(n0, _toDisplayString(_slotProps0.foo + _ctx.bar)))
return n0
})
}, true)
Expand Down Expand Up @@ -380,6 +380,91 @@ export function render(_ctx) {
}"
`;

exports[`compiler: transform slot > slot prop alias uses original key 1`] = `
"import { resolveComponent as _resolveComponent, toDisplayString as _toDisplayString, setText as _setText, renderEffect as _renderEffect, withVaporCtx as _withVaporCtx, createComponentWithFallback as _createComponentWithFallback, template as _template } from 'vue';
const t0 = _template(" ")

export function render(_ctx) {
const _component_Comp = _resolveComponent("Comp")
const n2 = _createComponentWithFallback(_component_Comp, null, {
"default": _withVaporCtx((_slotProps0) => {
const n0 = t0()
_renderEffect(() => _setText(n0, _toDisplayString(_slotProps0.msg)))
return n0
})
}, true)
return n2
}"
`;

exports[`compiler: transform slot > slot prop computed key destructuring 1`] = `
"import { resolveComponent as _resolveComponent, toDisplayString as _toDisplayString, setText as _setText, renderEffect as _renderEffect, withVaporCtx as _withVaporCtx, createComponentWithFallback as _createComponentWithFallback, template as _template } from 'vue';
const t0 = _template(" ")

export function render(_ctx) {
const _component_Comp = _resolveComponent("Comp")
const n2 = _createComponentWithFallback(_component_Comp, null, {
"default": _withVaporCtx((_slotProps0) => {
const n0 = t0()
_renderEffect(() => _setText(n0, _toDisplayString(_slotProps0[_ctx.key])))
return n0
})
}, true)
return n2
}"
`;

exports[`compiler: transform slot > slot prop default value 1`] = `
"import { resolveComponent as _resolveComponent, getDefaultValue as _getDefaultValue, toDisplayString as _toDisplayString, setText as _setText, renderEffect as _renderEffect, withVaporCtx as _withVaporCtx, createComponentWithFallback as _createComponentWithFallback, template as _template } from 'vue';
const t0 = _template(" ")

export function render(_ctx) {
const _component_Comp = _resolveComponent("Comp")
const n2 = _createComponentWithFallback(_component_Comp, null, {
"default": _withVaporCtx((_slotProps0) => {
const n0 = t0()
_renderEffect(() => _setText(n0, _toDisplayString(_getDefaultValue(_slotProps0.foo, 1))))
return n0
})
}, true)
return n2
}"
`;

exports[`compiler: transform slot > slot prop nested destructuring 1`] = `
"import { resolveComponent as _resolveComponent, toDisplayString as _toDisplayString, setText as _setText, renderEffect as _renderEffect, withVaporCtx as _withVaporCtx, createComponentWithFallback as _createComponentWithFallback, template as _template } from 'vue';
const t0 = _template(" ")

export function render(_ctx) {
const _component_Comp = _resolveComponent("Comp")
const n2 = _createComponentWithFallback(_component_Comp, null, {
"default": _withVaporCtx((_slotProps0) => {
const n0 = t0()
_renderEffect(() => _setText(n0, _toDisplayString(_slotProps0.foo.bar)))
return n0
})
}, true)
return n2
}"
`;

exports[`compiler: transform slot > slot prop rest destructuring 1`] = `
"import { resolveComponent as _resolveComponent, getRestElement as _getRestElement, toDisplayString as _toDisplayString, setText as _setText, renderEffect as _renderEffect, withVaporCtx as _withVaporCtx, createComponentWithFallback as _createComponentWithFallback, template as _template } from 'vue';
const t0 = _template(" ")

export function render(_ctx) {
const _component_Comp = _resolveComponent("Comp")
const n2 = _createComponentWithFallback(_component_Comp, null, {
"default": _withVaporCtx((_slotProps0) => {
const n0 = t0()
_renderEffect(() => _setText(n0, _toDisplayString(_getRestElement(_slotProps0, ["foo"]).bar)))
return n0
})
}, true)
return n2
}"
`;

exports[`compiler: transform slot > with whitespace: 'preserve' > implicit default slot 1`] = `
"import { resolveComponent as _resolveComponent, withVaporCtx as _withVaporCtx, createComponentWithFallback as _createComponentWithFallback, template as _template } from 'vue';
const t0 = _template(" Header ")
Expand Down
62 changes: 56 additions & 6 deletions packages/compiler-vapor/__tests__/transforms/vSlot.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ describe('compiler: transform slot', () => {
expect(code).toMatchSnapshot()

expect(code).contains(`"default": _withVaporCtx((_slotProps0) =>`)
expect(code).contains(`_slotProps0["foo"] + _ctx.bar`)
expect(code).contains(`_slotProps0.foo + _ctx.bar`)

expect(ir.block.dynamic.children[0].operation).toMatchObject({
type: IRNodeTypes.CREATE_COMPONENT_NODE,
Expand Down Expand Up @@ -102,7 +102,7 @@ describe('compiler: transform slot', () => {
expect(code).toMatchSnapshot()

expect(code).contains(`"named": _withVaporCtx((_slotProps0) =>`)
expect(code).contains(`_slotProps0["foo"] + _ctx.bar`)
expect(code).contains(`_slotProps0.foo + _ctx.bar`)

expect(ir.block.dynamic.children[0].operation).toMatchObject({
type: IRNodeTypes.CREATE_COMPONENT_NODE,
Expand Down Expand Up @@ -131,7 +131,7 @@ describe('compiler: transform slot', () => {
expect(code).toMatchSnapshot()

expect(code).contains(`fn: _withVaporCtx((_slotProps0) =>`)
expect(code).contains(`_slotProps0["foo"] + _ctx.bar`)
expect(code).contains(`_slotProps0.foo + _ctx.bar`)

expect(ir.block.dynamic.children[0].operation).toMatchObject({
type: IRNodeTypes.CREATE_COMPONENT_NODE,
Expand Down Expand Up @@ -165,6 +165,56 @@ describe('compiler: transform slot', () => {
expect(code).toMatchSnapshot()
})

test('slot prop alias uses original key', () => {
const { code } = compileWithSlots(
`<Comp><template #default="{ msg: msg1 }">{{ msg1 }}</template></Comp>`,
)

expect(code).toMatchSnapshot()
expect(code).contains(`"default": _withVaporCtx((_slotProps0) =>`)
expect(code).contains(`_slotProps0.msg`)
})

test('slot prop nested destructuring', () => {
const { code } = compileWithSlots(
`<Comp><template #default="{ foo: { bar: baz } }">{{ baz }}</template></Comp>`,
)

expect(code).toMatchSnapshot()
expect(code).contains(`"default": _withVaporCtx((_slotProps0) =>`)
expect(code).contains(`_slotProps0.foo.bar`)
})

test('slot prop computed key destructuring', () => {
const { code } = compileWithSlots(
`<Comp><template #default="{ [key]: val }">{{ val }}</template></Comp>`,
)

expect(code).toMatchSnapshot()
expect(code).contains(`"default": _withVaporCtx((_slotProps0) =>`)
expect(code).contains(`_slotProps0[_ctx.key]`)
})

test('slot prop rest destructuring', () => {
const { code } = compileWithSlots(
`<Comp><template #default="{ foo, ...rest }">{{ rest.bar }}</template></Comp>`,
)

expect(code).toMatchSnapshot()
expect(code).contains(`"default": _withVaporCtx((_slotProps0) =>`)
expect(code).contains(`_getRestElement(_slotProps0`)
})

test('slot prop default value', () => {
const { code } = compileWithSlots(
`<Comp><template #default="{ foo = 1 }">{{ foo }}</template></Comp>`,
)

expect(code).toMatchSnapshot()
expect(code).contains(`"default": _withVaporCtx((_slotProps0) =>`)
expect(code).contains(`_getDefaultValue(_slotProps0.foo, 1)`)
})

test('named slots w/ implicit default slot', () => {
const { ir, code } = compileWithSlots(
`<Comp>
Expand Down Expand Up @@ -216,8 +266,8 @@ describe('compiler: transform slot', () => {

expect(code).contains(`"default": _withVaporCtx((_slotProps0) =>`)
expect(code).contains(`"default": _withVaporCtx((_slotProps1) =>`)
expect(code).contains(`_slotProps0["foo"] + _slotProps1["bar"] + _ctx.baz`)
expect(code).contains(`_slotProps0["foo"] + _ctx.bar + _ctx.baz`)
expect(code).contains(`_slotProps0.foo + _slotProps1.bar + _ctx.baz`)
expect(code).contains(`_slotProps0.foo + _ctx.bar + _ctx.baz`)

const outerOp = ir.block.dynamic.children[0].operation
expect(outerOp).toMatchObject({
Expand Down Expand Up @@ -293,7 +343,7 @@ describe('compiler: transform slot', () => {
expect(code).toMatchSnapshot()

expect(code).contains(`fn: _withVaporCtx((_slotProps0) =>`)
expect(code).contains(`_setText(n0, _toDisplayString(_slotProps0["bar"]))`)
expect(code).contains(`_setText(n0, _toDisplayString(_slotProps0.bar))`)

expect(ir.block.dynamic.children[0].operation).toMatchObject({
type: IRNodeTypes.CREATE_COMPONENT_NODE,
Expand Down
44 changes: 22 additions & 22 deletions packages/compiler-vapor/src/generators/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,16 @@ import {
createSimpleExpression,
isMemberExpression,
toValidAssetId,
walkIdentifiers,
} from '@vue/compiler-dom'
import { genEventHandler } from './event'
import { genDirectiveModifiers, genDirectivesForElement } from './directive'
import { genBlock } from './block'
import {
type DestructureMap,
type DestructureMapValue,
buildDestructureIdMap,
parseValueDestructure,
} from './for'
import { genModelHandler } from './vModel'
import { isBuiltInComponent, isKeepAliveTag } from '../utils'

Expand Down Expand Up @@ -405,40 +410,35 @@ function genConditionalSlot(
}

function genSlotBlockWithProps(oper: SlotBlockIRNode, context: CodegenContext) {
let isDestructureAssignment = false
let rawProps: string | undefined
let propsName: string | undefined
let exitScope: (() => void) | undefined
let depth: number | undefined
const { props, key, node } = oper
const idsOfProps = new Set<string>()
const idToPathMap: DestructureMap = props
? parseValueDestructure(props, context)
: new Map<string, DestructureMapValue | null>()

if (props) {
rawProps = props.content
if ((isDestructureAssignment = !!props.ast)) {
if (props.ast) {
;[depth, exitScope] = context.enterScope()
propsName = `_slotProps${depth}`
walkIdentifiers(
props.ast,
(id, _, __, ___, isLocal) => {
if (isLocal) idsOfProps.add(id.name)
},
true,
)
} else {
idsOfProps.add((propsName = rawProps))
propsName = props.content
}
}

const idMap: Record<string, string | null> = {}

idsOfProps.forEach(
id =>
(idMap[id] = isDestructureAssignment
? `${propsName}[${JSON.stringify(id)}]`
: null),
const idMap = buildDestructureIdMap(
idToPathMap,
propsName || '',
context.options.expressionPlugins,
)

if (propsName) {
idMap[propsName] = null
}

let blockFn = context.withId(
() => genBlock(oper, context, [propsName]),
() => genBlock(oper, context, propsName ? [propsName] : []),
idMap,
)
exitScope && exitScope()
Expand Down
Loading
Loading