From 59a8c26e60d9681d4755b5db9d32f08662d0af52 Mon Sep 17 00:00:00 2001 From: Johnson Chu Date: Sat, 14 Oct 2023 16:31:47 +0800 Subject: [PATCH] Revert "feat(compiler-sfc): expose resolve type-based props and emits (#8874)" This reverts commit d52617fc15fcb48a47a12e0d7f57d34a2cc6bcb5. --- packages/compiler-sfc/src/index.ts | 3 --- .../compiler-sfc/src/script/defineEmits.ts | 10 +++---- .../compiler-sfc/src/script/defineProps.ts | 22 ++++++---------- .../compiler-sfc/src/script/resolveType.ts | 26 ++----------------- 4 files changed, 13 insertions(+), 48 deletions(-) diff --git a/packages/compiler-sfc/src/index.ts b/packages/compiler-sfc/src/index.ts index c6ee604146e..76b4900d46d 100644 --- a/packages/compiler-sfc/src/index.ts +++ b/packages/compiler-sfc/src/index.ts @@ -33,8 +33,6 @@ export { // Internals for type resolution export { invalidateTypeCache, registerTS } from './script/resolveType' -export { extractRuntimeProps } from './script/defineProps' -export { extractRuntimeEmits } from './script/defineEmits' // Types export type { @@ -60,7 +58,6 @@ export type { SFCScriptCompileOptions } from './compileScript' export type { ScriptCompileContext } from './script/context' export type { TypeResolveContext, - SimpleTypeResolveOptions, SimpleTypeResolveContext } from './script/resolveType' export type { diff --git a/packages/compiler-sfc/src/script/defineEmits.ts b/packages/compiler-sfc/src/script/defineEmits.ts index b7453076cfe..8bd4cdfe543 100644 --- a/packages/compiler-sfc/src/script/defineEmits.ts +++ b/packages/compiler-sfc/src/script/defineEmits.ts @@ -8,11 +8,7 @@ import { } from '@babel/types' import { isCallOf } from './utils' import { ScriptCompileContext } from './context' -import { - TypeResolveContext, - resolveTypeElements, - resolveUnionType -} from './resolveType' +import { resolveTypeElements, resolveUnionType } from './resolveType' export const DEFINE_EMITS = 'defineEmits' @@ -68,7 +64,7 @@ export function genRuntimeEmits(ctx: ScriptCompileContext): string | undefined { return emitsDecl } -export function extractRuntimeEmits(ctx: TypeResolveContext): Set { +function extractRuntimeEmits(ctx: ScriptCompileContext): Set { const emits = new Set() const node = ctx.emitsTypeDecl! @@ -101,7 +97,7 @@ export function extractRuntimeEmits(ctx: TypeResolveContext): Set { } function extractEventNames( - ctx: TypeResolveContext, + ctx: ScriptCompileContext, eventName: ArrayPattern | Identifier | ObjectPattern | RestElement, emits: Set ) { diff --git a/packages/compiler-sfc/src/script/defineProps.ts b/packages/compiler-sfc/src/script/defineProps.ts index 449ed250d1d..5004e314da1 100644 --- a/packages/compiler-sfc/src/script/defineProps.ts +++ b/packages/compiler-sfc/src/script/defineProps.ts @@ -8,11 +8,7 @@ import { } from '@babel/types' import { BindingTypes, isFunctionType } from '@vue/compiler-dom' import { ScriptCompileContext } from './context' -import { - TypeResolveContext, - inferRuntimeType, - resolveTypeElements -} from './resolveType' +import { inferRuntimeType, resolveTypeElements } from './resolveType' import { resolveObjectKey, UNKNOWN_TYPE, @@ -154,7 +150,7 @@ export function genRuntimeProps(ctx: ScriptCompileContext): string | undefined { } } } else if (ctx.propsTypeDecl) { - propsDecls = extractRuntimeProps(ctx) + propsDecls = genRuntimePropsFromTypes(ctx) } const modelsDecls = genModelProps(ctx) @@ -166,9 +162,7 @@ export function genRuntimeProps(ctx: ScriptCompileContext): string | undefined { } } -export function extractRuntimeProps( - ctx: TypeResolveContext -): string | undefined { +function genRuntimePropsFromTypes(ctx: ScriptCompileContext) { // this is only called if propsTypeDecl exists const props = resolveRuntimePropsFromType(ctx, ctx.propsTypeDecl!) if (!props.length) { @@ -181,7 +175,7 @@ export function extractRuntimeProps( for (const prop of props) { propStrings.push(genRuntimePropFromType(ctx, prop, hasStaticDefaults)) // register bindings - if ('bindingMetadata' in ctx && !(prop.key in ctx.bindingMetadata)) { + if (!(prop.key in ctx.bindingMetadata)) { ctx.bindingMetadata[prop.key] = BindingTypes.PROPS } } @@ -199,7 +193,7 @@ export function extractRuntimeProps( } function resolveRuntimePropsFromType( - ctx: TypeResolveContext, + ctx: ScriptCompileContext, node: Node ): PropTypeData[] { const props: PropTypeData[] = [] @@ -228,7 +222,7 @@ function resolveRuntimePropsFromType( } function genRuntimePropFromType( - ctx: TypeResolveContext, + ctx: ScriptCompileContext, { key, required, type, skipCheck }: PropTypeData, hasStaticDefaults: boolean ): string { @@ -290,7 +284,7 @@ function genRuntimePropFromType( * static properties, we can directly generate more optimized default * declarations. Otherwise we will have to fallback to runtime merging. */ -function hasStaticWithDefaults(ctx: TypeResolveContext) { +function hasStaticWithDefaults(ctx: ScriptCompileContext) { return !!( ctx.propsRuntimeDefaults && ctx.propsRuntimeDefaults.type === 'ObjectExpression' && @@ -303,7 +297,7 @@ function hasStaticWithDefaults(ctx: TypeResolveContext) { } function genDestructuredDefaultValue( - ctx: TypeResolveContext, + ctx: ScriptCompileContext, key: string, inferredType?: string[] ): diff --git a/packages/compiler-sfc/src/script/resolveType.ts b/packages/compiler-sfc/src/script/resolveType.ts index b298f9a8e82..78581432366 100644 --- a/packages/compiler-sfc/src/script/resolveType.ts +++ b/packages/compiler-sfc/src/script/resolveType.ts @@ -42,13 +42,6 @@ import type TS from 'typescript' 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 @@ -66,28 +59,13 @@ export type SimpleTypeResolveOptions = Partial< */ export type SimpleTypeResolveContext = Pick< ScriptCompileContext, - // file - | 'source' - | 'filename' - - // utils - | 'error' - | 'helper' - | 'getString' - - // props - | 'propsTypeDecl' - | 'propsRuntimeDefaults' - | 'propsDestructuredBindings' - - // emits - | 'emitsTypeDecl' + // required + 'source' | 'filename' | 'error' | 'options' > & Partial< Pick > & { ast: Statement[] - options: SimpleTypeResolveOptions } export type TypeResolveContext = ScriptCompileContext | SimpleTypeResolveContext