Skip to content

Commit

Permalink
feat: add defineSlots macro support
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoxiangmoe committed Jul 30, 2023
1 parent dd9cba0 commit 3f1d393
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/core/macros.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const DEFINE_PROPS = 'defineProps'
const DEFINE_EMITS = 'defineEmits'
const DEFINE_EXPOSE = 'defineExpose'
const WITH_DEFAULTS = 'withDefaults'
const DEFINE_SLOTS = 'defineSlots'

export interface PropTypeData {
key: string
Expand All @@ -28,6 +29,7 @@ export interface PropTypeData {
export function applyMacros(nodes: Statement[]) {
let hasDefinePropsCall = false
let hasDefineEmitCall = false
let hasDefineSlotsCall = false
let propsRuntimeDecl: Node | undefined
let propsRuntimeDefaults: Node | undefined
let propsTypeDecl: TSTypeLiteral | TSInterfaceBody | undefined
Expand Down Expand Up @@ -204,6 +206,23 @@ export function applyMacros(nodes: Statement[]) {
return true
}

function processDefineSlots(
node: Node,
): boolean {
if (!isCallOf(node, DEFINE_SLOTS))
return false

if (hasDefineSlotsCall)
error(`duplicate ${DEFINE_SLOTS}() call`, node)

hasDefineSlotsCall = true

if (node.arguments.length > 0)
error(`${DEFINE_SLOTS}() cannot accept arguments`, node)

return true
}

function genRuntimeProps(props: Record<string, PropTypeData>) {
const keys = Object.keys(props)
if (!keys.length)
Expand Down Expand Up @@ -270,7 +289,9 @@ export function applyMacros(nodes: Statement[]) {
const decl = node.declarations[i]
if (decl.init) {
if (processDefineEmits(decl.init))
decl.init = t.memberExpression(t.identifier('__ctx'), t.identifier('emit')) as any
decl.init = t.memberExpression(t.identifier('__ctx'), t.identifier('emit'))
else if (processDefineSlots(decl.init))
decl.init = t.memberExpression(t.identifier('__ctx'), t.identifier('slots'))
else if (processDefineProps(decl.init) || processWithDefaults(decl.init))
decl.init = t.identifier('__props') as any
else
Expand All @@ -279,7 +300,7 @@ export function applyMacros(nodes: Statement[]) {
}
}

if (processWithDefaults(node) || processDefineEmits(node) || processDefineProps(node) || processDefineExpose(node))
if (processWithDefaults(node) || processDefineEmits(node) || processDefineProps(node) || processDefineExpose(node) || processDefineSlots(node))
return null

throwIfAwait(node)
Expand Down
30 changes: 30 additions & 0 deletions test/__snapshots__/transform.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,36 @@ export default __sfc_main;
"
`;
exports[`transform > fixtures > test/fixtures/MacrosType5.vue 1`] = `
"<script lang=\\"ts\\">
const __sfc_main = {};
export default __sfc_main;
</script>
"
`;
exports[`transform > fixtures > test/fixtures/MacrosType6.vue 1`] = `
"<script lang=\\"ts\\">
const __sfc_main = {};
__sfc_main.setup = (__props, __ctx) => {
const slots = __ctx.slots;
return {
slots
};
};
export default __sfc_main;
</script>
<template>
<template v-if=\\"slots.default\\">
<slot name=\\"default\\" msg=\\"hello\\"></slot>
</template>
<div v-else>
fallback
</div>
</template>
"
`;
exports[`transform > fixtures > test/fixtures/MacrosTypeAny.vue 1`] = `
"<template>
<div>
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/MacrosType5.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script setup lang="ts">
defineSlots<{
default: { msg: string }
}>()
</script>
13 changes: 13 additions & 0 deletions test/fixtures/MacrosType6.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<script setup lang="ts">
const slots = defineSlots<{
default: { msg: string }
}>()
</script>
<template>
<template v-if="slots.default">
<slot name="default" msg="hello"></slot>
</template>
<div v-else>
fallback
</div>
</template>

0 comments on commit 3f1d393

Please sign in to comment.