Skip to content

Commit

Permalink
Revert "feat(compiler-sfc): expose resolve type-based props and emits (
Browse files Browse the repository at this point in the history
…vuejs#8874)"

This reverts commit d52617f.
  • Loading branch information
johnsoncodehk committed Oct 14, 2023
1 parent dc54c59 commit 59a8c26
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 48 deletions.
3 changes: 0 additions & 3 deletions packages/compiler-sfc/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
10 changes: 3 additions & 7 deletions packages/compiler-sfc/src/script/defineEmits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -68,7 +64,7 @@ export function genRuntimeEmits(ctx: ScriptCompileContext): string | undefined {
return emitsDecl
}

export function extractRuntimeEmits(ctx: TypeResolveContext): Set<string> {
function extractRuntimeEmits(ctx: ScriptCompileContext): Set<string> {
const emits = new Set<string>()
const node = ctx.emitsTypeDecl!

Expand Down Expand Up @@ -101,7 +97,7 @@ export function extractRuntimeEmits(ctx: TypeResolveContext): Set<string> {
}

function extractEventNames(
ctx: TypeResolveContext,
ctx: ScriptCompileContext,
eventName: ArrayPattern | Identifier | ObjectPattern | RestElement,
emits: Set<string>
) {
Expand Down
22 changes: 8 additions & 14 deletions packages/compiler-sfc/src/script/defineProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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) {
Expand All @@ -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
}
}
Expand All @@ -199,7 +193,7 @@ export function extractRuntimeProps(
}

function resolveRuntimePropsFromType(
ctx: TypeResolveContext,
ctx: ScriptCompileContext,
node: Node
): PropTypeData[] {
const props: PropTypeData[] = []
Expand Down Expand Up @@ -228,7 +222,7 @@ function resolveRuntimePropsFromType(
}

function genRuntimePropFromType(
ctx: TypeResolveContext,
ctx: ScriptCompileContext,
{ key, required, type, skipCheck }: PropTypeData,
hasStaticDefaults: boolean
): string {
Expand Down Expand Up @@ -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' &&
Expand All @@ -303,7 +297,7 @@ function hasStaticWithDefaults(ctx: TypeResolveContext) {
}

function genDestructuredDefaultValue(
ctx: TypeResolveContext,
ctx: ScriptCompileContext,
key: string,
inferredType?: string[]
):
Expand Down
26 changes: 2 additions & 24 deletions packages/compiler-sfc/src/script/resolveType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<ScriptCompileContext, 'scope' | 'globalScopes' | 'deps' | 'fs'>
> & {
ast: Statement[]
options: SimpleTypeResolveOptions
}

export type TypeResolveContext = ScriptCompileContext | SimpleTypeResolveContext
Expand Down

0 comments on commit 59a8c26

Please sign in to comment.