defineProps.spec.ts 23 KB

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