Skip to content

Commit 5191dbb

Browse files
committed
feat(compiler-sfc): expose resolve type-based props and emits (#8874)
1 parent b12498f commit 5191dbb

File tree

4 files changed

+48
-13
lines changed

4 files changed

+48
-13
lines changed

packages/compiler-sfc/src/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ export {
3333

3434
// Internals for type resolution
3535
export { invalidateTypeCache, registerTS } from './script/resolveType'
36+
export { extractRuntimeProps } from './script/defineProps'
37+
export { extractRuntimeEmits } from './script/defineEmits'
3638

3739
// Types
3840
export type {
@@ -58,6 +60,7 @@ export type { SFCScriptCompileOptions } from './compileScript'
5860
export type { ScriptCompileContext } from './script/context'
5961
export type {
6062
TypeResolveContext,
63+
SimpleTypeResolveOptions,
6164
SimpleTypeResolveContext
6265
} from './script/resolveType'
6366
export type {

packages/compiler-sfc/src/script/defineEmits.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ import {
88
} from '@babel/types'
99
import { isCallOf } from './utils'
1010
import { ScriptCompileContext } from './context'
11-
import { resolveTypeElements, resolveUnionType } from './resolveType'
11+
import {
12+
TypeResolveContext,
13+
resolveTypeElements,
14+
resolveUnionType
15+
} from './resolveType'
1216

1317
export const DEFINE_EMITS = 'defineEmits'
1418

@@ -64,7 +68,7 @@ export function genRuntimeEmits(ctx: ScriptCompileContext): string | undefined {
6468
return emitsDecl
6569
}
6670

67-
function extractRuntimeEmits(ctx: ScriptCompileContext): Set<string> {
71+
export function extractRuntimeEmits(ctx: TypeResolveContext): Set<string> {
6872
const emits = new Set<string>()
6973
const node = ctx.emitsTypeDecl!
7074

@@ -97,7 +101,7 @@ function extractRuntimeEmits(ctx: ScriptCompileContext): Set<string> {
97101
}
98102

99103
function extractEventNames(
100-
ctx: ScriptCompileContext,
104+
ctx: TypeResolveContext,
101105
eventName: ArrayPattern | Identifier | ObjectPattern | RestElement,
102106
emits: Set<string>
103107
) {

packages/compiler-sfc/src/script/defineProps.ts

+14-8
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ import {
88
} from '@babel/types'
99
import { BindingTypes, isFunctionType } from '@vue/compiler-dom'
1010
import { ScriptCompileContext } from './context'
11-
import { inferRuntimeType, resolveTypeElements } from './resolveType'
11+
import {
12+
TypeResolveContext,
13+
inferRuntimeType,
14+
resolveTypeElements
15+
} from './resolveType'
1216
import {
1317
resolveObjectKey,
1418
UNKNOWN_TYPE,
@@ -150,7 +154,7 @@ export function genRuntimeProps(ctx: ScriptCompileContext): string | undefined {
150154
}
151155
}
152156
} else if (ctx.propsTypeDecl) {
153-
propsDecls = genRuntimePropsFromTypes(ctx)
157+
propsDecls = extractRuntimeProps(ctx)
154158
}
155159

156160
const modelsDecls = genModelProps(ctx)
@@ -162,7 +166,9 @@ export function genRuntimeProps(ctx: ScriptCompileContext): string | undefined {
162166
}
163167
}
164168

165-
function genRuntimePropsFromTypes(ctx: ScriptCompileContext) {
169+
export function extractRuntimeProps(
170+
ctx: TypeResolveContext
171+
): string | undefined {
166172
// this is only called if propsTypeDecl exists
167173
const props = resolveRuntimePropsFromType(ctx, ctx.propsTypeDecl!)
168174
if (!props.length) {
@@ -175,7 +181,7 @@ function genRuntimePropsFromTypes(ctx: ScriptCompileContext) {
175181
for (const prop of props) {
176182
propStrings.push(genRuntimePropFromType(ctx, prop, hasStaticDefaults))
177183
// register bindings
178-
if (!(prop.key in ctx.bindingMetadata)) {
184+
if ('bindingMetadata' in ctx && !(prop.key in ctx.bindingMetadata)) {
179185
ctx.bindingMetadata[prop.key] = BindingTypes.PROPS
180186
}
181187
}
@@ -193,7 +199,7 @@ function genRuntimePropsFromTypes(ctx: ScriptCompileContext) {
193199
}
194200

195201
function resolveRuntimePropsFromType(
196-
ctx: ScriptCompileContext,
202+
ctx: TypeResolveContext,
197203
node: Node
198204
): PropTypeData[] {
199205
const props: PropTypeData[] = []
@@ -222,7 +228,7 @@ function resolveRuntimePropsFromType(
222228
}
223229

224230
function genRuntimePropFromType(
225-
ctx: ScriptCompileContext,
231+
ctx: TypeResolveContext,
226232
{ key, required, type, skipCheck }: PropTypeData,
227233
hasStaticDefaults: boolean
228234
): string {
@@ -284,7 +290,7 @@ function genRuntimePropFromType(
284290
* static properties, we can directly generate more optimized default
285291
* declarations. Otherwise we will have to fallback to runtime merging.
286292
*/
287-
function hasStaticWithDefaults(ctx: ScriptCompileContext) {
293+
function hasStaticWithDefaults(ctx: TypeResolveContext) {
288294
return !!(
289295
ctx.propsRuntimeDefaults &&
290296
ctx.propsRuntimeDefaults.type === 'ObjectExpression' &&
@@ -297,7 +303,7 @@ function hasStaticWithDefaults(ctx: ScriptCompileContext) {
297303
}
298304

299305
function genDestructuredDefaultValue(
300-
ctx: ScriptCompileContext,
306+
ctx: TypeResolveContext,
301307
key: string,
302308
inferredType?: string[]
303309
):

packages/compiler-sfc/src/script/resolveType.ts

+24-2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ import type TS from 'typescript'
4242
import { extname, dirname } from 'path'
4343
import { minimatch as isMatch } from 'minimatch'
4444

45+
export type SimpleTypeResolveOptions = Partial<
46+
Pick<
47+
SFCScriptCompileOptions,
48+
'globalTypeFiles' | 'fs' | 'babelParserPlugins' | 'isProd'
49+
>
50+
>
51+
4552
/**
4653
* TypeResolveContext is compatible with ScriptCompileContext
4754
* but also allows a simpler version of it with minimal required properties
@@ -59,13 +66,28 @@ import { minimatch as isMatch } from 'minimatch'
5966
*/
6067
export type SimpleTypeResolveContext = Pick<
6168
ScriptCompileContext,
62-
// required
63-
'source' | 'filename' | 'error' | 'options'
69+
// file
70+
| 'source'
71+
| 'filename'
72+
73+
// utils
74+
| 'error'
75+
| 'helper'
76+
| 'getString'
77+
78+
// props
79+
| 'propsTypeDecl'
80+
| 'propsRuntimeDefaults'
81+
| 'propsDestructuredBindings'
82+
83+
// emits
84+
| 'emitsTypeDecl'
6485
> &
6586
Partial<
6687
Pick<ScriptCompileContext, 'scope' | 'globalScopes' | 'deps' | 'fs'>
6788
> & {
6889
ast: Statement[]
90+
options: SimpleTypeResolveOptions
6991
}
7092

7193
export type TypeResolveContext = ScriptCompileContext | SimpleTypeResolveContext

0 commit comments

Comments
 (0)