提交 f37eb25a 编写于 作者: fxy060608's avatar fxy060608

wip(uvue): 调整 defineProps 类型为 PropType 的生成

上级 eb667df3
......@@ -328,7 +328,8 @@ export default {
unknownUnion: { type: null, required: true },
unknownIntersection: { type: Object, required: true },
unknownUnionWithBoolean: { type: Boolean, required: true, skipCheck: true },
unknownUnionWithFunction: { type: Function, required: true, skipCheck: true }
unknownUnionWithFunction: { type: Function, required: true, skipCheck: true },
propType: { type: Object as PropType<()=>string>, required: true, skipCheck: true }
},
setup(__props: GenAnonymous) {
const __ins = getCurrentInstance()!;
......
......@@ -130,6 +130,9 @@ const props = defineProps({ foo: String })
unknownIntersection: UnknownType & Object
unknownUnionWithBoolean: UnknownType | boolean
unknownUnionWithFunction: UnknownType | (() => any)
propType: PropType<()=>string>
}>()
</script>`)
assertCode(content)
......@@ -188,6 +191,9 @@ const props = defineProps({ foo: String })
expect(content).toMatch(
`unknownUnionWithFunction: { type: Function, required: true, skipCheck: true }`
)
expect(content).toMatch(
`propType: { type: Object as PropType<()=>string>, required: true, skipCheck: true }`
)
expect(bindings).toStrictEqual({
string: BindingTypes.PROPS,
number: BindingTypes.PROPS,
......@@ -228,6 +234,7 @@ const props = defineProps({ foo: String })
unknownIntersection: BindingTypes.PROPS,
unknownUnionWithBoolean: BindingTypes.PROPS,
unknownUnionWithFunction: BindingTypes.PROPS,
propType: BindingTypes.PROPS,
})
})
......
......@@ -5,6 +5,8 @@ import {
ObjectMethod,
ObjectExpression,
Expression,
TSPropertySignature,
TSMethodSignature,
} from '@babel/types'
import { BindingTypes, isFunctionType } from '@vue/compiler-dom'
import { ScriptCompileContext } from './context'
......@@ -225,6 +227,32 @@ function genRuntimePropsFromTypes(ctx: ScriptCompileContext) {
return propsDecls
}
function parsePropType(
ctx: ScriptCompileContext,
node: TSMethodSignature | TSPropertySignature
) {
if (node.type === 'TSPropertySignature') {
const typeAnn = node.typeAnnotation
if (typeAnn) {
const tsType = typeAnn?.typeAnnotation
if (tsType?.type === 'TSTypeReference') {
if (
tsType.typeName.type === 'Identifier' &&
tsType.typeName.name === 'PropType'
) {
return [
`Object as ${ctx.source.slice(
tsType.start! + ctx.startOffset!,
tsType.end! + ctx.startOffset!
)}`,
]
}
}
}
}
return []
}
function resolveRuntimePropsFromType(
ctx: ScriptCompileContext,
node: Node
......@@ -233,15 +261,20 @@ function resolveRuntimePropsFromType(
const elements = resolveTypeElements(ctx, node)
for (const key in elements.props) {
const e = elements.props[key]
let type = inferRuntimeType(ctx, e)
let type = parsePropType(ctx, e)
let skipCheck = false
// skip check for result containing unknown types
if (type.includes(UNKNOWN_TYPE)) {
if (type.includes('Boolean') || type.includes('Function')) {
type = type.filter((t) => t !== UNKNOWN_TYPE)
skipCheck = true
} else {
type = ['null']
if (type.length) {
skipCheck = true
} else {
type = inferRuntimeType(ctx, e)
// skip check for result containing unknown types
if (type.includes(UNKNOWN_TYPE)) {
if (type.includes('Boolean') || type.includes('Function')) {
type = type.filter((t) => t !== UNKNOWN_TYPE)
skipCheck = true
} else {
type = ['null']
}
}
}
props.push({
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册