defineProps.spec.ts 25 KB

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