Skip to content

Commit

Permalink
feat: add defineSlots macro
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Mar 30, 2023
1 parent ba9c2ae commit cae43f0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
27 changes: 26 additions & 1 deletion packages/compiler-sfc/src/compileScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ const DEFINE_EMITS = 'defineEmits'
const DEFINE_EXPOSE = 'defineExpose'
const WITH_DEFAULTS = 'withDefaults'
const DEFINE_OPTIONS = 'defineOptions'
const DEFINT_SLOTS = 'defineSlots'

// constants
const DEFAULT_VAR = `__default__`
Expand Down Expand Up @@ -299,6 +300,7 @@ export function compileScript(
let hasDefaultExportName = false
let hasDefaultExportRender = false
let hasDefineOptionsCall = false
let hasDefineSlotsCall = false
let propsRuntimeDecl: Node | undefined
let propsRuntimeDefaults: ObjectExpression | undefined
let propsDestructureDecl: Node | undefined
Expand Down Expand Up @@ -593,6 +595,26 @@ export function compileScript(
return true
}

function processDefineSlots(node: Node, declId?: LVal): boolean {
if (!isCallOf(node, DEFINT_SLOTS)) {
return false
}
if (hasDefineSlotsCall) {
error(`duplicate ${DEFINT_SLOTS}() call`, node)
}
hasDefineSlotsCall = true

if (declId) {
s.overwrite(
startOffset + node.start!,
startOffset + node.end!,
`${helper('useSlots')}()`
)
}

return true
}

function getAstBody(): Statement[] {
return scriptAst
? [...scriptSetupAst.body, ...scriptAst.body]
Expand Down Expand Up @@ -1288,7 +1310,8 @@ export function compileScript(
processDefineProps(expr) ||
processDefineEmits(expr) ||
processDefineOptions(expr) ||
processWithDefaults(expr)
processWithDefaults(expr) ||
processDefineSlots(expr)
) {
s.remove(node.start! + startOffset, node.end! + startOffset)
} else if (processDefineExpose(expr)) {
Expand Down Expand Up @@ -1323,6 +1346,8 @@ export function compileScript(
processDefineProps(init, decl.id) ||
processWithDefaults(init, decl.id, node.kind)
const isDefineEmits = processDefineEmits(init, decl.id)
processDefineSlots(init, decl.id)

if (isDefineProps || isDefineEmits) {
if (left === 1) {
s.remove(node.start! + startOffset, node.end! + startOffset)
Expand Down
12 changes: 12 additions & 0 deletions packages/runtime-core/src/apiSetupHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
ExtractPropTypes
} from './componentProps'
import { warn } from './warning'
import { VNode } from './vnode'

// dev only
const warnRuntimeUsage = (method: string) =>
Expand Down Expand Up @@ -176,6 +177,17 @@ export function defineOptions<
}
}

export function defineSlots<
T extends Record<string, any>
>(): // @ts-expect-error
{
[K in keyof T]: (scope: T[K]) => VNode[] | undefined
} {
if (__DEV__) {
warnRuntimeUsage(`defineSlots`)
}
}

type NotUndefined<T> = T extends undefined ? never : T

type InferDefaults<T> = {
Expand Down
2 changes: 2 additions & 0 deletions packages/runtime-core/types/scriptSetupHelpers.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ type _defineProps = typeof defineProps
type _defineEmits = typeof defineEmits
type _defineExpose = typeof defineExpose
type _defineOptions = typeof defineOptions
type _defineSlots = typeof defineSlots
type _withDefaults = typeof withDefaults

declare global {
const defineProps: _defineProps
const defineEmits: _defineEmits
const defineExpose: _defineExpose
const defineOptions: _defineOptions
const defineSlots: _defineSlots
const withDefaults: _withDefaults
}

0 comments on commit cae43f0

Please sign in to comment.