compileScript.spec.ts 41 KB

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