definePropsDestructure.spec.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. import { BindingTypes } from '@vue/compiler-core'
  2. import type { SFCScriptCompileOptions } from '../../src'
  3. import { assertCode, compileSFCScript } from '../utils'
  4. describe('sfc reactive props destructure', () => {
  5. function compile(src: string, options?: Partial<SFCScriptCompileOptions>) {
  6. return compileSFCScript(src, {
  7. inlineTemplate: true,
  8. ...options,
  9. })
  10. }
  11. test('basic usage', () => {
  12. const { content, bindings } = compile(`
  13. <script setup>
  14. const { foo } = defineProps(['foo'])
  15. console.log(foo)
  16. </script>
  17. <template>{{ foo }}</template>
  18. `)
  19. expect(content).not.toMatch(`const { foo } =`)
  20. expect(content).toMatch(`console.log(__props.foo)`)
  21. expect(content).toMatch(`_toDisplayString(__props.foo)`)
  22. assertCode(content)
  23. expect(bindings).toStrictEqual({
  24. foo: BindingTypes.PROPS,
  25. })
  26. })
  27. test('multiple variable declarations', () => {
  28. const { content, bindings } = compile(`
  29. <script setup>
  30. const bar = 'fish', { foo } = defineProps(['foo']), hello = 'world'
  31. </script>
  32. <template><div>{{ foo }} {{ hello }} {{ bar }}</div></template>
  33. `)
  34. expect(content).not.toMatch(`const { foo } =`)
  35. expect(content).toMatch(`const bar = 'fish', hello = 'world'`)
  36. expect(content).toMatch(`_toDisplayString(hello)`)
  37. expect(content).toMatch(`_toDisplayString(bar)`)
  38. expect(content).toMatch(`_toDisplayString(__props.foo)`)
  39. assertCode(content)
  40. expect(bindings).toStrictEqual({
  41. foo: BindingTypes.PROPS,
  42. bar: BindingTypes.LITERAL_CONST,
  43. hello: BindingTypes.LITERAL_CONST,
  44. })
  45. })
  46. test('nested scope', () => {
  47. const { content, bindings } = compile(`
  48. <script setup>
  49. const { foo, bar } = defineProps(['foo', 'bar'])
  50. function test(foo) {
  51. console.log(foo)
  52. console.log(bar)
  53. }
  54. </script>
  55. `)
  56. expect(content).not.toMatch(`const { foo, bar } =`)
  57. expect(content).toMatch(`console.log(foo)`)
  58. expect(content).toMatch(`console.log(__props.bar)`)
  59. assertCode(content)
  60. expect(bindings).toStrictEqual({
  61. foo: BindingTypes.PROPS,
  62. bar: BindingTypes.PROPS,
  63. test: BindingTypes.SETUP_CONST,
  64. })
  65. })
  66. test('default values w/ array runtime declaration', () => {
  67. const { content } = compile(`
  68. <script setup>
  69. const { foo = 1, bar = {}, func = () => {} } = defineProps(['foo', 'bar', 'baz'])
  70. </script>
  71. `)
  72. // literals can be used as-is, non-literals are always returned from a
  73. // function
  74. // functions need to be marked with a skip marker
  75. expect(content)
  76. .toMatch(`props: /*@__PURE__*/_mergeDefaults(['foo', 'bar', 'baz'], {
  77. foo: 1,
  78. bar: () => ({}),
  79. func: () => {}, __skip_func: true
  80. })`)
  81. assertCode(content)
  82. })
  83. test('default values w/ object runtime declaration', () => {
  84. const { content } = compile(`
  85. <script setup>
  86. const { foo = 1, bar = {}, func = () => {}, ext = x } = defineProps({ foo: Number, bar: Object, func: Function, ext: null })
  87. </script>
  88. `)
  89. // literals can be used as-is, non-literals are always returned from a
  90. // function
  91. // functions need to be marked with a skip marker since we cannot always
  92. // safely infer whether runtime type is Function (e.g. if the runtime decl
  93. // is imported, or spreads another object)
  94. expect(content)
  95. .toMatch(`props: /*@__PURE__*/_mergeDefaults({ foo: Number, bar: Object, func: Function, ext: null }, {
  96. foo: 1,
  97. bar: () => ({}),
  98. func: () => {}, __skip_func: true,
  99. ext: x, __skip_ext: true
  100. })`)
  101. assertCode(content)
  102. })
  103. test('default values w/ runtime declaration & key is string', () => {
  104. const { content, bindings } = compile(`
  105. <script setup>
  106. const { foo = 1, 'foo:bar': fooBar = 'foo-bar' } = defineProps(['foo', 'foo:bar'])
  107. </script>
  108. `)
  109. expect(bindings).toStrictEqual({
  110. __propsAliases: {
  111. fooBar: 'foo:bar',
  112. },
  113. foo: BindingTypes.PROPS,
  114. 'foo:bar': BindingTypes.PROPS,
  115. fooBar: BindingTypes.PROPS_ALIASED,
  116. })
  117. expect(content).toMatch(`
  118. props: /*@__PURE__*/_mergeDefaults(['foo', 'foo:bar'], {
  119. foo: 1,
  120. "foo:bar": 'foo-bar'
  121. }),`)
  122. assertCode(content)
  123. })
  124. test('default values w/ type declaration', () => {
  125. const { content } = compile(`
  126. <script setup lang="ts">
  127. const { foo = 1, bar = {}, func = () => {} } = defineProps<{ foo?: number, bar?: object, func?: () => any }>()
  128. </script>
  129. `)
  130. // literals can be used as-is, non-literals are always returned from a
  131. // function
  132. expect(content).toMatch(`props: {
  133. foo: { type: Number, required: false, default: 1 },
  134. bar: { type: Object, required: false, default: () => ({}) },
  135. func: { type: Function, required: false, default: () => {} }
  136. }`)
  137. assertCode(content)
  138. })
  139. test('default values w/ type declaration & key is string', () => {
  140. const { content, bindings } = compile(`
  141. <script setup lang="ts">
  142. const { foo = 1, bar = 2, 'foo:bar': fooBar = 'foo-bar' } = defineProps<{
  143. "foo": number // double-quoted string
  144. 'bar': number // single-quoted string
  145. 'foo:bar': string // single-quoted string containing symbols
  146. "onUpdate:modelValue": (val: number) => void // double-quoted string containing symbols
  147. }>()
  148. </script>
  149. <template>{{ foo }}</template>
  150. `)
  151. expect(bindings).toStrictEqual({
  152. __propsAliases: {
  153. fooBar: 'foo:bar',
  154. },
  155. foo: BindingTypes.PROPS,
  156. bar: BindingTypes.PROPS,
  157. 'foo:bar': BindingTypes.PROPS,
  158. fooBar: BindingTypes.PROPS_ALIASED,
  159. 'onUpdate:modelValue': BindingTypes.PROPS,
  160. })
  161. expect(content).toMatch(`
  162. props: {
  163. foo: { type: Number, required: true, default: 1 },
  164. bar: { type: Number, required: true, default: 2 },
  165. "foo:bar": { type: String, required: true, default: 'foo-bar' },
  166. "onUpdate:modelValue": { type: Function, required: true }
  167. },`)
  168. expect(content).toMatch(`__props.foo`)
  169. assertCode(content)
  170. })
  171. test('default values w/ type declaration, prod mode', () => {
  172. const { content } = compile(
  173. `
  174. <script setup lang="ts">
  175. const { foo = 1, bar = {}, func = () => {} } = defineProps<{ foo?: number, bar?: object, baz?: any, boola?: boolean, boolb?: boolean | number, func?: Function }>()
  176. </script>
  177. `,
  178. { isProd: true },
  179. )
  180. assertCode(content)
  181. // literals can be used as-is, non-literals are always returned from a
  182. // function
  183. expect(content).toMatch(`props: {
  184. foo: { default: 1 },
  185. bar: { default: () => ({}) },
  186. baz: {},
  187. boola: { type: Boolean },
  188. boolb: { type: [Boolean, Number] },
  189. func: { type: Function, default: () => {} }
  190. }`)
  191. })
  192. test('with TSInstantiationExpression', () => {
  193. const { content } = compile(
  194. `
  195. <script setup lang="ts">
  196. type Foo = <T extends string | number>(data: T) => void
  197. const { value } = defineProps<{ value: Foo }>()
  198. const foo = value<123>
  199. </script>
  200. `,
  201. { isProd: true },
  202. )
  203. assertCode(content)
  204. expect(content).toMatch(`const foo = __props.value<123>`)
  205. })
  206. test('aliasing', () => {
  207. const { content, bindings } = compile(`
  208. <script setup>
  209. const { foo: bar } = defineProps(['foo'])
  210. let x = foo
  211. let y = bar
  212. </script>
  213. <template>{{ foo + bar }}</template>
  214. `)
  215. expect(content).not.toMatch(`const { foo: bar } =`)
  216. expect(content).toMatch(`let x = foo`) // should not process
  217. expect(content).toMatch(`let y = __props.foo`)
  218. // should convert bar to __props.foo in template expressions
  219. expect(content).toMatch(`_toDisplayString(__props.foo + __props.foo)`)
  220. assertCode(content)
  221. expect(bindings).toStrictEqual({
  222. x: BindingTypes.SETUP_LET,
  223. y: BindingTypes.SETUP_LET,
  224. foo: BindingTypes.PROPS,
  225. bar: BindingTypes.PROPS_ALIASED,
  226. __propsAliases: {
  227. bar: 'foo',
  228. },
  229. })
  230. })
  231. // #5425
  232. test('non-identifier prop names', () => {
  233. const { content, bindings } = compile(`
  234. <script setup>
  235. const { 'foo.bar': fooBar } = defineProps({ 'foo.bar': Function })
  236. let x = fooBar
  237. </script>
  238. <template>{{ fooBar }}</template>
  239. `)
  240. expect(content).toMatch(`x = __props["foo.bar"]`)
  241. expect(content).toMatch(`toDisplayString(__props["foo.bar"])`)
  242. assertCode(content)
  243. expect(bindings).toStrictEqual({
  244. x: BindingTypes.SETUP_LET,
  245. 'foo.bar': BindingTypes.PROPS,
  246. fooBar: BindingTypes.PROPS_ALIASED,
  247. __propsAliases: {
  248. fooBar: 'foo.bar',
  249. },
  250. })
  251. })
  252. test('rest spread', () => {
  253. const { content, bindings } = compile(`
  254. <script setup>
  255. const { foo, bar, ...rest } = defineProps(['foo', 'bar', 'baz'])
  256. </script>
  257. `)
  258. expect(content).toMatch(
  259. `const rest = _createPropsRestProxy(__props, ["foo","bar"])`,
  260. )
  261. assertCode(content)
  262. expect(bindings).toStrictEqual({
  263. foo: BindingTypes.PROPS,
  264. bar: BindingTypes.PROPS,
  265. baz: BindingTypes.PROPS,
  266. rest: BindingTypes.SETUP_REACTIVE_CONST,
  267. })
  268. })
  269. test('rest spread non-inline', () => {
  270. const { content, bindings } = compile(
  271. `
  272. <script setup>
  273. const { foo, ...rest } = defineProps(['foo', 'bar'])
  274. </script>
  275. <template>{{ rest.bar }}</template>
  276. `,
  277. { inlineTemplate: false },
  278. )
  279. expect(content).toMatch(
  280. `const rest = _createPropsRestProxy(__props, ["foo"])`,
  281. )
  282. assertCode(content)
  283. expect(bindings).toStrictEqual({
  284. foo: BindingTypes.PROPS,
  285. bar: BindingTypes.PROPS,
  286. rest: BindingTypes.SETUP_REACTIVE_CONST,
  287. })
  288. })
  289. // #6960
  290. test('computed static key', () => {
  291. const { content, bindings } = compile(`
  292. <script setup>
  293. const { ['foo']: foo } = defineProps(['foo'])
  294. console.log(foo)
  295. </script>
  296. <template>{{ foo }}</template>
  297. `)
  298. expect(content).not.toMatch(`const { foo } =`)
  299. expect(content).toMatch(`console.log(__props.foo)`)
  300. expect(content).toMatch(`_toDisplayString(__props.foo)`)
  301. assertCode(content)
  302. expect(bindings).toStrictEqual({
  303. foo: BindingTypes.PROPS,
  304. })
  305. })
  306. test('multi-variable declaration', () => {
  307. const { content } = compile(`
  308. <script setup>
  309. const { item } = defineProps(['item']),
  310. a = 1;
  311. </script>
  312. `)
  313. assertCode(content)
  314. expect(content).toMatch(`const a = 1;`)
  315. expect(content).toMatch(`props: ['item'],`)
  316. })
  317. // #6757
  318. test('multi-variable declaration fix #6757 ', () => {
  319. const { content } = compile(`
  320. <script setup>
  321. const a = 1,
  322. { item } = defineProps(['item']);
  323. </script>
  324. `)
  325. assertCode(content)
  326. expect(content).toMatch(`const a = 1;`)
  327. expect(content).toMatch(`props: ['item'],`)
  328. })
  329. // #7422
  330. test('multi-variable declaration fix #7422', () => {
  331. const { content } = compile(`
  332. <script setup>
  333. const { item } = defineProps(['item']),
  334. a = 0,
  335. b = 0;
  336. </script>
  337. `)
  338. assertCode(content)
  339. expect(content).toMatch(`const a = 0,`)
  340. expect(content).toMatch(`b = 0;`)
  341. expect(content).toMatch(`props: ['item'],`)
  342. })
  343. test('handle function parameters with same name as destructured props', () => {
  344. const { content } = compile(`
  345. <script setup>
  346. const { value } = defineProps()
  347. function test(value) {
  348. try {
  349. } catch {
  350. }
  351. }
  352. console.log(value)
  353. </script>
  354. `)
  355. assertCode(content)
  356. expect(content).toMatch(`console.log(__props.value)`)
  357. })
  358. test('defineProps/defineEmits in multi-variable declaration (full removal)', () => {
  359. const { content } = compile(`
  360. <script setup>
  361. const props = defineProps(['item']),
  362. emit = defineEmits(['a']);
  363. </script>
  364. `)
  365. assertCode(content)
  366. expect(content).toMatch(`props: ['item'],`)
  367. expect(content).toMatch(`emits: ['a'],`)
  368. })
  369. describe('errors', () => {
  370. test('should error on deep destructure', () => {
  371. expect(() =>
  372. compile(
  373. `<script setup>const { foo: [bar] } = defineProps(['foo'])</script>`,
  374. ),
  375. ).toThrow(`destructure does not support nested patterns`)
  376. expect(() =>
  377. compile(
  378. `<script setup>const { foo: { bar } } = defineProps(['foo'])</script>`,
  379. ),
  380. ).toThrow(`destructure does not support nested patterns`)
  381. })
  382. test('should error on computed key', () => {
  383. expect(() =>
  384. compile(
  385. `<script setup>const { [foo]: bar } = defineProps(['foo'])</script>`,
  386. ),
  387. ).toThrow(`destructure cannot use computed key`)
  388. })
  389. test('should warn when used with withDefaults', () => {
  390. compile(
  391. `<script setup lang="ts">
  392. const { foo } = withDefaults(defineProps<{ foo: string }>(), { foo: 'foo' })
  393. </script>`,
  394. )
  395. expect(
  396. `withDefaults() is unnecessary when using destructure`,
  397. ).toHaveBeenWarned()
  398. })
  399. test('should error if destructure reference local vars', () => {
  400. expect(() =>
  401. compile(
  402. `<script setup>
  403. let x = 1
  404. const {
  405. foo = () => x
  406. } = defineProps(['foo'])
  407. </script>`,
  408. ),
  409. ).toThrow(`cannot reference locally declared variables`)
  410. })
  411. test('should error if assignment to destructured prop binding', () => {
  412. expect(() =>
  413. compile(
  414. `<script setup>
  415. const { foo } = defineProps(['foo'])
  416. foo = 'bar'
  417. </script>`,
  418. ),
  419. ).toThrow(`Cannot assign to destructured props`)
  420. expect(() =>
  421. compile(
  422. `<script setup>
  423. let { foo } = defineProps(['foo'])
  424. foo = 'bar'
  425. </script>`,
  426. ),
  427. ).toThrow(`Cannot assign to destructured props`)
  428. })
  429. test('should error when passing destructured prop into certain methods', () => {
  430. expect(() =>
  431. compile(
  432. `<script setup>
  433. import { watch } from 'vue'
  434. const { foo } = defineProps(['foo'])
  435. watch(foo, () => {})
  436. </script>`,
  437. ),
  438. ).toThrow(
  439. `"foo" is a destructured prop and should not be passed directly to watch().`,
  440. )
  441. expect(() =>
  442. compile(
  443. `<script setup>
  444. import { watch as w } from 'vue'
  445. const { foo } = defineProps(['foo'])
  446. w(foo, () => {})
  447. </script>`,
  448. ),
  449. ).toThrow(
  450. `"foo" is a destructured prop and should not be passed directly to watch().`,
  451. )
  452. expect(() =>
  453. compile(
  454. `<script setup>
  455. import { toRef } from 'vue'
  456. const { foo } = defineProps(['foo'])
  457. toRef(foo)
  458. </script>`,
  459. ),
  460. ).toThrow(
  461. `"foo" is a destructured prop and should not be passed directly to toRef().`,
  462. )
  463. expect(() =>
  464. compile(
  465. `<script setup>
  466. import { toRef as r } from 'vue'
  467. const { foo } = defineProps(['foo'])
  468. r(foo)
  469. </script>`,
  470. ),
  471. ).toThrow(
  472. `"foo" is a destructured prop and should not be passed directly to toRef().`,
  473. )
  474. })
  475. // not comprehensive, but should help for most common cases
  476. test('should error if default value type does not match declared type', () => {
  477. expect(() =>
  478. compile(
  479. `<script setup lang="ts">
  480. const { foo = 'hello' } = defineProps<{ foo?: number }>()
  481. </script>`,
  482. ),
  483. ).toThrow(`Default value of prop "foo" does not match declared type.`)
  484. })
  485. // #8017
  486. test('should not throw an error if the variable is not a props', () => {
  487. expect(() =>
  488. compile(
  489. `<script setup lang='ts'>
  490. import { watch } from 'vue'
  491. const { userId } = defineProps({ userId: Number })
  492. const { error: e, info } = useRequest();
  493. watch(e, () => {});
  494. watch(info, () => {});
  495. </script>`,
  496. ),
  497. ).not.toThrowError()
  498. })
  499. })
  500. })