-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
/
Copy pathdefineProps.spec.ts
811 lines (772 loc) · 24.6 KB
/
defineProps.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
import { BindingTypes } from '@vue/compiler-core'
import { assertCode, compileSFCScript as compile } from '../utils'
describe('defineProps', () => {
test('basic usage', () => {
const { content, bindings } = compile(`
<script setup>
const props = defineProps({
foo: String
})
const bar = 1
</script>
`)
// should generate working code
assertCode(content)
// should analyze bindings
expect(bindings).toStrictEqual({
foo: BindingTypes.PROPS,
bar: BindingTypes.LITERAL_CONST,
props: BindingTypes.SETUP_REACTIVE_CONST,
})
// should remove defineOptions import and call
expect(content).not.toMatch('defineProps')
// should generate correct setup signature
expect(content).toMatch(`setup(__props, { expose: __expose }) {`)
// should assign user identifier to it
expect(content).toMatch(`const props = __props`)
// should include context options in default export
expect(content).toMatch(`export default {
props: {
foo: String
},`)
})
test('w/ external definition', () => {
const { content } = compile(`
<script setup>
import { propsModel } from './props'
const props = defineProps(propsModel)
</script>
`)
assertCode(content)
expect(content).toMatch(`export default {
props: propsModel,`)
})
// #4764
test('w/ leading code', () => {
const { content } = compile(`
<script setup>import { x } from './x'
const props = defineProps({})
</script>
`)
// props declaration should be inside setup, not moved along with the import
expect(content).not.toMatch(`const props = __props\nimport`)
assertCode(content)
})
test('defineProps w/ runtime options', () => {
const { content } = compile(`
<script setup lang="ts">
const props = defineProps({ foo: String })
</script>
`)
assertCode(content)
expect(content).toMatch(`export default /*@__PURE__*/_defineComponent({
props: { foo: String },
setup(__props, { expose: __expose }) {`)
})
test('w/ type', () => {
const { content, bindings } = compile(`
<script setup lang="ts">
interface Test {}
type Alias = number[]
defineProps<{
string: string
number: number
boolean: boolean
object: object
objectLiteral: { a: number }
fn: (n: number) => void
functionRef: Function
objectRef: Object
dateTime: Date
array: string[]
arrayRef: Array<any>
tuple: [number, number]
set: Set<string>
literal: 'foo'
optional?: any
recordRef: Record<string, null>
interface: Test
alias: Alias
method(): void
symbol: symbol
error: Error
extract: Extract<1 | 2 | boolean, 2>
exclude: Exclude<1 | 2 | boolean, 2>
uppercase: Uppercase<'foo'>
params: Parameters<(foo: any) => void>
nonNull: NonNullable<string | null>
objectOrFn: {
(): void
foo: string
}
union: string | number
literalUnion: 'foo' | 'bar'
literalUnionNumber: 1 | 2 | 3 | 4 | 5
literalUnionMixed: 'foo' | 1 | boolean
intersection: Test & {}
intersection2: 'foo' & ('foo' | 'bar')
foo: ((item: any) => boolean) | null
unknown: UnknownType
unknownUnion: UnknownType | string
unknownIntersection: UnknownType & Object
unknownUnionWithBoolean: UnknownType | boolean
unknownUnionWithFunction: UnknownType | (() => any)
}>()
</script>`)
assertCode(content)
expect(content).toMatch(`string: { type: String, required: true }`)
expect(content).toMatch(`number: { type: Number, required: true }`)
expect(content).toMatch(`boolean: { type: Boolean, required: true }`)
expect(content).toMatch(`object: { type: Object, required: true }`)
expect(content).toMatch(`objectLiteral: { type: Object, required: true }`)
expect(content).toMatch(`fn: { type: Function, required: true }`)
expect(content).toMatch(`functionRef: { type: Function, required: true }`)
expect(content).toMatch(`objectRef: { type: Object, required: true }`)
expect(content).toMatch(`dateTime: { type: Date, required: true }`)
expect(content).toMatch(`array: { type: Array, required: true }`)
expect(content).toMatch(`arrayRef: { type: Array, required: true }`)
expect(content).toMatch(`tuple: { type: Array, required: true }`)
expect(content).toMatch(`set: { type: Set, required: true }`)
expect(content).toMatch(`literal: { type: String, required: true }`)
expect(content).toMatch(`optional: { type: null, required: false }`)
expect(content).toMatch(`recordRef: { type: Object, required: true }`)
expect(content).toMatch(`interface: { type: Object, required: true }`)
expect(content).toMatch(`alias: { type: Array, required: true }`)
expect(content).toMatch(`method: { type: Function, required: true }`)
expect(content).toMatch(`symbol: { type: Symbol, required: true }`)
expect(content).toMatch(`error: { type: Error, required: true }`)
expect(content).toMatch(
`objectOrFn: { type: [Function, Object], required: true },`,
)
expect(content).toMatch(`extract: { type: Number, required: true }`)
expect(content).toMatch(
`exclude: { type: [Number, Boolean], required: true }`,
)
expect(content).toMatch(`uppercase: { type: String, required: true }`)
expect(content).toMatch(`params: { type: Array, required: true }`)
expect(content).toMatch(`nonNull: { type: String, required: true }`)
expect(content).toMatch(`union: { type: [String, Number], required: true }`)
expect(content).toMatch(`literalUnion: { type: String, required: true }`)
expect(content).toMatch(
`literalUnionNumber: { type: Number, required: true }`,
)
expect(content).toMatch(
`literalUnionMixed: { type: [String, Number, Boolean], required: true }`,
)
expect(content).toMatch(`intersection: { type: Object, required: true }`)
expect(content).toMatch(`intersection2: { type: String, required: true }`)
expect(content).toMatch(`foo: { type: [Function, null], required: true }`)
expect(content).toMatch(`unknown: { type: null, required: true }`)
// uninon containing unknown type: skip check
expect(content).toMatch(`unknownUnion: { type: null, required: true }`)
// intersection containing unknown type: narrow to the known types
expect(content).toMatch(
`unknownIntersection: { type: Object, required: true },`,
)
expect(content).toMatch(
`unknownUnionWithBoolean: { type: Boolean, required: true, skipCheck: true },`,
)
expect(content).toMatch(
`unknownUnionWithFunction: { type: Function, required: true, skipCheck: true }`,
)
expect(bindings).toStrictEqual({
string: BindingTypes.PROPS,
number: BindingTypes.PROPS,
boolean: BindingTypes.PROPS,
object: BindingTypes.PROPS,
objectLiteral: BindingTypes.PROPS,
fn: BindingTypes.PROPS,
functionRef: BindingTypes.PROPS,
objectRef: BindingTypes.PROPS,
dateTime: BindingTypes.PROPS,
array: BindingTypes.PROPS,
arrayRef: BindingTypes.PROPS,
tuple: BindingTypes.PROPS,
set: BindingTypes.PROPS,
literal: BindingTypes.PROPS,
optional: BindingTypes.PROPS,
recordRef: BindingTypes.PROPS,
interface: BindingTypes.PROPS,
alias: BindingTypes.PROPS,
method: BindingTypes.PROPS,
symbol: BindingTypes.PROPS,
error: BindingTypes.PROPS,
objectOrFn: BindingTypes.PROPS,
extract: BindingTypes.PROPS,
exclude: BindingTypes.PROPS,
union: BindingTypes.PROPS,
literalUnion: BindingTypes.PROPS,
literalUnionNumber: BindingTypes.PROPS,
literalUnionMixed: BindingTypes.PROPS,
intersection: BindingTypes.PROPS,
intersection2: BindingTypes.PROPS,
foo: BindingTypes.PROPS,
uppercase: BindingTypes.PROPS,
params: BindingTypes.PROPS,
nonNull: BindingTypes.PROPS,
unknown: BindingTypes.PROPS,
unknownUnion: BindingTypes.PROPS,
unknownIntersection: BindingTypes.PROPS,
unknownUnionWithBoolean: BindingTypes.PROPS,
unknownUnionWithFunction: BindingTypes.PROPS,
})
})
test('w/ interface', () => {
const { content, bindings } = compile(`
<script setup lang="ts">
interface Props { x?: number }
defineProps<Props>()
</script>
`)
assertCode(content)
expect(content).toMatch(`x: { type: Number, required: false }`)
expect(bindings).toStrictEqual({
x: BindingTypes.PROPS,
})
})
test('w/ extends interface', () => {
const { content, bindings } = compile(`
<script lang="ts">
interface Foo { x?: number }
</script>
<script setup lang="ts">
interface Bar extends Foo { y?: number }
interface Props extends Bar {
z: number
y: string
}
defineProps<Props>()
</script>
`)
assertCode(content)
expect(content).toMatch(`z: { type: Number, required: true }`)
expect(content).toMatch(`y: { type: String, required: true }`)
expect(content).toMatch(`x: { type: Number, required: false }`)
expect(bindings).toStrictEqual({
x: BindingTypes.PROPS,
y: BindingTypes.PROPS,
z: BindingTypes.PROPS,
})
})
test('w/ extends intersection type', () => {
const { content, bindings } = compile(`
<script setup lang="ts">
type Foo = {
x?: number;
};
interface Props extends Foo {
z: number
y: string
}
defineProps<Props>()
</script>
`)
assertCode(content)
expect(content).toMatch(`z: { type: Number, required: true }`)
expect(content).toMatch(`y: { type: String, required: true }`)
expect(content).toMatch(`x: { type: Number, required: false }`)
expect(bindings).toStrictEqual({
x: BindingTypes.PROPS,
y: BindingTypes.PROPS,
z: BindingTypes.PROPS,
})
})
test('w/ intersection type', () => {
const { content, bindings } = compile(`
<script setup lang="ts">
type Foo = {
x?: number;
};
type Bar = {
y: string;
};
defineProps<Foo & Bar>()
</script>
`)
assertCode(content)
expect(content).toMatch(`y: { type: String, required: true }`)
expect(content).toMatch(`x: { type: Number, required: false }`)
expect(bindings).toStrictEqual({
x: BindingTypes.PROPS,
y: BindingTypes.PROPS,
})
})
test('w/ exported interface', () => {
const { content, bindings } = compile(`
<script setup lang="ts">
export interface Props { x?: number }
defineProps<Props>()
</script>
`)
assertCode(content)
expect(content).toMatch(`x: { type: Number, required: false }`)
expect(bindings).toStrictEqual({
x: BindingTypes.PROPS,
})
})
test('w/ exported interface in normal script', () => {
const { content, bindings } = compile(`
<script lang="ts">
export interface Props { x?: number }
</script>
<script setup lang="ts">
defineProps<Props>()
</script>
`)
assertCode(content)
expect(content).toMatch(`x: { type: Number, required: false }`)
expect(bindings).toStrictEqual({
x: BindingTypes.PROPS,
})
})
test('w/ type alias', () => {
const { content, bindings } = compile(`
<script setup lang="ts">
type Props = { x?: number }
defineProps<Props>()
</script>
`)
assertCode(content)
expect(content).toMatch(`x: { type: Number, required: false }`)
expect(bindings).toStrictEqual({
x: BindingTypes.PROPS,
})
})
test('w/ exported type alias', () => {
const { content, bindings } = compile(`
<script setup lang="ts">
export type Props = { x?: number }
defineProps<Props>()
</script>
`)
assertCode(content)
expect(content).toMatch(`x: { type: Number, required: false }`)
expect(bindings).toStrictEqual({
x: BindingTypes.PROPS,
})
})
test('w/ TS assertion', () => {
const { content, bindings } = compile(`
<script setup lang="ts">
defineProps(['foo'])! as any
</script>
`)
expect(content).toMatch(`props: ['foo']`)
assertCode(content)
expect(bindings).toStrictEqual({
foo: BindingTypes.PROPS,
})
})
test('withDefaults (static)', () => {
const { content, bindings } = compile(`
<script setup lang="ts">
const props = withDefaults(defineProps<{
foo?: string
bar?: number;
baz: boolean;
qux?(): number;
quux?(): void
quuxx?: Promise<string>;
fred?: string
}>(), {
foo: 'hi',
qux() { return 1 },
['quux']() { },
async quuxx() { return await Promise.resolve('hi') },
get fred() { return 'fred' }
})
</script>
`)
assertCode(content)
expect(content).toMatch(
`foo: { type: String, required: false, default: 'hi' }`,
)
expect(content).toMatch(`bar: { type: Number, required: false }`)
expect(content).toMatch(`baz: { type: Boolean, required: true }`)
expect(content).toMatch(
`qux: { type: Function, required: false, default() { return 1 } }`,
)
expect(content).toMatch(
`quux: { type: Function, required: false, default() { } }`,
)
expect(content).toMatch(
`quuxx: { type: Promise, required: false, async default() { return await Promise.resolve('hi') } }`,
)
expect(content).toMatch(
`fred: { type: String, required: false, get default() { return 'fred' } }`,
)
expect(content).toMatch(`const props = __props`)
expect(bindings).toStrictEqual({
foo: BindingTypes.PROPS,
bar: BindingTypes.PROPS,
baz: BindingTypes.PROPS,
qux: BindingTypes.PROPS,
quux: BindingTypes.PROPS,
quuxx: BindingTypes.PROPS,
fred: BindingTypes.PROPS,
props: BindingTypes.SETUP_CONST,
})
})
test('withDefaults (static) + normal script', () => {
const { content } = compile(`
<script lang="ts">
interface Props {
a?: string;
}
</script>
<script setup lang="ts">
const props = withDefaults(defineProps<Props>(), {
a: "a",
});
</script>
`)
assertCode(content)
})
// #7111
test('withDefaults (static) w/ production mode', () => {
const { content } = compile(
`
<script setup lang="ts">
const props = withDefaults(defineProps<{
foo: () => void
bar: boolean
baz: boolean | (() => void)
qux: string | number
}>(), {
baz: true,
qux: 'hi'
})
</script>
`,
{ isProd: true },
)
assertCode(content)
expect(content).toMatch(`const props = __props`)
// foo has no default value, the Function can be dropped
expect(content).toMatch(`foo: {}`)
expect(content).toMatch(`bar: { type: Boolean }`)
expect(content).toMatch(`baz: { type: [Boolean, Function], default: true }`)
expect(content).toMatch(`qux: { default: 'hi' }`)
})
test('withDefaults (dynamic)', () => {
const { content } = compile(`
<script setup lang="ts">
import { defaults } from './foo'
const props = withDefaults(defineProps<{
foo?: string
bar?: number
baz: boolean
}>(), { ...defaults })
</script>
`)
assertCode(content)
expect(content).toMatch(`import { mergeDefaults as _mergeDefaults`)
expect(content).toMatch(
`
_mergeDefaults({
foo: { type: String, required: false },
bar: { type: Number, required: false },
baz: { type: Boolean, required: true }
}, { ...defaults })`.trim(),
)
})
test('withDefaults (reference)', () => {
const { content } = compile(`
<script setup lang="ts">
import { defaults } from './foo'
const props = withDefaults(defineProps<{
foo?: string
bar?: number
baz: boolean
}>(), defaults)
</script>
`)
assertCode(content)
expect(content).toMatch(`import { mergeDefaults as _mergeDefaults`)
expect(content).toMatch(
`
_mergeDefaults({
foo: { type: String, required: false },
bar: { type: Number, required: false },
baz: { type: Boolean, required: true }
}, defaults)`.trim(),
)
})
// #7111
test('withDefaults (dynamic) w/ production mode', () => {
const { content } = compile(
`
<script setup lang="ts">
import { defaults } from './foo'
const props = withDefaults(defineProps<{
foo: () => void
bar: boolean
baz: boolean | (() => void)
qux: string | number
}>(), { ...defaults })
</script>
`,
{ isProd: true },
)
assertCode(content)
expect(content).toMatch(`import { mergeDefaults as _mergeDefaults`)
expect(content).toMatch(
`
_mergeDefaults({
foo: { type: Function },
bar: { type: Boolean },
baz: { type: [Boolean, Function] },
qux: {}
}, { ...defaults })`.trim(),
)
})
test('withDefaults w/ dynamic object method', () => {
const { content } = compile(`
<script setup lang="ts">
const props = withDefaults(defineProps<{
foo?: () => 'string'
}>(), {
['fo' + 'o']() { return 'foo' }
})
</script>
`)
assertCode(content)
expect(content).toMatch(`import { mergeDefaults as _mergeDefaults`)
expect(content).toMatch(
`
_mergeDefaults({
foo: { type: Function, required: false }
}, {
['fo' + 'o']() { return 'foo' }
})`.trim(),
)
})
test('runtime inference for Enum', () => {
expect(
compile(
`<script setup lang="ts">
const enum Foo { A = 123 }
defineProps<{
foo: Foo
}>()
</script>`,
{ hoistStatic: true },
).content,
).toMatch(`foo: { type: Number`)
expect(
compile(
`<script setup lang="ts">
const enum Foo { A = '123' }
defineProps<{
foo: Foo
}>()
</script>`,
{ hoistStatic: true },
).content,
).toMatch(`foo: { type: String`)
expect(
compile(
`<script setup lang="ts">
const enum Foo { A = '123', B = 123 }
defineProps<{
foo: Foo
}>()
</script>`,
{ hoistStatic: true },
).content,
).toMatch(`foo: { type: [String, Number]`)
expect(
compile(
`<script setup lang="ts">
const enum Foo { A, B }
defineProps<{
foo: Foo
}>()
</script>`,
{ hoistStatic: true },
).content,
).toMatch(`foo: { type: Number`)
})
// #8148
test('should not override local bindings', () => {
const { bindings } = compile(`
<script setup lang="ts">
import { computed } from 'vue'
defineProps<{ bar: string }>()
const bar = computed(() => 1)
</script>
`)
expect(bindings).toStrictEqual({
bar: BindingTypes.SETUP_REF,
computed: BindingTypes.SETUP_CONST,
})
})
// #8289
test('destructure without enabling reactive destructure', () => {
const { content, bindings } = compile(
`<script setup lang="ts">
const { foo } = defineProps<{
foo: Foo
}>()
</script>`,
{
propsDestructure: false,
},
)
expect(content).toMatch(`const { foo } = __props`)
expect(content).toMatch(`return { foo }`)
expect(bindings).toStrictEqual({
foo: BindingTypes.SETUP_CONST,
})
assertCode(content)
})
test('prohibiting reactive destructure', () => {
expect(() =>
compile(
`<script setup lang="ts">
const { foo } = defineProps<{
foo: Foo
}>()
</script>`,
{
propsDestructure: 'error',
},
),
).toThrow()
})
describe('errors', () => {
test('w/ both type and non-type args', () => {
expect(() => {
compile(`<script setup lang="ts">
defineProps<{}>({})
</script>`)
}).toThrow(`cannot accept both type and non-type arguments`)
})
})
test('should escape names w/ special symbols', () => {
const { content, bindings } = compile(`
<script setup lang="ts">
defineProps<{
'spa ce': unknown
'exclamation!mark': unknown
'double"quote': unknown
'hash#tag': unknown
'dollar$sign': unknown
'percentage%sign': unknown
'amper&sand': unknown
"single'quote": unknown
'round(brack)ets': unknown
'aste*risk': unknown
'pl+us': unknown
'com,ma': unknown
'do.t': unknown
'sla/sh': unknown
'co:lon': unknown
'semi;colon': unknown
'angle<brack>ets': unknown
'equal=sign': unknown
'question?mark': unknown
'at@sign': unknown
'square[brack]ets': unknown
'back\\\\slash': unknown
'ca^ret': unknown
'back\`tick': unknown
'curly{bra}ces': unknown
'pi|pe': unknown
'til~de': unknown
'da-sh': unknown
}>()
</script>`)
assertCode(content)
expect(content).toMatch(`"spa ce": { type: null, required: true }`)
expect(content).toMatch(
`"exclamation!mark": { type: null, required: true }`,
)
expect(content).toMatch(`"double\\"quote": { type: null, required: true }`)
expect(content).toMatch(`"hash#tag": { type: null, required: true }`)
expect(content).toMatch(`"dollar$sign": { type: null, required: true }`)
expect(content).toMatch(`"percentage%sign": { type: null, required: true }`)
expect(content).toMatch(`"amper&sand": { type: null, required: true }`)
expect(content).toMatch(`"single'quote": { type: null, required: true }`)
expect(content).toMatch(`"round(brack)ets": { type: null, required: true }`)
expect(content).toMatch(`"aste*risk": { type: null, required: true }`)
expect(content).toMatch(`"pl+us": { type: null, required: true }`)
expect(content).toMatch(`"com,ma": { type: null, required: true }`)
expect(content).toMatch(`"do.t": { type: null, required: true }`)
expect(content).toMatch(`"sla/sh": { type: null, required: true }`)
expect(content).toMatch(`"co:lon": { type: null, required: true }`)
expect(content).toMatch(`"semi;colon": { type: null, required: true }`)
expect(content).toMatch(`"angle<brack>ets": { type: null, required: true }`)
expect(content).toMatch(`"equal=sign": { type: null, required: true }`)
expect(content).toMatch(`"question?mark": { type: null, required: true }`)
expect(content).toMatch(`"at@sign": { type: null, required: true }`)
expect(content).toMatch(
`"square[brack]ets": { type: null, required: true }`,
)
expect(content).toMatch(`"back\\\\slash": { type: null, required: true }`)
expect(content).toMatch(`"ca^ret": { type: null, required: true }`)
expect(content).toMatch(`"back\`tick": { type: null, required: true }`)
expect(content).toMatch(`"curly{bra}ces": { type: null, required: true }`)
expect(content).toMatch(`"pi|pe": { type: null, required: true }`)
expect(content).toMatch(`"til~de": { type: null, required: true }`)
expect(content).toMatch(`"da-sh": { type: null, required: true }`)
expect(bindings).toStrictEqual({
'spa ce': BindingTypes.PROPS,
'exclamation!mark': BindingTypes.PROPS,
'double"quote': BindingTypes.PROPS,
'hash#tag': BindingTypes.PROPS,
dollar$sign: BindingTypes.PROPS,
'percentage%sign': BindingTypes.PROPS,
'amper&sand': BindingTypes.PROPS,
"single'quote": BindingTypes.PROPS,
'round(brack)ets': BindingTypes.PROPS,
'aste*risk': BindingTypes.PROPS,
'pl+us': BindingTypes.PROPS,
'com,ma': BindingTypes.PROPS,
'do.t': BindingTypes.PROPS,
'sla/sh': BindingTypes.PROPS,
'co:lon': BindingTypes.PROPS,
'semi;colon': BindingTypes.PROPS,
'angle<brack>ets': BindingTypes.PROPS,
'equal=sign': BindingTypes.PROPS,
'question?mark': BindingTypes.PROPS,
'at@sign': BindingTypes.PROPS,
'square[brack]ets': BindingTypes.PROPS,
'back\\slash': BindingTypes.PROPS,
'ca^ret': BindingTypes.PROPS,
'back`tick': BindingTypes.PROPS,
'curly{bra}ces': BindingTypes.PROPS,
'pi|pe': BindingTypes.PROPS,
'til~de': BindingTypes.PROPS,
'da-sh': BindingTypes.PROPS,
})
})
// #8989
test('custom element retains the props type & production mode', () => {
const { content } = compile(
`<script setup lang="ts">
const props = defineProps<{ foo: number}>()
</script>`,
{ isProd: true, customElement: filename => /\.ce\.vue$/.test(filename) },
{ filename: 'app.ce.vue' },
)
expect(content).toMatch(`foo: {type: Number}`)
assertCode(content)
})
test('custom element retains the props type & default value & production mode', () => {
const { content } = compile(
`<script setup lang="ts">
interface Props {
foo?: number;
}
const props = withDefaults(defineProps<Props>(), {
foo: 5.5,
});
</script>`,
{ isProd: true, customElement: filename => /\.ce\.vue$/.test(filename) },
{ filename: 'app.ce.vue' },
)
expect(content).toMatch(`foo: { default: 5.5, type: Number }`)
assertCode(content)
})
})