definePropsDestructure.spec.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. import { BindingTypes } from '@vue/compiler-core'
  2. import { SFCScriptCompileOptions } from '../../src'
  3. import { compileSFCScript, assertCode } from '../utils'
  4. describe('sfc reactive props destructure', () => {
  5. function compile(src: string, options?: Partial<SFCScriptCompileOptions>) {
  6. return compileSFCScript(src, {
  7. inlineTemplate: true,
  8. propsDestructure: true,
  9. ...options
  10. })
  11. }
  12. test('basic usage', () => {
  13. const { content, bindings } = compile(`
  14. <script setup>
  15. const { foo } = defineProps(['foo'])
  16. console.log(foo)
  17. </script>
  18. <template>{{ foo }}</template>
  19. `)
  20. expect(content).not.toMatch(`const { foo } =`)
  21. expect(content).toMatch(`console.log(__props.foo)`)
  22. expect(content).toMatch(`_toDisplayString(__props.foo)`)
  23. assertCode(content)
  24. expect(bindings).toStrictEqual({
  25. foo: BindingTypes.PROPS
  26. })
  27. })
  28. test('multiple variable declarations', () => {
  29. const { content, bindings } = compile(`
  30. <script setup>
  31. const bar = 'fish', { foo } = defineProps(['foo']), hello = 'world'
  32. </script>
  33. <template><div>{{ foo }} {{ hello }} {{ bar }}</div></template>
  34. `)
  35. expect(content).not.toMatch(`const { foo } =`)
  36. expect(content).toMatch(`const bar = 'fish', hello = 'world'`)
  37. expect(content).toMatch(`_toDisplayString(hello)`)
  38. expect(content).toMatch(`_toDisplayString(bar)`)
  39. expect(content).toMatch(`_toDisplayString(__props.foo)`)
  40. assertCode(content)
  41. expect(bindings).toStrictEqual({
  42. foo: BindingTypes.PROPS,
  43. bar: BindingTypes.LITERAL_CONST,
  44. hello: BindingTypes.LITERAL_CONST
  45. })
  46. })
  47. test('nested scope', () => {
  48. const { content, bindings } = compile(`
  49. <script setup>
  50. const { foo, bar } = defineProps(['foo', 'bar'])
  51. function test(foo) {
  52. console.log(foo)
  53. console.log(bar)
  54. }
  55. </script>
  56. `)
  57. expect(content).not.toMatch(`const { foo, bar } =`)
  58. expect(content).toMatch(`console.log(foo)`)
  59. expect(content).toMatch(`console.log(__props.bar)`)
  60. assertCode(content)
  61. expect(bindings).toStrictEqual({
  62. foo: BindingTypes.PROPS,
  63. bar: BindingTypes.PROPS,
  64. test: BindingTypes.SETUP_CONST
  65. })
  66. })
  67. test('default values w/ array runtime declaration', () => {
  68. const { content } = compile(`
  69. <script setup>
  70. const { foo = 1, bar = {}, func = () => {} } = defineProps(['foo', 'bar', 'baz'])
  71. </script>
  72. `)
  73. // literals can be used as-is, non-literals are always returned from a
  74. // function
  75. // functions need to be marked with a skip marker
  76. expect(content).toMatch(`props: _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: _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/ type declaration', () => {
  104. const { content } = compile(`
  105. <script setup lang="ts">
  106. const { foo = 1, bar = {}, func = () => {} } = defineProps<{ foo?: number, bar?: object, func?: () => any }>()
  107. </script>
  108. `)
  109. // literals can be used as-is, non-literals are always returned from a
  110. // function
  111. expect(content).toMatch(`props: {
  112. foo: { type: Number, required: false, default: 1 },
  113. bar: { type: Object, required: false, default: () => ({}) },
  114. func: { type: Function, required: false, default: () => {} }
  115. }`)
  116. assertCode(content)
  117. })
  118. test('default values w/ type declaration, prod mode', () => {
  119. const { content } = compile(
  120. `
  121. <script setup lang="ts">
  122. const { foo = 1, bar = {}, func = () => {} } = defineProps<{ foo?: number, bar?: object, baz?: any, boola?: boolean, boolb?: boolean | number, func?: Function }>()
  123. </script>
  124. `,
  125. { isProd: true }
  126. )
  127. assertCode(content)
  128. // literals can be used as-is, non-literals are always returned from a
  129. // function
  130. expect(content).toMatch(`props: {
  131. foo: { default: 1 },
  132. bar: { default: () => ({}) },
  133. baz: {},
  134. boola: { type: Boolean },
  135. boolb: { type: [Boolean, Number] },
  136. func: { type: Function, default: () => {} }
  137. }`)
  138. })
  139. test('aliasing', () => {
  140. const { content, bindings } = compile(`
  141. <script setup>
  142. const { foo: bar } = defineProps(['foo'])
  143. let x = foo
  144. let y = bar
  145. </script>
  146. <template>{{ foo + bar }}</template>
  147. `)
  148. expect(content).not.toMatch(`const { foo: bar } =`)
  149. expect(content).toMatch(`let x = foo`) // should not process
  150. expect(content).toMatch(`let y = __props.foo`)
  151. // should convert bar to __props.foo in template expressions
  152. expect(content).toMatch(`_toDisplayString(__props.foo + __props.foo)`)
  153. assertCode(content)
  154. expect(bindings).toStrictEqual({
  155. x: BindingTypes.SETUP_LET,
  156. y: BindingTypes.SETUP_LET,
  157. foo: BindingTypes.PROPS,
  158. bar: BindingTypes.PROPS_ALIASED,
  159. __propsAliases: {
  160. bar: 'foo'
  161. }
  162. })
  163. })
  164. // #5425
  165. test('non-identifier prop names', () => {
  166. const { content, bindings } = compile(`
  167. <script setup>
  168. const { 'foo.bar': fooBar } = defineProps({ 'foo.bar': Function })
  169. let x = fooBar
  170. </script>
  171. <template>{{ fooBar }}</template>
  172. `)
  173. expect(content).toMatch(`x = __props["foo.bar"]`)
  174. expect(content).toMatch(`toDisplayString(__props["foo.bar"])`)
  175. assertCode(content)
  176. expect(bindings).toStrictEqual({
  177. x: BindingTypes.SETUP_LET,
  178. 'foo.bar': BindingTypes.PROPS,
  179. fooBar: BindingTypes.PROPS_ALIASED,
  180. __propsAliases: {
  181. fooBar: 'foo.bar'
  182. }
  183. })
  184. })
  185. test('rest spread', () => {
  186. const { content, bindings } = compile(`
  187. <script setup>
  188. const { foo, bar, ...rest } = defineProps(['foo', 'bar', 'baz'])
  189. </script>
  190. `)
  191. expect(content).toMatch(
  192. `const rest = _createPropsRestProxy(__props, ["foo","bar"])`
  193. )
  194. assertCode(content)
  195. expect(bindings).toStrictEqual({
  196. foo: BindingTypes.PROPS,
  197. bar: BindingTypes.PROPS,
  198. baz: BindingTypes.PROPS,
  199. rest: BindingTypes.SETUP_REACTIVE_CONST
  200. })
  201. })
  202. // #6960
  203. test('computed static key', () => {
  204. const { content, bindings } = compile(`
  205. <script setup>
  206. const { ['foo']: foo } = defineProps(['foo'])
  207. console.log(foo)
  208. </script>
  209. <template>{{ foo }}</template>
  210. `)
  211. expect(content).not.toMatch(`const { foo } =`)
  212. expect(content).toMatch(`console.log(__props.foo)`)
  213. expect(content).toMatch(`_toDisplayString(__props.foo)`)
  214. assertCode(content)
  215. expect(bindings).toStrictEqual({
  216. foo: BindingTypes.PROPS
  217. })
  218. })
  219. describe('errors', () => {
  220. test('should error on deep destructure', () => {
  221. expect(() =>
  222. compile(
  223. `<script setup>const { foo: [bar] } = defineProps(['foo'])</script>`
  224. )
  225. ).toThrow(`destructure does not support nested patterns`)
  226. expect(() =>
  227. compile(
  228. `<script setup>const { foo: { bar } } = defineProps(['foo'])</script>`
  229. )
  230. ).toThrow(`destructure does not support nested patterns`)
  231. })
  232. test('should error on computed key', () => {
  233. expect(() =>
  234. compile(
  235. `<script setup>const { [foo]: bar } = defineProps(['foo'])</script>`
  236. )
  237. ).toThrow(`destructure cannot use computed key`)
  238. })
  239. test('should error when used with withDefaults', () => {
  240. expect(() =>
  241. compile(
  242. `<script setup lang="ts">
  243. const { foo } = withDefaults(defineProps<{ foo: string }>(), { foo: 'foo' })
  244. </script>`
  245. )
  246. ).toThrow(`withDefaults() is unnecessary when using destructure`)
  247. })
  248. test('should error if destructure reference local vars', () => {
  249. expect(() =>
  250. compile(
  251. `<script setup>
  252. let x = 1
  253. const {
  254. foo = () => x
  255. } = defineProps(['foo'])
  256. </script>`
  257. )
  258. ).toThrow(`cannot reference locally declared variables`)
  259. })
  260. test('should error if assignment to destructured prop binding', () => {
  261. expect(() =>
  262. compile(
  263. `<script setup>
  264. const { foo } = defineProps(['foo'])
  265. foo = 'bar'
  266. </script>`
  267. )
  268. ).toThrow(`Cannot assign to destructured props`)
  269. expect(() =>
  270. compile(
  271. `<script setup>
  272. let { foo } = defineProps(['foo'])
  273. foo = 'bar'
  274. </script>`
  275. )
  276. ).toThrow(`Cannot assign to destructured props`)
  277. })
  278. test('should error when passing destructured prop into certain methods', () => {
  279. expect(() =>
  280. compile(
  281. `<script setup>
  282. import { watch } from 'vue'
  283. const { foo } = defineProps(['foo'])
  284. watch(foo, () => {})
  285. </script>`
  286. )
  287. ).toThrow(
  288. `"foo" is a destructured prop and should not be passed directly to watch().`
  289. )
  290. expect(() =>
  291. compile(
  292. `<script setup>
  293. import { watch as w } from 'vue'
  294. const { foo } = defineProps(['foo'])
  295. w(foo, () => {})
  296. </script>`
  297. )
  298. ).toThrow(
  299. `"foo" is a destructured prop and should not be passed directly to watch().`
  300. )
  301. expect(() =>
  302. compile(
  303. `<script setup>
  304. import { toRef } from 'vue'
  305. const { foo } = defineProps(['foo'])
  306. toRef(foo)
  307. </script>`
  308. )
  309. ).toThrow(
  310. `"foo" is a destructured prop and should not be passed directly to toRef().`
  311. )
  312. expect(() =>
  313. compile(
  314. `<script setup>
  315. import { toRef as r } from 'vue'
  316. const { foo } = defineProps(['foo'])
  317. r(foo)
  318. </script>`
  319. )
  320. ).toThrow(
  321. `"foo" is a destructured prop and should not be passed directly to toRef().`
  322. )
  323. })
  324. // not comprehensive, but should help for most common cases
  325. test('should error if default value type does not match declared type', () => {
  326. expect(() =>
  327. compile(
  328. `<script setup lang="ts">
  329. const { foo = 'hello' } = defineProps<{ foo?: number }>()
  330. </script>`
  331. )
  332. ).toThrow(`Default value of prop "foo" does not match declared type.`)
  333. })
  334. // #8017
  335. test('should not throw an error if the variable is not a props', () => {
  336. expect(() =>
  337. compile(
  338. `<script setup lang='ts'>
  339. import { watch } from 'vue'
  340. const { userId } = defineProps({ userId: Number })
  341. const { error: e, info } = useRequest();
  342. watch(e, () => {});
  343. watch(info, () => {});
  344. </script>`
  345. )
  346. ).not.toThrowError()
  347. })
  348. })
  349. })