defineProps.spec.ts 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  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/ extends intersection type', () => {
  252. const { content, bindings } = compile(`
  253. <script setup lang="ts">
  254. type Foo = {
  255. x?: number;
  256. };
  257. interface Props extends Foo {
  258. z: number
  259. y: string
  260. }
  261. defineProps<Props>()
  262. </script>
  263. `)
  264. assertCode(content)
  265. expect(content).toMatch(`z: { type: Number, required: true }`)
  266. expect(content).toMatch(`y: { type: String, required: true }`)
  267. expect(content).toMatch(`x: { type: Number, required: false }`)
  268. expect(bindings).toStrictEqual({
  269. x: BindingTypes.PROPS,
  270. y: BindingTypes.PROPS,
  271. z: BindingTypes.PROPS,
  272. })
  273. })
  274. test('w/ intersection type', () => {
  275. const { content, bindings } = compile(`
  276. <script setup lang="ts">
  277. type Foo = {
  278. x?: number;
  279. };
  280. type Bar = {
  281. y: string;
  282. };
  283. defineProps<Foo & Bar>()
  284. </script>
  285. `)
  286. assertCode(content)
  287. expect(content).toMatch(`y: { type: String, required: true }`)
  288. expect(content).toMatch(`x: { type: Number, required: false }`)
  289. expect(bindings).toStrictEqual({
  290. x: BindingTypes.PROPS,
  291. y: BindingTypes.PROPS,
  292. })
  293. })
  294. test('w/ exported interface', () => {
  295. const { content, bindings } = compile(`
  296. <script setup lang="ts">
  297. export interface Props { x?: number }
  298. defineProps<Props>()
  299. </script>
  300. `)
  301. assertCode(content)
  302. expect(content).toMatch(`x: { type: Number, required: false }`)
  303. expect(bindings).toStrictEqual({
  304. x: BindingTypes.PROPS,
  305. })
  306. })
  307. test('w/ exported interface in normal script', () => {
  308. const { content, bindings } = compile(`
  309. <script lang="ts">
  310. export interface Props { x?: number }
  311. </script>
  312. <script setup lang="ts">
  313. defineProps<Props>()
  314. </script>
  315. `)
  316. assertCode(content)
  317. expect(content).toMatch(`x: { type: Number, required: false }`)
  318. expect(bindings).toStrictEqual({
  319. x: BindingTypes.PROPS,
  320. })
  321. })
  322. test('w/ type alias', () => {
  323. const { content, bindings } = compile(`
  324. <script setup lang="ts">
  325. type Props = { x?: number }
  326. defineProps<Props>()
  327. </script>
  328. `)
  329. assertCode(content)
  330. expect(content).toMatch(`x: { type: Number, required: false }`)
  331. expect(bindings).toStrictEqual({
  332. x: BindingTypes.PROPS,
  333. })
  334. })
  335. test('w/ exported type alias', () => {
  336. const { content, bindings } = compile(`
  337. <script setup lang="ts">
  338. export type Props = { x?: number }
  339. defineProps<Props>()
  340. </script>
  341. `)
  342. assertCode(content)
  343. expect(content).toMatch(`x: { type: Number, required: false }`)
  344. expect(bindings).toStrictEqual({
  345. x: BindingTypes.PROPS,
  346. })
  347. })
  348. test('w/ TS assertion', () => {
  349. const { content, bindings } = compile(`
  350. <script setup lang="ts">
  351. defineProps(['foo'])! as any
  352. </script>
  353. `)
  354. expect(content).toMatch(`props: ['foo']`)
  355. assertCode(content)
  356. expect(bindings).toStrictEqual({
  357. foo: BindingTypes.PROPS,
  358. })
  359. })
  360. test('withDefaults (static)', () => {
  361. const { content, bindings } = compile(`
  362. <script setup lang="ts">
  363. const props = withDefaults(defineProps<{
  364. foo?: string
  365. bar?: number;
  366. baz: boolean;
  367. qux?(): number;
  368. quux?(): void
  369. quuxx?: Promise<string>;
  370. quuux?: number;
  371. fred?: string
  372. }>(), {
  373. foo: 'hi',
  374. qux() { return 1 },
  375. ['quux']() { },
  376. async quuxx() { return await Promise.resolve('hi') },
  377. quuux(a, [b, ...c], {d, ...e}, ...f) { return 1 },
  378. get fred() { return 'fred' }
  379. })
  380. </script>
  381. `)
  382. assertCode(content)
  383. expect(content).toMatch(
  384. `foo: { type: String, required: false, default: 'hi' }`,
  385. )
  386. expect(content).toMatch(`bar: { type: Number, required: false }`)
  387. expect(content).toMatch(`baz: { type: Boolean, required: true }`)
  388. expect(content).toMatch(
  389. `qux: { type: Function, required: false, default() { return 1 } }`,
  390. )
  391. expect(content).toMatch(
  392. `quux: { type: Function, required: false, default() { } }`,
  393. )
  394. expect(content).toMatch(
  395. `quuxx: { type: Promise, required: false, async default() { return await Promise.resolve('hi') } }`,
  396. )
  397. expect(content).toMatch(
  398. `quuux: { type: Number, required: false, default(a, [b, ...c], {d, ...e}, ...f) { return 1 } }`,
  399. )
  400. expect(content).toMatch(
  401. `fred: { type: String, required: false, get default() { return 'fred' } }`,
  402. )
  403. expect(content).toMatch(`const props = __props`)
  404. expect(bindings).toStrictEqual({
  405. foo: BindingTypes.PROPS,
  406. bar: BindingTypes.PROPS,
  407. baz: BindingTypes.PROPS,
  408. qux: BindingTypes.PROPS,
  409. quux: BindingTypes.PROPS,
  410. quuxx: BindingTypes.PROPS,
  411. quuux: BindingTypes.PROPS,
  412. fred: BindingTypes.PROPS,
  413. props: BindingTypes.SETUP_CONST,
  414. })
  415. })
  416. test('withDefaults (static) + normal script', () => {
  417. const { content } = compile(`
  418. <script lang="ts">
  419. interface Props {
  420. a?: string;
  421. }
  422. </script>
  423. <script setup lang="ts">
  424. const props = withDefaults(defineProps<Props>(), {
  425. a: "a",
  426. });
  427. </script>
  428. `)
  429. assertCode(content)
  430. })
  431. // #7111
  432. test('withDefaults (static) w/ production mode', () => {
  433. const { content } = compile(
  434. `
  435. <script setup lang="ts">
  436. const props = withDefaults(defineProps<{
  437. foo: () => void
  438. bar: boolean
  439. baz: boolean | (() => void)
  440. qux: string | number
  441. }>(), {
  442. baz: true,
  443. qux: 'hi'
  444. })
  445. </script>
  446. `,
  447. { isProd: true },
  448. )
  449. assertCode(content)
  450. expect(content).toMatch(`const props = __props`)
  451. // foo has no default value, the Function can be dropped
  452. expect(content).toMatch(`foo: {}`)
  453. expect(content).toMatch(`bar: { type: Boolean }`)
  454. expect(content).toMatch(`baz: { type: [Boolean, Function], default: true }`)
  455. expect(content).toMatch(`qux: { default: 'hi' }`)
  456. })
  457. test('withDefaults (dynamic)', () => {
  458. const { content } = compile(`
  459. <script setup lang="ts">
  460. import { defaults } from './foo'
  461. const props = withDefaults(defineProps<{
  462. foo?: string
  463. bar?: number
  464. baz: boolean
  465. }>(), { ...defaults })
  466. </script>
  467. `)
  468. assertCode(content)
  469. expect(content).toMatch(`import { mergeDefaults as _mergeDefaults`)
  470. expect(content).toMatch(
  471. `
  472. _mergeDefaults({
  473. foo: { type: String, required: false },
  474. bar: { type: Number, required: false },
  475. baz: { type: Boolean, required: true }
  476. }, { ...defaults })`.trim(),
  477. )
  478. })
  479. test('withDefaults (reference)', () => {
  480. const { content } = compile(`
  481. <script setup lang="ts">
  482. import { defaults } from './foo'
  483. const props = withDefaults(defineProps<{
  484. foo?: string
  485. bar?: number
  486. baz: boolean
  487. }>(), defaults)
  488. </script>
  489. `)
  490. assertCode(content)
  491. expect(content).toMatch(`import { mergeDefaults as _mergeDefaults`)
  492. expect(content).toMatch(
  493. `
  494. _mergeDefaults({
  495. foo: { type: String, required: false },
  496. bar: { type: Number, required: false },
  497. baz: { type: Boolean, required: true }
  498. }, defaults)`.trim(),
  499. )
  500. })
  501. // #7111
  502. test('withDefaults (dynamic) w/ production mode', () => {
  503. const { content } = compile(
  504. `
  505. <script setup lang="ts">
  506. import { defaults } from './foo'
  507. const props = withDefaults(defineProps<{
  508. foo: () => void
  509. bar: boolean
  510. baz: boolean | (() => void)
  511. qux: string | number
  512. }>(), { ...defaults })
  513. </script>
  514. `,
  515. { isProd: true },
  516. )
  517. assertCode(content)
  518. expect(content).toMatch(`import { mergeDefaults as _mergeDefaults`)
  519. expect(content).toMatch(
  520. `
  521. _mergeDefaults({
  522. foo: { type: Function },
  523. bar: { type: Boolean },
  524. baz: { type: [Boolean, Function] },
  525. qux: {}
  526. }, { ...defaults })`.trim(),
  527. )
  528. })
  529. test('withDefaults w/ dynamic object method', () => {
  530. const { content } = compile(`
  531. <script setup lang="ts">
  532. const props = withDefaults(defineProps<{
  533. foo?: () => 'string'
  534. }>(), {
  535. ['fo' + 'o']() { return 'foo' }
  536. })
  537. </script>
  538. `)
  539. assertCode(content)
  540. expect(content).toMatch(`import { mergeDefaults as _mergeDefaults`)
  541. expect(content).toMatch(
  542. `
  543. _mergeDefaults({
  544. foo: { type: Function, required: false }
  545. }, {
  546. ['fo' + 'o']() { return 'foo' }
  547. })`.trim(),
  548. )
  549. })
  550. test('runtime inference for Enum', () => {
  551. expect(
  552. compile(
  553. `<script setup lang="ts">
  554. const enum Foo { A = 123 }
  555. defineProps<{
  556. foo: Foo
  557. }>()
  558. </script>`,
  559. { hoistStatic: true },
  560. ).content,
  561. ).toMatch(`foo: { type: Number`)
  562. expect(
  563. compile(
  564. `<script setup lang="ts">
  565. const enum Foo { A = '123' }
  566. defineProps<{
  567. foo: Foo
  568. }>()
  569. </script>`,
  570. { hoistStatic: true },
  571. ).content,
  572. ).toMatch(`foo: { type: String`)
  573. expect(
  574. compile(
  575. `<script setup lang="ts">
  576. const enum Foo { A = '123', B = 123 }
  577. defineProps<{
  578. foo: Foo
  579. }>()
  580. </script>`,
  581. { hoistStatic: true },
  582. ).content,
  583. ).toMatch(`foo: { type: [String, Number]`)
  584. expect(
  585. compile(
  586. `<script setup lang="ts">
  587. const enum Foo { A, B }
  588. defineProps<{
  589. foo: Foo
  590. }>()
  591. </script>`,
  592. { hoistStatic: true },
  593. ).content,
  594. ).toMatch(`foo: { type: Number`)
  595. })
  596. // #8148
  597. test('should not override local bindings', () => {
  598. const { bindings } = compile(`
  599. <script setup lang="ts">
  600. import { computed } from 'vue'
  601. defineProps<{ bar: string }>()
  602. const bar = computed(() => 1)
  603. </script>
  604. `)
  605. expect(bindings).toStrictEqual({
  606. bar: BindingTypes.SETUP_REF,
  607. computed: BindingTypes.SETUP_CONST,
  608. })
  609. })
  610. // #8289
  611. test('destructure without enabling reactive destructure', () => {
  612. const { content, bindings } = compile(
  613. `<script setup lang="ts">
  614. const { foo } = defineProps<{
  615. foo: Foo
  616. }>()
  617. </script>`,
  618. {
  619. propsDestructure: false,
  620. },
  621. )
  622. expect(content).toMatch(`const { foo } = __props`)
  623. expect(content).toMatch(`return { foo }`)
  624. expect(bindings).toStrictEqual({
  625. foo: BindingTypes.SETUP_CONST,
  626. })
  627. assertCode(content)
  628. })
  629. test('prohibiting reactive destructure', () => {
  630. expect(() =>
  631. compile(
  632. `<script setup lang="ts">
  633. const { foo } = defineProps<{
  634. foo: Foo
  635. }>()
  636. </script>`,
  637. {
  638. propsDestructure: 'error',
  639. },
  640. ),
  641. ).toThrow()
  642. })
  643. describe('errors', () => {
  644. test('w/ both type and non-type args', () => {
  645. expect(() => {
  646. compile(`<script setup lang="ts">
  647. defineProps<{}>({})
  648. </script>`)
  649. }).toThrow(`cannot accept both type and non-type arguments`)
  650. })
  651. })
  652. test('should escape names w/ special symbols', () => {
  653. const { content, bindings } = compile(`
  654. <script setup lang="ts">
  655. defineProps<{
  656. 'spa ce': unknown
  657. 'exclamation!mark': unknown
  658. 'double"quote': unknown
  659. 'hash#tag': unknown
  660. 'dollar$sign': unknown
  661. 'percentage%sign': unknown
  662. 'amper&sand': unknown
  663. "single'quote": unknown
  664. 'round(brack)ets': unknown
  665. 'aste*risk': unknown
  666. 'pl+us': unknown
  667. 'com,ma': unknown
  668. 'do.t': unknown
  669. 'sla/sh': unknown
  670. 'co:lon': unknown
  671. 'semi;colon': unknown
  672. 'angle<brack>ets': unknown
  673. 'equal=sign': unknown
  674. 'question?mark': unknown
  675. 'at@sign': unknown
  676. 'square[brack]ets': unknown
  677. 'back\\\\slash': unknown
  678. 'ca^ret': unknown
  679. 'back\`tick': unknown
  680. 'curly{bra}ces': unknown
  681. 'pi|pe': unknown
  682. 'til~de': unknown
  683. 'da-sh': unknown
  684. }>()
  685. </script>`)
  686. assertCode(content)
  687. expect(content).toMatch(`"spa ce": { type: null, required: true }`)
  688. expect(content).toMatch(
  689. `"exclamation!mark": { type: null, required: true }`,
  690. )
  691. expect(content).toMatch(`"double\\"quote": { type: null, required: true }`)
  692. expect(content).toMatch(`"hash#tag": { type: null, required: true }`)
  693. expect(content).toMatch(`"dollar$sign": { type: null, required: true }`)
  694. expect(content).toMatch(`"percentage%sign": { type: null, required: true }`)
  695. expect(content).toMatch(`"amper&sand": { type: null, required: true }`)
  696. expect(content).toMatch(`"single'quote": { type: null, required: true }`)
  697. expect(content).toMatch(`"round(brack)ets": { type: null, required: true }`)
  698. expect(content).toMatch(`"aste*risk": { type: null, required: true }`)
  699. expect(content).toMatch(`"pl+us": { type: null, required: true }`)
  700. expect(content).toMatch(`"com,ma": { type: null, required: true }`)
  701. expect(content).toMatch(`"do.t": { type: null, required: true }`)
  702. expect(content).toMatch(`"sla/sh": { type: null, required: true }`)
  703. expect(content).toMatch(`"co:lon": { type: null, required: true }`)
  704. expect(content).toMatch(`"semi;colon": { type: null, required: true }`)
  705. expect(content).toMatch(`"angle<brack>ets": { type: null, required: true }`)
  706. expect(content).toMatch(`"equal=sign": { type: null, required: true }`)
  707. expect(content).toMatch(`"question?mark": { type: null, required: true }`)
  708. expect(content).toMatch(`"at@sign": { type: null, required: true }`)
  709. expect(content).toMatch(
  710. `"square[brack]ets": { type: null, required: true }`,
  711. )
  712. expect(content).toMatch(`"back\\\\slash": { type: null, required: true }`)
  713. expect(content).toMatch(`"ca^ret": { type: null, required: true }`)
  714. expect(content).toMatch(`"back\`tick": { type: null, required: true }`)
  715. expect(content).toMatch(`"curly{bra}ces": { type: null, required: true }`)
  716. expect(content).toMatch(`"pi|pe": { type: null, required: true }`)
  717. expect(content).toMatch(`"til~de": { type: null, required: true }`)
  718. expect(content).toMatch(`"da-sh": { type: null, required: true }`)
  719. expect(bindings).toStrictEqual({
  720. 'spa ce': BindingTypes.PROPS,
  721. 'exclamation!mark': BindingTypes.PROPS,
  722. 'double"quote': BindingTypes.PROPS,
  723. 'hash#tag': BindingTypes.PROPS,
  724. dollar$sign: BindingTypes.PROPS,
  725. 'percentage%sign': BindingTypes.PROPS,
  726. 'amper&sand': BindingTypes.PROPS,
  727. "single'quote": BindingTypes.PROPS,
  728. 'round(brack)ets': BindingTypes.PROPS,
  729. 'aste*risk': BindingTypes.PROPS,
  730. 'pl+us': BindingTypes.PROPS,
  731. 'com,ma': BindingTypes.PROPS,
  732. 'do.t': BindingTypes.PROPS,
  733. 'sla/sh': BindingTypes.PROPS,
  734. 'co:lon': BindingTypes.PROPS,
  735. 'semi;colon': BindingTypes.PROPS,
  736. 'angle<brack>ets': BindingTypes.PROPS,
  737. 'equal=sign': BindingTypes.PROPS,
  738. 'question?mark': BindingTypes.PROPS,
  739. 'at@sign': BindingTypes.PROPS,
  740. 'square[brack]ets': BindingTypes.PROPS,
  741. 'back\\slash': BindingTypes.PROPS,
  742. 'ca^ret': BindingTypes.PROPS,
  743. 'back`tick': BindingTypes.PROPS,
  744. 'curly{bra}ces': BindingTypes.PROPS,
  745. 'pi|pe': BindingTypes.PROPS,
  746. 'til~de': BindingTypes.PROPS,
  747. 'da-sh': BindingTypes.PROPS,
  748. })
  749. })
  750. // #8989
  751. test('custom element retains the props type & production mode', () => {
  752. const { content } = compile(
  753. `<script setup lang="ts">
  754. const props = defineProps<{ foo: number}>()
  755. </script>`,
  756. { isProd: true, customElement: filename => /\.ce\.vue$/.test(filename) },
  757. { filename: 'app.ce.vue' },
  758. )
  759. expect(content).toMatch(`foo: {type: Number}`)
  760. assertCode(content)
  761. })
  762. test('custom element retains the props type & default value & production mode', () => {
  763. const { content } = compile(
  764. `<script setup lang="ts">
  765. interface Props {
  766. foo?: number;
  767. }
  768. const props = withDefaults(defineProps<Props>(), {
  769. foo: 5.5,
  770. });
  771. </script>`,
  772. { isProd: true, customElement: filename => /\.ce\.vue$/.test(filename) },
  773. { filename: 'app.ce.vue' },
  774. )
  775. expect(content).toMatch(`foo: { default: 5.5, type: Number }`)
  776. assertCode(content)
  777. })
  778. })