// Internals for type resolution
export { invalidateTypeCache, registerTS } from './script/resolveType'
+export { extractRuntimeProps } from './script/defineProps'
+export { extractRuntimeEmits } from './script/defineEmits'
// Types
export type {
export type { ScriptCompileContext } from './script/context'
export type {
TypeResolveContext,
+ SimpleTypeResolveOptions,
SimpleTypeResolveContext
} from './script/resolveType'
export type {
} from '@babel/types'
import { isCallOf } from './utils'
import { ScriptCompileContext } from './context'
-import { resolveTypeElements, resolveUnionType } from './resolveType'
+import {
+ TypeResolveContext,
+ resolveTypeElements,
+ resolveUnionType
+} from './resolveType'
export const DEFINE_EMITS = 'defineEmits'
return emitsDecl
}
-function extractRuntimeEmits(ctx: ScriptCompileContext): Set<string> {
+export function extractRuntimeEmits(ctx: TypeResolveContext): Set<string> {
const emits = new Set<string>()
const node = ctx.emitsTypeDecl!
}
function extractEventNames(
- ctx: ScriptCompileContext,
+ ctx: TypeResolveContext,
eventName: ArrayPattern | Identifier | ObjectPattern | RestElement,
emits: Set<string>
) {
} from '@babel/types'
import { BindingTypes, isFunctionType } from '@vue/compiler-dom'
import { ScriptCompileContext } from './context'
-import { inferRuntimeType, resolveTypeElements } from './resolveType'
+import {
+ TypeResolveContext,
+ inferRuntimeType,
+ resolveTypeElements
+} from './resolveType'
import {
resolveObjectKey,
UNKNOWN_TYPE,
}
}
} else if (ctx.propsTypeDecl) {
- propsDecls = genRuntimePropsFromTypes(ctx)
+ propsDecls = extractRuntimeProps(ctx)
}
const modelsDecls = genModelProps(ctx)
}
}
-function genRuntimePropsFromTypes(ctx: ScriptCompileContext) {
+export function extractRuntimeProps(
+ ctx: TypeResolveContext
+): string | undefined {
// this is only called if propsTypeDecl exists
const props = resolveRuntimePropsFromType(ctx, ctx.propsTypeDecl!)
if (!props.length) {
for (const prop of props) {
propStrings.push(genRuntimePropFromType(ctx, prop, hasStaticDefaults))
// register bindings
- if (!(prop.key in ctx.bindingMetadata)) {
+ if ('bindingMetadata' in ctx && !(prop.key in ctx.bindingMetadata)) {
ctx.bindingMetadata[prop.key] = BindingTypes.PROPS
}
}
}
function resolveRuntimePropsFromType(
- ctx: ScriptCompileContext,
+ ctx: TypeResolveContext,
node: Node
): PropTypeData[] {
const props: PropTypeData[] = []
}
function genRuntimePropFromType(
- ctx: ScriptCompileContext,
+ ctx: TypeResolveContext,
{ key, required, type, skipCheck }: PropTypeData,
hasStaticDefaults: boolean
): string {
* static properties, we can directly generate more optimized default
* declarations. Otherwise we will have to fallback to runtime merging.
*/
-function hasStaticWithDefaults(ctx: ScriptCompileContext) {
+function hasStaticWithDefaults(ctx: TypeResolveContext) {
return !!(
ctx.propsRuntimeDefaults &&
ctx.propsRuntimeDefaults.type === 'ObjectExpression' &&
}
function genDestructuredDefaultValue(
- ctx: ScriptCompileContext,
+ ctx: TypeResolveContext,
key: string,
inferredType?: string[]
):
import { extname, dirname } from 'path'
import { minimatch as isMatch } from 'minimatch'
+export type SimpleTypeResolveOptions = Partial<
+ Pick<
+ SFCScriptCompileOptions,
+ 'globalTypeFiles' | 'fs' | 'babelParserPlugins' | 'isProd'
+ >
+>
+
/**
* TypeResolveContext is compatible with ScriptCompileContext
* but also allows a simpler version of it with minimal required properties
*/
export type SimpleTypeResolveContext = Pick<
ScriptCompileContext,
- // required
- 'source' | 'filename' | 'error' | 'options'
+ // file
+ | 'source'
+ | 'filename'
+
+ // utils
+ | 'error'
+ | 'helper'
+ | 'getString'
+
+ // props
+ | 'propsTypeDecl'
+ | 'propsRuntimeDefaults'
+ | 'propsDestructuredBindings'
+
+ // emits
+ | 'emitsTypeDecl'
> &
Partial<
Pick<ScriptCompileContext, 'scope' | 'globalScopes' | 'deps' | 'fs'>
> & {
ast: Statement[]
+ options: SimpleTypeResolveOptions
}
export type TypeResolveContext = ScriptCompileContext | SimpleTypeResolveContext