compileScript.spec.ts 46 KB

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