2
0

compileScript.spec.ts 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798
  1. import { BindingTypes } from '@vue/compiler-core'
  2. import { compileSFCScript as compile, assertCode, mockId } from './utils'
  3. describe('SFC compile <script setup>', () => {
  4. test('should expose top level declarations', () => {
  5. const { content, bindings } = compile(`
  6. <script setup>
  7. import { x } from './x'
  8. let a = 1
  9. const b = 2
  10. function c() {}
  11. class d {}
  12. </script>
  13. <script>
  14. import { xx } from './x'
  15. let aa = 1
  16. const bb = 2
  17. function cc() {}
  18. class dd {}
  19. </script>
  20. `)
  21. expect(content).toMatch('return { aa, bb, cc, dd, a, b, c, d, xx, x }')
  22. expect(bindings).toStrictEqual({
  23. x: BindingTypes.SETUP_MAYBE_REF,
  24. a: BindingTypes.SETUP_LET,
  25. b: BindingTypes.SETUP_CONST,
  26. c: BindingTypes.SETUP_CONST,
  27. d: BindingTypes.SETUP_CONST,
  28. xx: BindingTypes.SETUP_MAYBE_REF,
  29. aa: BindingTypes.SETUP_LET,
  30. bb: BindingTypes.SETUP_CONST,
  31. cc: BindingTypes.SETUP_CONST,
  32. dd: BindingTypes.SETUP_CONST
  33. })
  34. assertCode(content)
  35. })
  36. test('binding analysis for destructure', () => {
  37. const { content, bindings } = compile(`
  38. <script setup>
  39. const { foo, b: bar, ['x' + 'y']: baz, x: { y, zz: { z }}} = {}
  40. </script>
  41. `)
  42. expect(content).toMatch('return { foo, bar, baz, y, z }')
  43. expect(bindings).toStrictEqual({
  44. foo: BindingTypes.SETUP_MAYBE_REF,
  45. bar: BindingTypes.SETUP_MAYBE_REF,
  46. baz: BindingTypes.SETUP_MAYBE_REF,
  47. y: BindingTypes.SETUP_MAYBE_REF,
  48. z: BindingTypes.SETUP_MAYBE_REF
  49. })
  50. assertCode(content)
  51. })
  52. test('defineProps()', () => {
  53. const { content, bindings } = compile(`
  54. <script setup>
  55. const props = defineProps({
  56. foo: String
  57. })
  58. const bar = 1
  59. </script>
  60. `)
  61. // should generate working code
  62. assertCode(content)
  63. // should analyze bindings
  64. expect(bindings).toStrictEqual({
  65. foo: BindingTypes.PROPS,
  66. bar: BindingTypes.SETUP_CONST,
  67. props: BindingTypes.SETUP_REACTIVE_CONST
  68. })
  69. // should remove defineOptions import and call
  70. expect(content).not.toMatch('defineProps')
  71. // should generate correct setup signature
  72. expect(content).toMatch(`setup(__props, { expose }) {`)
  73. // should assign user identifier to it
  74. expect(content).toMatch(`const props = __props`)
  75. // should include context options in default export
  76. expect(content).toMatch(`export default {
  77. props: {
  78. foo: String
  79. },`)
  80. })
  81. test('defineProps w/ external definition', () => {
  82. const { content } = compile(`
  83. <script setup>
  84. import { propsModel } from './props'
  85. const props = defineProps(propsModel)
  86. </script>
  87. `)
  88. assertCode(content)
  89. expect(content).toMatch(`export default {
  90. props: propsModel,`)
  91. })
  92. // #4764
  93. test('defineProps w/ leading code', () => {
  94. const { content } = compile(`
  95. <script setup>import { x } from './x'
  96. const props = defineProps({})
  97. </script>
  98. `)
  99. // props declaration should be inside setup, not moved along with the import
  100. expect(content).not.toMatch(`const props = __props\nimport`)
  101. assertCode(content)
  102. })
  103. test('defineEmits()', () => {
  104. const { content, bindings } = compile(`
  105. <script setup>
  106. const myEmit = defineEmits(['foo', 'bar'])
  107. </script>
  108. `)
  109. assertCode(content)
  110. expect(bindings).toStrictEqual({
  111. myEmit: BindingTypes.SETUP_CONST
  112. })
  113. // should remove defineOptions import and call
  114. expect(content).not.toMatch('defineEmits')
  115. // should generate correct setup signature
  116. expect(content).toMatch(`setup(__props, { expose, emit: myEmit }) {`)
  117. // should include context options in default export
  118. expect(content).toMatch(`export default {
  119. emits: ['foo', 'bar'],`)
  120. })
  121. test('defineProps/defineEmits in multi-variable declaration', () => {
  122. const { content } = compile(`
  123. <script setup>
  124. const props = defineProps(['item']),
  125. a = 1,
  126. emit = defineEmits(['a']);
  127. </script>
  128. `)
  129. assertCode(content)
  130. expect(content).toMatch(`const a = 1;`) // test correct removal
  131. expect(content).toMatch(`props: ['item'],`)
  132. expect(content).toMatch(`emits: ['a'],`)
  133. })
  134. // #6757
  135. test('defineProps/defineEmits in multi-variable declaration fix #6757 ', () => {
  136. const { content } = compile(`
  137. <script setup>
  138. const a = 1,
  139. props = defineProps(['item']),
  140. emit = defineEmits(['a']);
  141. </script>
  142. `)
  143. assertCode(content)
  144. expect(content).toMatch(`const a = 1;`) // test correct removal
  145. expect(content).toMatch(`props: ['item'],`)
  146. expect(content).toMatch(`emits: ['a'],`)
  147. })
  148. test('defineProps/defineEmits in multi-variable declaration (full removal)', () => {
  149. const { content } = compile(`
  150. <script setup>
  151. const props = defineProps(['item']),
  152. emit = defineEmits(['a']);
  153. </script>
  154. `)
  155. assertCode(content)
  156. expect(content).toMatch(`props: ['item'],`)
  157. expect(content).toMatch(`emits: ['a'],`)
  158. })
  159. test('defineExpose()', () => {
  160. const { content } = compile(`
  161. <script setup>
  162. defineExpose({ foo: 123 })
  163. </script>
  164. `)
  165. assertCode(content)
  166. // should remove defineOptions import and call
  167. expect(content).not.toMatch('defineExpose')
  168. // should generate correct setup signature
  169. expect(content).toMatch(`setup(__props, { expose }) {`)
  170. // should replace callee
  171. expect(content).toMatch(/\bexpose\(\{ foo: 123 \}\)/)
  172. })
  173. test('<script> after <script setup> the script content not end with `\\n`', () => {
  174. const { content } = compile(`
  175. <script setup>
  176. import { x } from './x'
  177. </script>
  178. <script>const n = 1</script>
  179. `)
  180. assertCode(content)
  181. })
  182. describe('<script> and <script setup> co-usage', () => {
  183. test('script first', () => {
  184. const { content } = compile(`
  185. <script>
  186. export const n = 1
  187. export default {}
  188. </script>
  189. <script setup>
  190. import { x } from './x'
  191. x()
  192. </script>
  193. `)
  194. assertCode(content)
  195. })
  196. test('script setup first', () => {
  197. const { content } = compile(`
  198. <script setup>
  199. import { x } from './x'
  200. x()
  201. </script>
  202. <script>
  203. export const n = 1
  204. export default {}
  205. </script>
  206. `)
  207. assertCode(content)
  208. })
  209. test('script setup first, named default export', () => {
  210. const { content } = compile(`
  211. <script setup>
  212. import { x } from './x'
  213. x()
  214. </script>
  215. <script>
  216. export const n = 1
  217. const def = {}
  218. export { def as default }
  219. </script>
  220. `)
  221. assertCode(content)
  222. })
  223. // #4395
  224. test('script setup first, lang="ts", script block content export default', () => {
  225. const { content } = compile(`
  226. <script setup lang="ts">
  227. import { x } from './x'
  228. x()
  229. </script>
  230. <script lang="ts">
  231. export default {
  232. name: "test"
  233. }
  234. </script>
  235. `)
  236. // ensure __default__ is declared before used
  237. expect(content).toMatch(/const __default__[\S\s]*\.\.\.__default__/m)
  238. assertCode(content)
  239. })
  240. describe('spaces in ExportDefaultDeclaration node', () => {
  241. // #4371
  242. test('with many spaces and newline', () => {
  243. // #4371
  244. const { content } = compile(`
  245. <script>
  246. export const n = 1
  247. export default
  248. {
  249. some:'option'
  250. }
  251. </script>
  252. <script setup>
  253. import { x } from './x'
  254. x()
  255. </script>
  256. `)
  257. assertCode(content)
  258. })
  259. test('with minimal spaces', () => {
  260. const { content } = compile(`
  261. <script>
  262. export const n = 1
  263. export default{
  264. some:'option'
  265. }
  266. </script>
  267. <script setup>
  268. import { x } from './x'
  269. x()
  270. </script>
  271. `)
  272. assertCode(content)
  273. })
  274. })
  275. })
  276. describe('imports', () => {
  277. test('should hoist and expose imports', () => {
  278. assertCode(
  279. compile(`<script setup>
  280. import { ref } from 'vue'
  281. import 'foo/css'
  282. </script>`).content
  283. )
  284. })
  285. test('should extract comment for import or type declarations', () => {
  286. assertCode(
  287. compile(`
  288. <script setup>
  289. import a from 'a' // comment
  290. import b from 'b'
  291. </script>
  292. `).content
  293. )
  294. })
  295. // #2740
  296. test('should allow defineProps/Emit at the start of imports', () => {
  297. assertCode(
  298. compile(`<script setup>
  299. import { ref } from 'vue'
  300. defineProps(['foo'])
  301. defineEmits(['bar'])
  302. const r = ref(0)
  303. </script>`).content
  304. )
  305. })
  306. test('dedupe between user & helper', () => {
  307. const { content } = compile(
  308. `
  309. <script setup>
  310. import { ref } from 'vue'
  311. let foo = $ref(1)
  312. </script>
  313. `,
  314. { reactivityTransform: true }
  315. )
  316. assertCode(content)
  317. expect(content).toMatch(`import { ref } from 'vue'`)
  318. })
  319. test('import dedupe between <script> and <script setup>', () => {
  320. const { content } = compile(`
  321. <script>
  322. import { x } from './x'
  323. </script>
  324. <script setup>
  325. import { x } from './x'
  326. x()
  327. </script>
  328. `)
  329. assertCode(content)
  330. expect(content.indexOf(`import { x }`)).toEqual(
  331. content.lastIndexOf(`import { x }`)
  332. )
  333. })
  334. describe('import ref/reactive function from other place', () => {
  335. test('import directly', () => {
  336. const { bindings } = compile(`
  337. <script setup>
  338. import { ref, reactive } from './foo'
  339. const foo = ref(1)
  340. const bar = reactive(1)
  341. </script>
  342. `)
  343. expect(bindings).toStrictEqual({
  344. ref: BindingTypes.SETUP_MAYBE_REF,
  345. reactive: BindingTypes.SETUP_MAYBE_REF,
  346. foo: BindingTypes.SETUP_MAYBE_REF,
  347. bar: BindingTypes.SETUP_MAYBE_REF
  348. })
  349. })
  350. test('import w/ alias', () => {
  351. const { bindings } = compile(`
  352. <script setup>
  353. import { ref as _ref, reactive as _reactive } from './foo'
  354. const foo = ref(1)
  355. const bar = reactive(1)
  356. </script>
  357. `)
  358. expect(bindings).toStrictEqual({
  359. _reactive: BindingTypes.SETUP_MAYBE_REF,
  360. _ref: BindingTypes.SETUP_MAYBE_REF,
  361. foo: BindingTypes.SETUP_REF,
  362. bar: BindingTypes.SETUP_REACTIVE_CONST
  363. })
  364. })
  365. })
  366. })
  367. // in dev mode, declared bindings are returned as an object from setup()
  368. // when using TS, users may import types which should not be returned as
  369. // values, so we need to check import usage in the template to determine
  370. // what to be returned.
  371. describe('dev mode import usage check', () => {
  372. test('components', () => {
  373. const { content } = compile(`
  374. <script setup lang="ts">
  375. import { FooBar, FooBaz, FooQux, foo } from './x'
  376. const fooBar: FooBar = 1
  377. </script>
  378. <template>
  379. <FooBaz></FooBaz>
  380. <foo-qux/>
  381. <foo/>
  382. FooBar
  383. </template>
  384. `)
  385. // FooBar: should not be matched by plain text or incorrect case
  386. // FooBaz: used as PascalCase component
  387. // FooQux: used as kebab-case component
  388. // foo: lowercase component
  389. expect(content).toMatch(`return { fooBar, FooBaz, FooQux, foo }`)
  390. assertCode(content)
  391. })
  392. test('directive', () => {
  393. const { content } = compile(`
  394. <script setup lang="ts">
  395. import { vMyDir } from './x'
  396. </script>
  397. <template>
  398. <div v-my-dir></div>
  399. </template>
  400. `)
  401. expect(content).toMatch(`return { vMyDir }`)
  402. assertCode(content)
  403. })
  404. // https://github.com/vuejs/core/issues/4599
  405. test('attribute expressions', () => {
  406. const { content } = compile(`
  407. <script setup lang="ts">
  408. import { bar, baz } from './x'
  409. const cond = true
  410. </script>
  411. <template>
  412. <div :class="[cond ? '' : bar(), 'default']" :style="baz"></div>
  413. </template>
  414. `)
  415. expect(content).toMatch(`return { cond, bar, baz }`)
  416. assertCode(content)
  417. })
  418. test('vue interpolations', () => {
  419. const { content } = compile(`
  420. <script setup lang="ts">
  421. import { x, y, z, x$y } from './x'
  422. </script>
  423. <template>
  424. <div :id="z + 'y'">{{ x }} {{ yy }} {{ x$y }}</div>
  425. </template>
  426. `)
  427. // x: used in interpolation
  428. // y: should not be matched by {{ yy }} or 'y' in binding exps
  429. // x$y: #4274 should escape special chars when creating Regex
  430. expect(content).toMatch(`return { x, z, x$y }`)
  431. assertCode(content)
  432. })
  433. // #4340 interpolations in template strings
  434. test('js template string interpolations', () => {
  435. const { content } = compile(`
  436. <script setup lang="ts">
  437. import { VAR, VAR2, VAR3 } from './x'
  438. </script>
  439. <template>
  440. {{ \`\${VAR}VAR2\${VAR3}\` }}
  441. </template>
  442. `)
  443. // VAR2 should not be matched
  444. expect(content).toMatch(`return { VAR, VAR3 }`)
  445. assertCode(content)
  446. })
  447. // edge case: last tag in template
  448. test('last tag', () => {
  449. const { content } = compile(`
  450. <script setup lang="ts">
  451. import { FooBaz, Last } from './x'
  452. </script>
  453. <template>
  454. <FooBaz></FooBaz>
  455. <Last/>
  456. </template>
  457. `)
  458. expect(content).toMatch(`return { FooBaz, Last }`)
  459. assertCode(content)
  460. })
  461. test('TS annotations', () => {
  462. const { content } = compile(`
  463. <script setup lang="ts">
  464. import { Foo, Bar, Baz, Qux, Fred } from './x'
  465. const a = 1
  466. function b() {}
  467. </script>
  468. <template>
  469. {{ a as Foo }}
  470. {{ b<Bar>() }}
  471. {{ Baz }}
  472. <Comp v-slot="{ data }: Qux">{{ data }}</Comp>
  473. <div v-for="{ z = x as Qux } in list as Fred"/>
  474. </template>
  475. `)
  476. expect(content).toMatch(`return { a, b, Baz }`)
  477. assertCode(content)
  478. })
  479. // vuejs/vue#12591
  480. test('v-on inline statement', () => {
  481. // should not error
  482. compile(`
  483. <script setup lang="ts">
  484. import { foo } from './foo'
  485. </script>
  486. <template>
  487. <div @click="$emit('update:a');"></div>
  488. </template>
  489. `)
  490. })
  491. })
  492. describe('inlineTemplate mode', () => {
  493. test('should work', () => {
  494. const { content } = compile(
  495. `
  496. <script setup>
  497. import { ref } from 'vue'
  498. const count = ref(0)
  499. </script>
  500. <template>
  501. <div>{{ count }}</div>
  502. <div>static</div>
  503. </template>
  504. `,
  505. { inlineTemplate: true }
  506. )
  507. // check snapshot and make sure helper imports and
  508. // hoists are placed correctly.
  509. assertCode(content)
  510. // in inline mode, no need to call expose() since nothing is exposed
  511. // anyway!
  512. expect(content).not.toMatch(`expose()`)
  513. })
  514. test('with defineExpose()', () => {
  515. const { content } = compile(
  516. `
  517. <script setup>
  518. const count = ref(0)
  519. defineExpose({ count })
  520. </script>
  521. `,
  522. { inlineTemplate: true }
  523. )
  524. assertCode(content)
  525. expect(content).toMatch(`setup(__props, { expose })`)
  526. expect(content).toMatch(`expose({ count })`)
  527. })
  528. test('referencing scope components and directives', () => {
  529. const { content } = compile(
  530. `
  531. <script setup>
  532. import ChildComp from './Child.vue'
  533. import SomeOtherComp from './Other.vue'
  534. import vMyDir from './my-dir'
  535. </script>
  536. <template>
  537. <div v-my-dir></div>
  538. <ChildComp/>
  539. <some-other-comp/>
  540. </template>
  541. `,
  542. { inlineTemplate: true }
  543. )
  544. expect(content).toMatch('[_unref(vMyDir)]')
  545. expect(content).toMatch('_createVNode(ChildComp)')
  546. // kebab-case component support
  547. expect(content).toMatch('_createVNode(SomeOtherComp)')
  548. assertCode(content)
  549. })
  550. test('avoid unref() when necessary', () => {
  551. // function, const, component import
  552. const { content } = compile(
  553. `<script setup>
  554. import { ref } from 'vue'
  555. import Foo, { bar } from './Foo.vue'
  556. import other from './util'
  557. import * as tree from './tree'
  558. const count = ref(0)
  559. const constant = {}
  560. const maybe = foo()
  561. let lett = 1
  562. function fn() {}
  563. </script>
  564. <template>
  565. <Foo>{{ bar }}</Foo>
  566. <div @click="fn">{{ count }} {{ constant }} {{ maybe }} {{ lett }} {{ other }}</div>
  567. {{ tree.foo() }}
  568. </template>
  569. `,
  570. { inlineTemplate: true }
  571. )
  572. // no need to unref vue component import
  573. expect(content).toMatch(`createVNode(Foo,`)
  574. // #2699 should unref named imports from .vue
  575. expect(content).toMatch(`unref(bar)`)
  576. // should unref other imports
  577. expect(content).toMatch(`unref(other)`)
  578. // no need to unref constant literals
  579. expect(content).not.toMatch(`unref(constant)`)
  580. // should directly use .value for known refs
  581. expect(content).toMatch(`count.value`)
  582. // should unref() on const bindings that may be refs
  583. expect(content).toMatch(`unref(maybe)`)
  584. // should unref() on let bindings
  585. expect(content).toMatch(`unref(lett)`)
  586. // no need to unref namespace import (this also preserves tree-shaking)
  587. expect(content).toMatch(`tree.foo()`)
  588. // no need to unref function declarations
  589. expect(content).toMatch(`{ onClick: fn }`)
  590. // no need to mark constant fns in patch flag
  591. expect(content).not.toMatch(`PROPS`)
  592. assertCode(content)
  593. })
  594. test('v-model codegen', () => {
  595. const { content } = compile(
  596. `<script setup>
  597. import { ref } from 'vue'
  598. const count = ref(0)
  599. const maybe = foo()
  600. let lett = 1
  601. </script>
  602. <template>
  603. <input v-model="count">
  604. <input v-model="maybe">
  605. <input v-model="lett">
  606. </template>
  607. `,
  608. { inlineTemplate: true }
  609. )
  610. // known const ref: set value
  611. expect(content).toMatch(`(count).value = $event`)
  612. // const but maybe ref: assign if ref, otherwise do nothing
  613. expect(content).toMatch(`_isRef(maybe) ? (maybe).value = $event : null`)
  614. // let: handle both cases
  615. expect(content).toMatch(
  616. `_isRef(lett) ? (lett).value = $event : lett = $event`
  617. )
  618. assertCode(content)
  619. })
  620. test('template assignment expression codegen', () => {
  621. const { content } = compile(
  622. `<script setup>
  623. import { ref } from 'vue'
  624. const count = ref(0)
  625. const maybe = foo()
  626. let lett = 1
  627. let v = ref(1)
  628. </script>
  629. <template>
  630. <div @click="count = 1"/>
  631. <div @click="maybe = count"/>
  632. <div @click="lett = count"/>
  633. <div @click="v += 1"/>
  634. <div @click="v -= 1"/>
  635. <div @click="() => {
  636. let a = '' + lett
  637. v = a
  638. }"/>
  639. <div @click="() => {
  640. // nested scopes
  641. (()=>{
  642. let x = a
  643. (()=>{
  644. let z = x
  645. let z2 = z
  646. })
  647. let lz = z
  648. })
  649. v = a
  650. }"/>
  651. </template>
  652. `,
  653. { inlineTemplate: true }
  654. )
  655. // known const ref: set value
  656. expect(content).toMatch(`count.value = 1`)
  657. // const but maybe ref: only assign after check
  658. expect(content).toMatch(`maybe.value = count.value`)
  659. // let: handle both cases
  660. expect(content).toMatch(
  661. `_isRef(lett) ? lett.value = count.value : lett = count.value`
  662. )
  663. expect(content).toMatch(`_isRef(v) ? v.value += 1 : v += 1`)
  664. expect(content).toMatch(`_isRef(v) ? v.value -= 1 : v -= 1`)
  665. expect(content).toMatch(`_isRef(v) ? v.value = a : v = a`)
  666. expect(content).toMatch(`_isRef(v) ? v.value = _ctx.a : v = _ctx.a`)
  667. assertCode(content)
  668. })
  669. test('template update expression codegen', () => {
  670. const { content } = compile(
  671. `<script setup>
  672. import { ref } from 'vue'
  673. const count = ref(0)
  674. const maybe = foo()
  675. let lett = 1
  676. </script>
  677. <template>
  678. <div @click="count++"/>
  679. <div @click="--count"/>
  680. <div @click="maybe++"/>
  681. <div @click="--maybe"/>
  682. <div @click="lett++"/>
  683. <div @click="--lett"/>
  684. </template>
  685. `,
  686. { inlineTemplate: true }
  687. )
  688. // known const ref: set value
  689. expect(content).toMatch(`count.value++`)
  690. expect(content).toMatch(`--count.value`)
  691. // const but maybe ref (non-ref case ignored)
  692. expect(content).toMatch(`maybe.value++`)
  693. expect(content).toMatch(`--maybe.value`)
  694. // let: handle both cases
  695. expect(content).toMatch(`_isRef(lett) ? lett.value++ : lett++`)
  696. expect(content).toMatch(`_isRef(lett) ? --lett.value : --lett`)
  697. assertCode(content)
  698. })
  699. test('template destructure assignment codegen', () => {
  700. const { content } = compile(
  701. `<script setup>
  702. import { ref } from 'vue'
  703. const val = {}
  704. const count = ref(0)
  705. const maybe = foo()
  706. let lett = 1
  707. </script>
  708. <template>
  709. <div @click="({ count } = val)"/>
  710. <div @click="[maybe] = val"/>
  711. <div @click="({ lett } = val)"/>
  712. </template>
  713. `,
  714. { inlineTemplate: true }
  715. )
  716. // known const ref: set value
  717. expect(content).toMatch(`({ count: count.value } = val)`)
  718. // const but maybe ref (non-ref case ignored)
  719. expect(content).toMatch(`[maybe.value] = val`)
  720. // let: assumes non-ref
  721. expect(content).toMatch(`{ lett: lett } = val`)
  722. assertCode(content)
  723. })
  724. test('ssr codegen', () => {
  725. const { content } = compile(
  726. `
  727. <script setup>
  728. import { ref } from 'vue'
  729. const count = ref(0)
  730. </script>
  731. <template>
  732. <div>{{ count }}</div>
  733. <div>static</div>
  734. </template>
  735. <style>
  736. div { color: v-bind(count) }
  737. </style>
  738. `,
  739. {
  740. inlineTemplate: true,
  741. templateOptions: {
  742. ssr: true
  743. }
  744. }
  745. )
  746. expect(content).toMatch(`\n __ssrInlineRender: true,\n`)
  747. expect(content).toMatch(`return (_ctx, _push`)
  748. expect(content).toMatch(`ssrInterpolate`)
  749. expect(content).not.toMatch(`useCssVars`)
  750. expect(content).toMatch(`"--${mockId}-count": (count.value)`)
  751. assertCode(content)
  752. })
  753. })
  754. describe('with TypeScript', () => {
  755. test('hoist type declarations', () => {
  756. const { content } = compile(`
  757. <script setup lang="ts">
  758. export interface Foo {}
  759. type Bar = {}
  760. </script>`)
  761. assertCode(content)
  762. })
  763. test('defineProps/Emit w/ runtime options', () => {
  764. const { content } = compile(`
  765. <script setup lang="ts">
  766. const props = defineProps({ foo: String })
  767. const emit = defineEmits(['a', 'b'])
  768. </script>
  769. `)
  770. assertCode(content)
  771. expect(content).toMatch(`export default /*#__PURE__*/_defineComponent({
  772. props: { foo: String },
  773. emits: ['a', 'b'],
  774. setup(__props, { expose, emit }) {`)
  775. })
  776. test('defineProps w/ type', () => {
  777. const { content, bindings } = compile(`
  778. <script setup lang="ts">
  779. interface Test {}
  780. type Alias = number[]
  781. defineProps<{
  782. string: string
  783. number: number
  784. boolean: boolean
  785. object: object
  786. objectLiteral: { a: number }
  787. fn: (n: number) => void
  788. functionRef: Function
  789. objectRef: Object
  790. dateTime: Date
  791. array: string[]
  792. arrayRef: Array<any>
  793. tuple: [number, number]
  794. set: Set<string>
  795. literal: 'foo'
  796. optional?: any
  797. recordRef: Record<string, null>
  798. interface: Test
  799. alias: Alias
  800. method(): void
  801. symbol: symbol
  802. union: string | number
  803. literalUnion: 'foo' | 'bar'
  804. literalUnionNumber: 1 | 2 | 3 | 4 | 5
  805. literalUnionMixed: 'foo' | 1 | boolean
  806. intersection: Test & {}
  807. foo: ((item: any) => boolean) | null
  808. }>()
  809. </script>`)
  810. assertCode(content)
  811. expect(content).toMatch(`string: { type: String, required: true }`)
  812. expect(content).toMatch(`number: { type: Number, required: true }`)
  813. expect(content).toMatch(`boolean: { type: Boolean, required: true }`)
  814. expect(content).toMatch(`object: { type: Object, required: true }`)
  815. expect(content).toMatch(`objectLiteral: { type: Object, required: true }`)
  816. expect(content).toMatch(`fn: { type: Function, required: true }`)
  817. expect(content).toMatch(`functionRef: { type: Function, required: true }`)
  818. expect(content).toMatch(`objectRef: { type: Object, required: true }`)
  819. expect(content).toMatch(`dateTime: { type: Date, required: true }`)
  820. expect(content).toMatch(`array: { type: Array, required: true }`)
  821. expect(content).toMatch(`arrayRef: { type: Array, required: true }`)
  822. expect(content).toMatch(`tuple: { type: Array, required: true }`)
  823. expect(content).toMatch(`set: { type: Set, required: true }`)
  824. expect(content).toMatch(`literal: { type: String, required: true }`)
  825. expect(content).toMatch(`optional: { type: null, required: false }`)
  826. expect(content).toMatch(`recordRef: { type: Object, required: true }`)
  827. expect(content).toMatch(`interface: { type: Object, required: true }`)
  828. expect(content).toMatch(`alias: { type: Array, required: true }`)
  829. expect(content).toMatch(`method: { type: Function, required: true }`)
  830. expect(content).toMatch(`symbol: { type: Symbol, required: true }`)
  831. expect(content).toMatch(
  832. `union: { type: [String, Number], required: true }`
  833. )
  834. expect(content).toMatch(`literalUnion: { type: String, required: true }`)
  835. expect(content).toMatch(
  836. `literalUnionNumber: { type: Number, required: true }`
  837. )
  838. expect(content).toMatch(
  839. `literalUnionMixed: { type: [String, Number, Boolean], required: true }`
  840. )
  841. expect(content).toMatch(`intersection: { type: Object, required: true }`)
  842. expect(content).toMatch(`foo: { type: [Function, null], required: true }`)
  843. expect(bindings).toStrictEqual({
  844. string: BindingTypes.PROPS,
  845. number: BindingTypes.PROPS,
  846. boolean: BindingTypes.PROPS,
  847. object: BindingTypes.PROPS,
  848. objectLiteral: BindingTypes.PROPS,
  849. fn: BindingTypes.PROPS,
  850. functionRef: BindingTypes.PROPS,
  851. objectRef: BindingTypes.PROPS,
  852. dateTime: BindingTypes.PROPS,
  853. array: BindingTypes.PROPS,
  854. arrayRef: BindingTypes.PROPS,
  855. tuple: BindingTypes.PROPS,
  856. set: BindingTypes.PROPS,
  857. literal: BindingTypes.PROPS,
  858. optional: BindingTypes.PROPS,
  859. recordRef: BindingTypes.PROPS,
  860. interface: BindingTypes.PROPS,
  861. alias: BindingTypes.PROPS,
  862. method: BindingTypes.PROPS,
  863. symbol: BindingTypes.PROPS,
  864. union: BindingTypes.PROPS,
  865. literalUnion: BindingTypes.PROPS,
  866. literalUnionNumber: BindingTypes.PROPS,
  867. literalUnionMixed: BindingTypes.PROPS,
  868. intersection: BindingTypes.PROPS,
  869. foo: BindingTypes.PROPS
  870. })
  871. })
  872. test('defineProps w/ interface', () => {
  873. const { content, bindings } = compile(`
  874. <script setup lang="ts">
  875. interface Props { x?: number }
  876. defineProps<Props>()
  877. </script>
  878. `)
  879. assertCode(content)
  880. expect(content).toMatch(`x: { type: Number, required: false }`)
  881. expect(bindings).toStrictEqual({
  882. x: BindingTypes.PROPS
  883. })
  884. })
  885. test('defineProps w/ extends interface', () => {
  886. const { content, bindings } = compile(`
  887. <script lang="ts">
  888. interface Foo { x?: number }
  889. </script>
  890. <script setup lang="ts">
  891. interface Bar extends Foo { y?: number }
  892. interface Props extends Bar {
  893. z: number
  894. y: string
  895. }
  896. defineProps<Props>()
  897. </script>
  898. `)
  899. assertCode(content)
  900. expect(content).toMatch(`z: { type: Number, required: true }`)
  901. expect(content).toMatch(`y: { type: String, required: true }`)
  902. expect(content).toMatch(`x: { type: Number, required: false }`)
  903. expect(bindings).toStrictEqual({
  904. x: BindingTypes.PROPS,
  905. y: BindingTypes.PROPS,
  906. z: BindingTypes.PROPS
  907. })
  908. })
  909. test('defineProps w/ exported interface', () => {
  910. const { content, bindings } = compile(`
  911. <script setup lang="ts">
  912. export interface Props { x?: number }
  913. defineProps<Props>()
  914. </script>
  915. `)
  916. assertCode(content)
  917. expect(content).toMatch(`x: { type: Number, required: false }`)
  918. expect(bindings).toStrictEqual({
  919. x: BindingTypes.PROPS
  920. })
  921. })
  922. test('defineProps w/ exported interface in normal script', () => {
  923. const { content, bindings } = compile(`
  924. <script lang="ts">
  925. export interface Props { x?: number }
  926. </script>
  927. <script setup lang="ts">
  928. defineProps<Props>()
  929. </script>
  930. `)
  931. assertCode(content)
  932. expect(content).toMatch(`x: { type: Number, required: false }`)
  933. expect(bindings).toStrictEqual({
  934. x: BindingTypes.PROPS
  935. })
  936. })
  937. test('defineProps w/ type alias', () => {
  938. const { content, bindings } = compile(`
  939. <script setup lang="ts">
  940. type Props = { x?: number }
  941. defineProps<Props>()
  942. </script>
  943. `)
  944. assertCode(content)
  945. expect(content).toMatch(`x: { type: Number, required: false }`)
  946. expect(bindings).toStrictEqual({
  947. x: BindingTypes.PROPS
  948. })
  949. })
  950. test('defineProps w/ exported type alias', () => {
  951. const { content, bindings } = compile(`
  952. <script setup lang="ts">
  953. export type Props = { x?: number }
  954. defineProps<Props>()
  955. </script>
  956. `)
  957. assertCode(content)
  958. expect(content).toMatch(`x: { type: Number, required: false }`)
  959. expect(bindings).toStrictEqual({
  960. x: BindingTypes.PROPS
  961. })
  962. })
  963. test('withDefaults (static)', () => {
  964. const { content, bindings } = compile(`
  965. <script setup lang="ts">
  966. const props = withDefaults(defineProps<{
  967. foo?: string
  968. bar?: number;
  969. baz: boolean;
  970. qux?(): number
  971. }>(), {
  972. foo: 'hi',
  973. qux() { return 1 }
  974. })
  975. </script>
  976. `)
  977. assertCode(content)
  978. expect(content).toMatch(
  979. `foo: { type: String, required: false, default: 'hi' }`
  980. )
  981. expect(content).toMatch(`bar: { type: Number, required: false }`)
  982. expect(content).toMatch(`baz: { type: Boolean, required: true }`)
  983. expect(content).toMatch(
  984. `qux: { type: Function, required: false, default() { return 1 } }`
  985. )
  986. expect(content).toMatch(
  987. `{ foo: string, bar?: number, baz: boolean, qux(): number }`
  988. )
  989. expect(content).toMatch(`const props = __props`)
  990. expect(bindings).toStrictEqual({
  991. foo: BindingTypes.PROPS,
  992. bar: BindingTypes.PROPS,
  993. baz: BindingTypes.PROPS,
  994. qux: BindingTypes.PROPS,
  995. props: BindingTypes.SETUP_CONST
  996. })
  997. })
  998. test('withDefaults (dynamic)', () => {
  999. const { content } = compile(`
  1000. <script setup lang="ts">
  1001. import { defaults } from './foo'
  1002. const props = withDefaults(defineProps<{
  1003. foo?: string
  1004. bar?: number
  1005. baz: boolean
  1006. }>(), { ...defaults })
  1007. </script>
  1008. `)
  1009. assertCode(content)
  1010. expect(content).toMatch(`import { mergeDefaults as _mergeDefaults`)
  1011. expect(content).toMatch(
  1012. `
  1013. _mergeDefaults({
  1014. foo: { type: String, required: false },
  1015. bar: { type: Number, required: false },
  1016. baz: { type: Boolean, required: true }
  1017. }, { ...defaults })`.trim()
  1018. )
  1019. })
  1020. test('defineEmits w/ type', () => {
  1021. const { content } = compile(`
  1022. <script setup lang="ts">
  1023. const emit = defineEmits<(e: 'foo' | 'bar') => void>()
  1024. </script>
  1025. `)
  1026. assertCode(content)
  1027. expect(content).toMatch(`emit: ((e: 'foo' | 'bar') => void),`)
  1028. expect(content).toMatch(`emits: ["foo", "bar"]`)
  1029. })
  1030. test('defineEmits w/ type (union)', () => {
  1031. const type = `((e: 'foo' | 'bar') => void) | ((e: 'baz', id: number) => void)`
  1032. expect(() =>
  1033. compile(`
  1034. <script setup lang="ts">
  1035. const emit = defineEmits<${type}>()
  1036. </script>
  1037. `)
  1038. ).toThrow()
  1039. })
  1040. test('defineEmits w/ type (type literal w/ call signatures)', () => {
  1041. const type = `{(e: 'foo' | 'bar'): void; (e: 'baz', id: number): void;}`
  1042. const { content } = compile(`
  1043. <script setup lang="ts">
  1044. const emit = defineEmits<${type}>()
  1045. </script>
  1046. `)
  1047. assertCode(content)
  1048. expect(content).toMatch(`emit: (${type}),`)
  1049. expect(content).toMatch(`emits: ["foo", "bar", "baz"]`)
  1050. })
  1051. test('defineEmits w/ type (interface)', () => {
  1052. const { content } = compile(`
  1053. <script setup lang="ts">
  1054. interface Emits { (e: 'foo' | 'bar'): void }
  1055. const emit = defineEmits<Emits>()
  1056. </script>
  1057. `)
  1058. assertCode(content)
  1059. expect(content).toMatch(`emit: ({ (e: 'foo' | 'bar'): void }),`)
  1060. expect(content).toMatch(`emits: ["foo", "bar"]`)
  1061. })
  1062. test('defineEmits w/ type (exported interface)', () => {
  1063. const { content } = compile(`
  1064. <script setup lang="ts">
  1065. export interface Emits { (e: 'foo' | 'bar'): void }
  1066. const emit = defineEmits<Emits>()
  1067. </script>
  1068. `)
  1069. assertCode(content)
  1070. expect(content).toMatch(`emit: ({ (e: 'foo' | 'bar'): void }),`)
  1071. expect(content).toMatch(`emits: ["foo", "bar"]`)
  1072. })
  1073. test('defineEmits w/ type (type alias)', () => {
  1074. const { content } = compile(`
  1075. <script setup lang="ts">
  1076. type Emits = { (e: 'foo' | 'bar'): void }
  1077. const emit = defineEmits<Emits>()
  1078. </script>
  1079. `)
  1080. assertCode(content)
  1081. expect(content).toMatch(`emit: ({ (e: 'foo' | 'bar'): void }),`)
  1082. expect(content).toMatch(`emits: ["foo", "bar"]`)
  1083. })
  1084. test('defineEmits w/ type (exported type alias)', () => {
  1085. const { content } = compile(`
  1086. <script setup lang="ts">
  1087. export type Emits = { (e: 'foo' | 'bar'): void }
  1088. const emit = defineEmits<Emits>()
  1089. </script>
  1090. `)
  1091. assertCode(content)
  1092. expect(content).toMatch(`emit: ({ (e: 'foo' | 'bar'): void }),`)
  1093. expect(content).toMatch(`emits: ["foo", "bar"]`)
  1094. })
  1095. test('defineEmits w/ type (referenced function type)', () => {
  1096. const { content } = compile(`
  1097. <script setup lang="ts">
  1098. type Emits = (e: 'foo' | 'bar') => void
  1099. const emit = defineEmits<Emits>()
  1100. </script>
  1101. `)
  1102. assertCode(content)
  1103. expect(content).toMatch(`emit: ((e: 'foo' | 'bar') => void),`)
  1104. expect(content).toMatch(`emits: ["foo", "bar"]`)
  1105. })
  1106. test('defineEmits w/ type (referenced exported function type)', () => {
  1107. const { content } = compile(`
  1108. <script setup lang="ts">
  1109. export type Emits = (e: 'foo' | 'bar') => void
  1110. const emit = defineEmits<Emits>()
  1111. </script>
  1112. `)
  1113. assertCode(content)
  1114. expect(content).toMatch(`emit: ((e: 'foo' | 'bar') => void),`)
  1115. expect(content).toMatch(`emits: ["foo", "bar"]`)
  1116. })
  1117. // #5393
  1118. test('defineEmits w/ type (interface ts type)', () => {
  1119. const { content } = compile(`
  1120. <script setup lang="ts">
  1121. interface Emits { (e: 'foo'): void }
  1122. const emit: Emits = defineEmits(['foo'])
  1123. </script>
  1124. `)
  1125. assertCode(content)
  1126. expect(content).toMatch(`setup(__props, { expose, emit }) {`)
  1127. expect(content).toMatch(`emits: ['foo']`)
  1128. })
  1129. test('runtime Enum', () => {
  1130. const { content, bindings } = compile(
  1131. `<script setup lang="ts">
  1132. enum Foo { A = 123 }
  1133. </script>`
  1134. )
  1135. assertCode(content)
  1136. expect(bindings).toStrictEqual({
  1137. Foo: BindingTypes.SETUP_CONST
  1138. })
  1139. })
  1140. test('runtime Enum in normal script', () => {
  1141. const { content, bindings } = compile(
  1142. `<script lang="ts">
  1143. export enum D { D = "D" }
  1144. const enum C { C = "C" }
  1145. enum B { B = "B" }
  1146. </script>
  1147. <script setup lang="ts">
  1148. enum Foo { A = 123 }
  1149. </script>`
  1150. )
  1151. assertCode(content)
  1152. expect(bindings).toStrictEqual({
  1153. D: BindingTypes.SETUP_CONST,
  1154. C: BindingTypes.SETUP_CONST,
  1155. B: BindingTypes.SETUP_CONST,
  1156. Foo: BindingTypes.SETUP_CONST
  1157. })
  1158. })
  1159. test('const Enum', () => {
  1160. const { content, bindings } = compile(
  1161. `<script setup lang="ts">
  1162. const enum Foo { A = 123 }
  1163. </script>`
  1164. )
  1165. assertCode(content)
  1166. expect(bindings).toStrictEqual({
  1167. Foo: BindingTypes.SETUP_CONST
  1168. })
  1169. })
  1170. test('import type', () => {
  1171. const { content } = compile(
  1172. `<script setup lang="ts">
  1173. import type { Foo } from './main.ts'
  1174. import { type Bar, Baz } from './main.ts'
  1175. </script>`
  1176. )
  1177. expect(content).toMatch(`return { Baz }`)
  1178. assertCode(content)
  1179. })
  1180. })
  1181. describe('async/await detection', () => {
  1182. function assertAwaitDetection(code: string, shouldAsync = true) {
  1183. const { content } = compile(`<script setup>${code}</script>`, {
  1184. reactivityTransform: true
  1185. })
  1186. if (shouldAsync) {
  1187. expect(content).toMatch(`let __temp, __restore`)
  1188. }
  1189. expect(content).toMatch(`${shouldAsync ? `async ` : ``}setup(`)
  1190. assertCode(content)
  1191. return content
  1192. }
  1193. test('expression statement', () => {
  1194. assertAwaitDetection(`await foo`)
  1195. })
  1196. test('variable', () => {
  1197. assertAwaitDetection(`const a = 1 + (await foo)`)
  1198. })
  1199. test('ref', () => {
  1200. assertAwaitDetection(`let a = $ref(1 + (await foo))`)
  1201. })
  1202. // #4448
  1203. test('nested await', () => {
  1204. assertAwaitDetection(`await (await foo)`)
  1205. assertAwaitDetection(`await ((await foo))`)
  1206. assertAwaitDetection(`await (await (await foo))`)
  1207. })
  1208. // should prepend semicolon
  1209. test('nested leading await in expression statement', () => {
  1210. const code = assertAwaitDetection(`foo()\nawait 1 + await 2`)
  1211. expect(code).toMatch(`foo()\n;(`)
  1212. })
  1213. // #4596 should NOT prepend semicolon
  1214. test('single line conditions', () => {
  1215. const code = assertAwaitDetection(`if (false) await foo()`)
  1216. expect(code).not.toMatch(`if (false) ;(`)
  1217. })
  1218. test('nested statements', () => {
  1219. assertAwaitDetection(`if (ok) { await foo } else { await bar }`)
  1220. })
  1221. test('multiple `if` nested statements', () => {
  1222. assertAwaitDetection(`if (ok) {
  1223. let a = 'foo'
  1224. await 0 + await 1
  1225. await 2
  1226. } else if (a) {
  1227. await 10
  1228. if (b) {
  1229. await 0 + await 1
  1230. } else {
  1231. let a = 'foo'
  1232. await 2
  1233. }
  1234. if (b) {
  1235. await 3
  1236. await 4
  1237. }
  1238. } else {
  1239. await 5
  1240. }`)
  1241. })
  1242. test('multiple `if while` nested statements', () => {
  1243. assertAwaitDetection(`if (ok) {
  1244. while (d) {
  1245. await 5
  1246. }
  1247. while (d) {
  1248. await 5
  1249. await 6
  1250. if (c) {
  1251. let f = 10
  1252. 10 + await 7
  1253. } else {
  1254. await 8
  1255. await 9
  1256. }
  1257. }
  1258. }`)
  1259. })
  1260. test('multiple `if for` nested statements', () => {
  1261. assertAwaitDetection(`if (ok) {
  1262. for (let a of [1,2,3]) {
  1263. await a
  1264. }
  1265. for (let a of [1,2,3]) {
  1266. await a
  1267. await a
  1268. }
  1269. }`)
  1270. })
  1271. test('should ignore await inside functions', () => {
  1272. // function declaration
  1273. assertAwaitDetection(`async function foo() { await bar }`, false)
  1274. // function expression
  1275. assertAwaitDetection(`const foo = async () => { await bar }`, false)
  1276. // object method
  1277. assertAwaitDetection(`const obj = { async method() { await bar }}`, false)
  1278. // class method
  1279. assertAwaitDetection(
  1280. `const cls = class Foo { async method() { await bar }}`,
  1281. false
  1282. )
  1283. })
  1284. })
  1285. describe('errors', () => {
  1286. test('<script> and <script setup> must have same lang', () => {
  1287. expect(() =>
  1288. compile(`<script>foo()</script><script setup lang="ts">bar()</script>`)
  1289. ).toThrow(`<script> and <script setup> must have the same language type`)
  1290. })
  1291. const moduleErrorMsg = `cannot contain ES module exports`
  1292. test('non-type named exports', () => {
  1293. expect(() =>
  1294. compile(`<script setup>
  1295. export const a = 1
  1296. </script>`)
  1297. ).toThrow(moduleErrorMsg)
  1298. expect(() =>
  1299. compile(`<script setup>
  1300. export * from './foo'
  1301. </script>`)
  1302. ).toThrow(moduleErrorMsg)
  1303. expect(() =>
  1304. compile(`<script setup>
  1305. const bar = 1
  1306. export { bar as default }
  1307. </script>`)
  1308. ).toThrow(moduleErrorMsg)
  1309. })
  1310. test('defineProps/Emit() w/ both type and non-type args', () => {
  1311. expect(() => {
  1312. compile(`<script setup lang="ts">
  1313. defineProps<{}>({})
  1314. </script>`)
  1315. }).toThrow(`cannot accept both type and non-type arguments`)
  1316. expect(() => {
  1317. compile(`<script setup lang="ts">
  1318. defineEmits<{}>({})
  1319. </script>`)
  1320. }).toThrow(`cannot accept both type and non-type arguments`)
  1321. })
  1322. test('defineProps/Emit() referencing local var', () => {
  1323. expect(() =>
  1324. compile(`<script setup>
  1325. const bar = 1
  1326. defineProps({
  1327. foo: {
  1328. default: () => bar
  1329. }
  1330. })
  1331. </script>`)
  1332. ).toThrow(`cannot reference locally declared variables`)
  1333. expect(() =>
  1334. compile(`<script setup>
  1335. const bar = 'hello'
  1336. defineEmits([bar])
  1337. </script>`)
  1338. ).toThrow(`cannot reference locally declared variables`)
  1339. // #4644
  1340. expect(() =>
  1341. compile(`
  1342. <script>const bar = 1</script>
  1343. <script setup>
  1344. defineProps({
  1345. foo: {
  1346. default: () => bar
  1347. }
  1348. })
  1349. </script>`)
  1350. ).not.toThrow(`cannot reference locally declared variables`)
  1351. })
  1352. test('should allow defineProps/Emit() referencing scope var', () => {
  1353. assertCode(
  1354. compile(`<script setup>
  1355. const bar = 1
  1356. defineProps({
  1357. foo: {
  1358. default: bar => bar + 1
  1359. }
  1360. })
  1361. defineEmits({
  1362. foo: bar => bar > 1
  1363. })
  1364. </script>`).content
  1365. )
  1366. })
  1367. test('should allow defineProps/Emit() referencing imported binding', () => {
  1368. assertCode(
  1369. compile(`<script setup>
  1370. import { bar } from './bar'
  1371. defineProps({
  1372. foo: {
  1373. default: () => bar
  1374. }
  1375. })
  1376. defineEmits({
  1377. foo: () => bar > 1
  1378. })
  1379. </script>`).content
  1380. )
  1381. })
  1382. })
  1383. })
  1384. describe('SFC analyze <script> bindings', () => {
  1385. it('can parse decorators syntax in typescript block', () => {
  1386. const { scriptAst } = compile(`
  1387. <script lang="ts">
  1388. import { Options, Vue } from 'vue-class-component';
  1389. @Options({
  1390. components: {
  1391. HelloWorld,
  1392. },
  1393. props: ['foo', 'bar']
  1394. })
  1395. export default class Home extends Vue {}
  1396. </script>
  1397. `)
  1398. expect(scriptAst).toBeDefined()
  1399. })
  1400. it('recognizes props array declaration', () => {
  1401. const { bindings } = compile(`
  1402. <script>
  1403. export default {
  1404. props: ['foo', 'bar']
  1405. }
  1406. </script>
  1407. `)
  1408. expect(bindings).toStrictEqual({
  1409. foo: BindingTypes.PROPS,
  1410. bar: BindingTypes.PROPS
  1411. })
  1412. expect(bindings!.__isScriptSetup).toBe(false)
  1413. })
  1414. it('recognizes props object declaration', () => {
  1415. const { bindings } = compile(`
  1416. <script>
  1417. export default {
  1418. props: {
  1419. foo: String,
  1420. bar: {
  1421. type: String,
  1422. },
  1423. baz: null,
  1424. qux: [String, Number]
  1425. }
  1426. }
  1427. </script>
  1428. `)
  1429. expect(bindings).toStrictEqual({
  1430. foo: BindingTypes.PROPS,
  1431. bar: BindingTypes.PROPS,
  1432. baz: BindingTypes.PROPS,
  1433. qux: BindingTypes.PROPS
  1434. })
  1435. expect(bindings!.__isScriptSetup).toBe(false)
  1436. })
  1437. it('recognizes setup return', () => {
  1438. const { bindings } = compile(`
  1439. <script>
  1440. const bar = 2
  1441. export default {
  1442. setup() {
  1443. return {
  1444. foo: 1,
  1445. bar
  1446. }
  1447. }
  1448. }
  1449. </script>
  1450. `)
  1451. expect(bindings).toStrictEqual({
  1452. foo: BindingTypes.SETUP_MAYBE_REF,
  1453. bar: BindingTypes.SETUP_MAYBE_REF
  1454. })
  1455. expect(bindings!.__isScriptSetup).toBe(false)
  1456. })
  1457. it('recognizes exported vars', () => {
  1458. const { bindings } = compile(`
  1459. <script>
  1460. export const foo = 2
  1461. </script>
  1462. <script setup>
  1463. console.log(foo)
  1464. </script>
  1465. `)
  1466. expect(bindings).toStrictEqual({
  1467. foo: BindingTypes.SETUP_CONST
  1468. })
  1469. })
  1470. it('recognizes async setup return', () => {
  1471. const { bindings } = compile(`
  1472. <script>
  1473. const bar = 2
  1474. export default {
  1475. async setup() {
  1476. return {
  1477. foo: 1,
  1478. bar
  1479. }
  1480. }
  1481. }
  1482. </script>
  1483. `)
  1484. expect(bindings).toStrictEqual({
  1485. foo: BindingTypes.SETUP_MAYBE_REF,
  1486. bar: BindingTypes.SETUP_MAYBE_REF
  1487. })
  1488. expect(bindings!.__isScriptSetup).toBe(false)
  1489. })
  1490. it('recognizes data return', () => {
  1491. const { bindings } = compile(`
  1492. <script>
  1493. const bar = 2
  1494. export default {
  1495. data() {
  1496. return {
  1497. foo: null,
  1498. bar
  1499. }
  1500. }
  1501. }
  1502. </script>
  1503. `)
  1504. expect(bindings).toStrictEqual({
  1505. foo: BindingTypes.DATA,
  1506. bar: BindingTypes.DATA
  1507. })
  1508. })
  1509. it('recognizes methods', () => {
  1510. const { bindings } = compile(`
  1511. <script>
  1512. export default {
  1513. methods: {
  1514. foo() {}
  1515. }
  1516. }
  1517. </script>
  1518. `)
  1519. expect(bindings).toStrictEqual({ foo: BindingTypes.OPTIONS })
  1520. })
  1521. it('recognizes computeds', () => {
  1522. const { bindings } = compile(`
  1523. <script>
  1524. export default {
  1525. computed: {
  1526. foo() {},
  1527. bar: {
  1528. get() {},
  1529. set() {},
  1530. }
  1531. }
  1532. }
  1533. </script>
  1534. `)
  1535. expect(bindings).toStrictEqual({
  1536. foo: BindingTypes.OPTIONS,
  1537. bar: BindingTypes.OPTIONS
  1538. })
  1539. })
  1540. it('recognizes injections array declaration', () => {
  1541. const { bindings } = compile(`
  1542. <script>
  1543. export default {
  1544. inject: ['foo', 'bar']
  1545. }
  1546. </script>
  1547. `)
  1548. expect(bindings).toStrictEqual({
  1549. foo: BindingTypes.OPTIONS,
  1550. bar: BindingTypes.OPTIONS
  1551. })
  1552. })
  1553. it('recognizes injections object declaration', () => {
  1554. const { bindings } = compile(`
  1555. <script>
  1556. export default {
  1557. inject: {
  1558. foo: {},
  1559. bar: {},
  1560. }
  1561. }
  1562. </script>
  1563. `)
  1564. expect(bindings).toStrictEqual({
  1565. foo: BindingTypes.OPTIONS,
  1566. bar: BindingTypes.OPTIONS
  1567. })
  1568. })
  1569. it('works for mixed bindings', () => {
  1570. const { bindings } = compile(`
  1571. <script>
  1572. export default {
  1573. inject: ['foo'],
  1574. props: {
  1575. bar: String,
  1576. },
  1577. setup() {
  1578. return {
  1579. baz: null,
  1580. }
  1581. },
  1582. data() {
  1583. return {
  1584. qux: null
  1585. }
  1586. },
  1587. methods: {
  1588. quux() {}
  1589. },
  1590. computed: {
  1591. quuz() {}
  1592. }
  1593. }
  1594. </script>
  1595. `)
  1596. expect(bindings).toStrictEqual({
  1597. foo: BindingTypes.OPTIONS,
  1598. bar: BindingTypes.PROPS,
  1599. baz: BindingTypes.SETUP_MAYBE_REF,
  1600. qux: BindingTypes.DATA,
  1601. quux: BindingTypes.OPTIONS,
  1602. quuz: BindingTypes.OPTIONS
  1603. })
  1604. })
  1605. it('works for script setup', () => {
  1606. const { bindings } = compile(`
  1607. <script setup>
  1608. import { ref as r } from 'vue'
  1609. defineProps({
  1610. foo: String
  1611. })
  1612. const a = r(1)
  1613. let b = 2
  1614. const c = 3
  1615. const { d } = someFoo()
  1616. let { e } = someBar()
  1617. </script>
  1618. `)
  1619. expect(bindings).toStrictEqual({
  1620. r: BindingTypes.SETUP_CONST,
  1621. a: BindingTypes.SETUP_REF,
  1622. b: BindingTypes.SETUP_LET,
  1623. c: BindingTypes.SETUP_CONST,
  1624. d: BindingTypes.SETUP_MAYBE_REF,
  1625. e: BindingTypes.SETUP_LET,
  1626. foo: BindingTypes.PROPS
  1627. })
  1628. })
  1629. describe('auto name inference', () => {
  1630. test('basic', () => {
  1631. const { content } = compile(
  1632. `<script setup>const a = 1</script>
  1633. <template>{{ a }}</template>`,
  1634. undefined,
  1635. {
  1636. filename: 'FooBar.vue'
  1637. }
  1638. )
  1639. expect(content).toMatch(`export default {
  1640. __name: 'FooBar'`)
  1641. assertCode(content)
  1642. })
  1643. test('do not overwrite manual name (object)', () => {
  1644. const { content } = compile(
  1645. `<script>
  1646. export default {
  1647. name: 'Baz'
  1648. }
  1649. </script>
  1650. <script setup>const a = 1</script>
  1651. <template>{{ a }}</template>`,
  1652. undefined,
  1653. {
  1654. filename: 'FooBar.vue'
  1655. }
  1656. )
  1657. expect(content).not.toMatch(`name: 'FooBar'`)
  1658. expect(content).toMatch(`name: 'Baz'`)
  1659. assertCode(content)
  1660. })
  1661. test('do not overwrite manual name (call)', () => {
  1662. const { content } = compile(
  1663. `<script>
  1664. import { defineComponent } from 'vue'
  1665. export default defineComponent({
  1666. name: 'Baz'
  1667. })
  1668. </script>
  1669. <script setup>const a = 1</script>
  1670. <template>{{ a }}</template>`,
  1671. undefined,
  1672. {
  1673. filename: 'FooBar.vue'
  1674. }
  1675. )
  1676. expect(content).not.toMatch(`name: 'FooBar'`)
  1677. expect(content).toMatch(`name: 'Baz'`)
  1678. assertCode(content)
  1679. })
  1680. })
  1681. })