Skip to content

Commit

Permalink
refactor: adjust ResolvedElements shape
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Apr 13, 2023
1 parent 1cfab4c commit 51773d5
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 84 deletions.
42 changes: 21 additions & 21 deletions packages/compiler-sfc/__tests__/compileScript/resolveType.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,27 @@ import {

describe('resolveType', () => {
test('type literal', () => {
const { elements, callSignatures } = resolve(`type Target = {
const { props, calls } = resolve(`type Target = {
foo: number // property
bar(): void // method
'baz': string // string literal key
(e: 'foo'): void // call signature
(e: 'bar'): void
}`)
expect(elements).toStrictEqual({
expect(props).toStrictEqual({
foo: ['Number'],
bar: ['Function'],
baz: ['String']
})
expect(callSignatures?.length).toBe(2)
expect(calls?.length).toBe(2)
})

test('reference type', () => {
expect(
resolve(`
type Aliased = { foo: number }
type Target = Aliased
`).elements
`).props
).toStrictEqual({
foo: ['Number']
})
Expand All @@ -39,7 +39,7 @@ describe('resolveType', () => {
resolve(`
export type Aliased = { foo: number }
type Target = Aliased
`).elements
`).props
).toStrictEqual({
foo: ['Number']
})
Expand All @@ -50,7 +50,7 @@ describe('resolveType', () => {
resolve(`
interface Aliased { foo: number }
type Target = Aliased
`).elements
`).props
).toStrictEqual({
foo: ['Number']
})
Expand All @@ -61,7 +61,7 @@ describe('resolveType', () => {
resolve(`
export interface Aliased { foo: number }
type Target = Aliased
`).elements
`).props
).toStrictEqual({
foo: ['Number']
})
Expand All @@ -75,7 +75,7 @@ describe('resolveType', () => {
interface C { c: string }
interface Aliased extends B, C { foo: number }
type Target = Aliased
`).elements
`).props
).toStrictEqual({
a: ['Function'],
b: ['Boolean'],
Expand All @@ -88,7 +88,7 @@ describe('resolveType', () => {
expect(
resolve(`
type Target = (e: 'foo') => void
`).callSignatures?.length
`).calls?.length
).toBe(1)
})

Expand All @@ -97,7 +97,7 @@ describe('resolveType', () => {
resolve(`
type Fn = (e: 'foo') => void
type Target = Fn
`).callSignatures?.length
`).calls?.length
).toBe(1)
})

Expand All @@ -108,7 +108,7 @@ describe('resolveType', () => {
type Bar = { bar: string }
type Baz = { bar: string | boolean }
type Target = { self: any } & Foo & Bar & Baz
`).elements
`).props
).toStrictEqual({
self: ['Unknown'],
foo: ['Number'],
Expand Down Expand Up @@ -138,7 +138,7 @@ describe('resolveType', () => {
}
type Target = CommonProps & ConditionalProps
`).elements
`).props
).toStrictEqual({
size: ['String'],
color: ['String', 'Number'],
Expand All @@ -155,7 +155,7 @@ describe('resolveType', () => {
type Target = {
[\`_\${T}_\${S}_\`]: string
}
`).elements
`).props
).toStrictEqual({
_foo_x_: ['String'],
_foo_y_: ['String'],
Expand All @@ -177,7 +177,7 @@ describe('resolveType', () => {
} & {
[K in \`x\${T}\`]: string
}
`).elements
`).props
).toStrictEqual({
foo: ['String', 'Number'],
bar: ['String', 'Number'],
Expand All @@ -196,7 +196,7 @@ describe('resolveType', () => {
type T = { foo: number, bar: string, baz: boolean }
type K = 'foo' | 'bar'
type Target = Pick<T, K>
`).elements
`).props
).toStrictEqual({
foo: ['Number'],
bar: ['String']
Expand All @@ -209,7 +209,7 @@ describe('resolveType', () => {
type T = { foo: number, bar: string, baz: boolean }
type K = 'foo' | 'bar'
type Target = Omit<T, K>
`).elements
`).props
).toStrictEqual({
baz: ['Boolean']
})
Expand All @@ -231,13 +231,13 @@ function resolve(code: string) {
s => s.type === 'TSTypeAliasDeclaration' && s.id.name === 'Target'
) as TSTypeAliasDeclaration
const raw = resolveTypeElements(ctx, targetDecl.typeAnnotation)
const elements: Record<string, string[]> = {}
for (const key in raw) {
elements[key] = inferRuntimeType(ctx, raw[key])
const props: Record<string, string[]> = {}
for (const key in raw.props) {
props[key] = inferRuntimeType(ctx, raw.props[key])
}
return {
elements,
callSignatures: raw.__callSignatures,
props,
calls: raw.calls,
raw
}
}
8 changes: 4 additions & 4 deletions packages/compiler-sfc/src/script/defineEmits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,22 @@ function extractRuntimeEmits(ctx: ScriptCompileContext): Set<string> {
return emits
}

const elements = resolveTypeElements(ctx, node)
const { props, calls } = resolveTypeElements(ctx, node)

let hasProperty = false
for (const key in elements) {
for (const key in props) {
emits.add(key)
hasProperty = true
}

if (elements.__callSignatures) {
if (calls) {
if (hasProperty) {
ctx.error(
`defineEmits() type cannot mixed call signature and property syntax.`,
node
)
}
for (const call of elements.__callSignatures) {
for (const call of calls) {
extractEventNames(call.parameters[0], emits)
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/compiler-sfc/src/script/defineProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ function resolveRuntimePropsFromType(
): PropTypeData[] {
const props: PropTypeData[] = []
const elements = resolveTypeElements(ctx, node)
for (const key in elements) {
const e = elements[key]
for (const key in elements.props) {
const e = elements.props[key]
let type = inferRuntimeType(ctx, e)
let skipCheck = false
// skip check for result containing unknown types
Expand Down
88 changes: 31 additions & 57 deletions packages/compiler-sfc/src/script/resolveType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { UNKNOWN_TYPE } from './utils'
import { ScriptCompileContext } from './context'
import { ImportBinding } from '../compileScript'
import { TSInterfaceDeclaration } from '@babel/types'
import { capitalize, hasOwn, isArray } from '@vue/shared'
import { capitalize, hasOwn } from '@vue/shared'
import { Expression } from '@babel/types'

export interface TypeScope {
Expand All @@ -28,11 +28,9 @@ export interface TypeScope {
types: Record<string, Node>
}

type ResolvedElements = Record<
string,
TSPropertySignature | TSMethodSignature
> & {
__callSignatures?: (TSCallSignatureDeclaration | TSFunctionType)[]
interface ResolvedElements {
props: Record<string, TSPropertySignature | TSMethodSignature>
calls?: (TSCallSignatureDeclaration | TSFunctionType)[]
}

/**
Expand Down Expand Up @@ -62,9 +60,7 @@ function innerResolveTypeElements(
case 'TSParenthesizedType':
return resolveTypeElements(ctx, node.typeAnnotation)
case 'TSFunctionType': {
const ret: ResolvedElements = {}
addCallSignature(ret, node)
return ret
return { props: {}, calls: [node] }
}
case 'TSExpressionWithTypeArguments': // referenced by interface extends
case 'TSTypeReference': {
Expand Down Expand Up @@ -98,32 +94,11 @@ function innerResolveTypeElements(
ctx.error(`Unsupported type in SFC macro: ${node.type}`, node)
}

function addCallSignature(
elements: ResolvedElements,
node:
| TSCallSignatureDeclaration
| TSFunctionType
| (TSCallSignatureDeclaration | TSFunctionType)[]
) {
if (!elements.__callSignatures) {
Object.defineProperty(elements, '__callSignatures', {
enumerable: false,
value: isArray(node) ? node : [node]
})
} else {
if (isArray(node)) {
elements.__callSignatures.push(...node)
} else {
elements.__callSignatures.push(node)
}
}
}

function typeElementsToMap(
ctx: ScriptCompileContext,
elements: TSTypeElement[]
): ResolvedElements {
const ret: ResolvedElements = {}
const res: ResolvedElements = { props: {} }
for (const e of elements) {
if (e.type === 'TSPropertySignature' || e.type === 'TSMethodSignature') {
const name =
Expand All @@ -133,10 +108,10 @@ function typeElementsToMap(
? e.key.value
: null
if (name && !e.computed) {
ret[name] = e
res.props[name] = e
} else if (e.key.type === 'TemplateLiteral') {
for (const key of resolveTemplateKeys(ctx, e.key)) {
ret[key] = e
res.props[key] = e
}
} else {
ctx.error(
Expand All @@ -145,31 +120,32 @@ function typeElementsToMap(
)
}
} else if (e.type === 'TSCallSignatureDeclaration') {
addCallSignature(ret, e)
;(res.calls || (res.calls = [])).push(e)
}
}
return ret
return res
}

function mergeElements(
maps: ResolvedElements[],
type: 'TSUnionType' | 'TSIntersectionType'
): ResolvedElements {
const res: ResolvedElements = Object.create(null)
for (const m of maps) {
for (const key in m) {
if (!(key in res)) {
res[key] = m[key]
const res: ResolvedElements = { props: {} }
const { props: baseProps } = res
for (const { props, calls } of maps) {
for (const key in props) {
if (!hasOwn(baseProps, key)) {
baseProps[key] = props[key]
} else {
res[key] = createProperty(res[key].key, {
baseProps[key] = createProperty(baseProps[key].key, {
type,
// @ts-ignore
types: [res[key], m[key]]
types: [baseProps[key], props[key]]
})
}
}
if (m.__callSignatures) {
addCallSignature(res, m.__callSignatures)
if (calls) {
;(res.calls || (res.calls = [])).push(...calls)
}
}
return res
Expand Down Expand Up @@ -197,10 +173,10 @@ function resolveInterfaceMembers(
const base = typeElementsToMap(ctx, node.body.body)
if (node.extends) {
for (const ext of node.extends) {
const resolvedExt = resolveTypeElements(ctx, ext)
for (const key in resolvedExt) {
if (!hasOwn(base, key)) {
base[key] = resolvedExt[key]
const { props } = resolveTypeElements(ctx, ext)
for (const key in props) {
if (!hasOwn(base.props, key)) {
base.props[key] = props[key]
}
}
}
Expand All @@ -212,13 +188,13 @@ function resolveMappedType(
ctx: ScriptCompileContext,
node: TSMappedType
): ResolvedElements {
const res: ResolvedElements = {}
const res: ResolvedElements = { props: {} }
if (!node.typeParameter.constraint) {
ctx.error(`mapped type used in macros must have a finite constraint.`, node)
}
const keys = resolveStringType(ctx, node.typeParameter.constraint)
for (const key of keys) {
res[key] = createProperty(
res.props[key] = createProperty(
{
type: 'Identifier',
name: key
Expand Down Expand Up @@ -323,20 +299,18 @@ function resolveBuiltin(
return t
case 'Pick': {
const picked = resolveStringType(ctx, node.typeParameters!.params[1])
const res: ResolvedElements = {}
if (t.__callSignatures) addCallSignature(res, t.__callSignatures)
const res: ResolvedElements = { props: {}, calls: t.calls }
for (const key of picked) {
res[key] = t[key]
res.props[key] = t.props[key]
}
return res
}
case 'Omit':
const omitted = resolveStringType(ctx, node.typeParameters!.params[1])
const res: ResolvedElements = {}
if (t.__callSignatures) addCallSignature(res, t.__callSignatures)
for (const key in t) {
const res: ResolvedElements = { props: {}, calls: t.calls }
for (const key in t.props) {
if (!omitted.includes(key)) {
res[key] = t[key]
res.props[key] = t.props[key]
}
}
return res
Expand Down

0 comments on commit 51773d5

Please sign in to comment.