Skip to content
This repository was archived by the owner on Dec 25, 2024. It is now read-only.

Commit 29f6f7b

Browse files
committed
fix: generate null props when type is failed to infer, close #30
1 parent 947d219 commit 29f6f7b

File tree

3 files changed

+48
-18
lines changed

3 files changed

+48
-18
lines changed

src/core/macros.ts

+9-7
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const WITH_DEFAULTS = 'withDefaults'
2222

2323
export interface PropTypeData {
2424
key: string
25-
type: string[]
25+
type: string[] | string
2626
required: boolean
2727
}
2828

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

212212
return t.objectExpression(
213213
Object.entries(props).map(([key, value]) => {
214+
if (value.type === 'null')
215+
return t.objectProperty(t.identifier(key), t.nullLiteral())
216+
214217
const prop = hasStaticDefaults
215218
? (propsRuntimeDefaults as ObjectExpression).properties.find((node: any) => node.key.name === key) as ObjectProperty
216219
: undefined
217220

218221
if (prop)
219222
value.required = false
220223

221-
const entries = Object.entries(value).map(([key, value]) =>
222-
key === 'type'
223-
? t.objectProperty(t.identifier(key), t.arrayExpression(value.map((i: any) => t.identifier(i))) as any)
224-
: t.objectProperty(t.identifier(key), parseExpression(JSON.stringify(value)) as any),
224+
const entries = Object.entries(value).map(([key, value]) => key === 'type'
225+
? t.objectProperty(t.identifier(key), typeof value === 'string' ? t.identifier(value) : t.arrayExpression(value.map((i: any) => t.identifier(i))) as any)
226+
: t.objectProperty(t.identifier(key), parseExpression(JSON.stringify(value)) as any),
225227
)
226228

227229
if (prop)
@@ -311,7 +313,7 @@ function extractRuntimeProps(
311313
(m.type === 'TSPropertySignature' || m.type === 'TSMethodSignature')
312314
&& m.key.type === 'Identifier'
313315
) {
314-
let type
316+
let type: string[] | undefined
315317
if (m.type === 'TSMethodSignature') {
316318
type = ['Function']
317319
}
@@ -324,7 +326,7 @@ function extractRuntimeProps(
324326
props[m.key.name] = {
325327
key: m.key.name,
326328
required: !m.optional,
327-
type: type || ['null'],
329+
type: type?.length === 1 ? type[0] : type || 'null',
328330
}
329331
}
330332
}

test/__snapshots__/transform.test.ts.snap

+30-11
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ __sfc_main.props = {
6969
msg: {
7070
key: \\"msg\\",
7171
required: false,
72-
type: [String],
72+
type: String,
7373
default: 'Hello'
7474
},
7575
name: {
@@ -221,7 +221,7 @@ __sfc_main.props = {
221221
msg: {
222222
key: \\"msg\\",
223223
required: false,
224-
type: [String],
224+
type: String,
225225
default: 'Hello'
226226
},
227227
value: {
@@ -232,7 +232,7 @@ __sfc_main.props = {
232232
data: {
233233
key: \\"data\\",
234234
required: false,
235-
type: [Object]
235+
type: Object
236236
}
237237
};
238238
@@ -272,7 +272,7 @@ __sfc_main.props = {
272272
msg: {
273273
key: \\"msg\\",
274274
required: false,
275-
type: [String],
275+
type: String,
276276
default: 'Hello'
277277
},
278278
value: {
@@ -283,18 +283,14 @@ __sfc_main.props = {
283283
data: {
284284
key: \\"data\\",
285285
required: false,
286-
type: [Object]
286+
type: Object
287287
},
288288
arr: {
289289
key: \\"arr\\",
290290
required: false,
291-
type: [Array]
291+
type: Array
292292
},
293-
any: {
294-
key: \\"any\\",
295-
required: true,
296-
type: [null]
297-
}
293+
any: null
298294
};
299295
300296
__sfc_main.setup = (__props, __ctx) => {
@@ -311,6 +307,29 @@ export default __sfc_main;
311307
"
312308
`;
313309
310+
exports[`transform fixtures test/fixtures/MacrosTypeAny.vue 1`] = `
311+
"<template>
312+
<div>
313+
{{ value }}
314+
</div>
315+
</template>
316+
317+
<script lang=\\"ts\\">
318+
const __sfc_main = {};
319+
__sfc_main.props = {
320+
value: null
321+
};
322+
323+
__sfc_main.setup = (__props, __ctx) => {
324+
const props = __props;
325+
return {};
326+
};
327+
328+
export default __sfc_main;
329+
</script>
330+
"
331+
`;
332+
314333
exports[`transform fixtures test/fixtures/Object1.vue 1`] = `
315334
"<template>
316335
<div>

test/fixtures/MacrosTypeAny.vue

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<template>
2+
<div>
3+
{{ value }}
4+
</div>
5+
</template>
6+
7+
<script setup lang="ts">
8+
const props = defineProps<{ value: any }>()
9+
</script>

0 commit comments

Comments
 (0)