Skip to content

Commit

Permalink
fix: generate null props when type is failed to infer, close #30
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Aug 31, 2021
1 parent 947d219 commit 29f6f7b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 18 deletions.
16 changes: 9 additions & 7 deletions src/core/macros.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const WITH_DEFAULTS = 'withDefaults'

export interface PropTypeData {
key: string
type: string[]
type: string[] | string
required: boolean
}

Expand Down Expand Up @@ -211,17 +211,19 @@ export function applyMacros(nodes: Statement[]) {

return t.objectExpression(
Object.entries(props).map(([key, value]) => {
if (value.type === 'null')
return t.objectProperty(t.identifier(key), t.nullLiteral())

const prop = hasStaticDefaults
? (propsRuntimeDefaults as ObjectExpression).properties.find((node: any) => node.key.name === key) as ObjectProperty
: undefined

if (prop)
value.required = false

const entries = Object.entries(value).map(([key, value]) =>
key === 'type'
? t.objectProperty(t.identifier(key), t.arrayExpression(value.map((i: any) => t.identifier(i))) as any)
: t.objectProperty(t.identifier(key), parseExpression(JSON.stringify(value)) as any),
const entries = Object.entries(value).map(([key, value]) => key === 'type'
? t.objectProperty(t.identifier(key), typeof value === 'string' ? t.identifier(value) : t.arrayExpression(value.map((i: any) => t.identifier(i))) as any)
: t.objectProperty(t.identifier(key), parseExpression(JSON.stringify(value)) as any),
)

if (prop)
Expand Down Expand Up @@ -311,7 +313,7 @@ function extractRuntimeProps(
(m.type === 'TSPropertySignature' || m.type === 'TSMethodSignature')
&& m.key.type === 'Identifier'
) {
let type
let type: string[] | undefined
if (m.type === 'TSMethodSignature') {
type = ['Function']
}
Expand All @@ -324,7 +326,7 @@ function extractRuntimeProps(
props[m.key.name] = {
key: m.key.name,
required: !m.optional,
type: type || ['null'],
type: type?.length === 1 ? type[0] : type || 'null',
}
}
}
Expand Down
41 changes: 30 additions & 11 deletions test/__snapshots__/transform.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ __sfc_main.props = {
msg: {
key: \\"msg\\",
required: false,
type: [String],
type: String,
default: 'Hello'
},
name: {
Expand Down Expand Up @@ -221,7 +221,7 @@ __sfc_main.props = {
msg: {
key: \\"msg\\",
required: false,
type: [String],
type: String,
default: 'Hello'
},
value: {
Expand All @@ -232,7 +232,7 @@ __sfc_main.props = {
data: {
key: \\"data\\",
required: false,
type: [Object]
type: Object
}
};
Expand Down Expand Up @@ -272,7 +272,7 @@ __sfc_main.props = {
msg: {
key: \\"msg\\",
required: false,
type: [String],
type: String,
default: 'Hello'
},
value: {
Expand All @@ -283,18 +283,14 @@ __sfc_main.props = {
data: {
key: \\"data\\",
required: false,
type: [Object]
type: Object
},
arr: {
key: \\"arr\\",
required: false,
type: [Array]
type: Array
},
any: {
key: \\"any\\",
required: true,
type: [null]
}
any: null
};
__sfc_main.setup = (__props, __ctx) => {
Expand All @@ -311,6 +307,29 @@ export default __sfc_main;
"
`;
exports[`transform fixtures test/fixtures/MacrosTypeAny.vue 1`] = `
"<template>
<div>
{{ value }}
</div>
</template>
<script lang=\\"ts\\">
const __sfc_main = {};
__sfc_main.props = {
value: null
};
__sfc_main.setup = (__props, __ctx) => {
const props = __props;
return {};
};
export default __sfc_main;
</script>
"
`;
exports[`transform fixtures test/fixtures/Object1.vue 1`] = `
"<template>
<div>
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/MacrosTypeAny.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<template>
<div>
{{ value }}
</div>
</template>

<script setup lang="ts">
const props = defineProps<{ value: any }>()
</script>

0 comments on commit 29f6f7b

Please sign in to comment.