compileScript.spec.ts 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466
  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 async setup return', () => {
  1214. const { bindings } = compile(`
  1215. <script>
  1216. const bar = 2
  1217. export default {
  1218. async setup() {
  1219. return {
  1220. foo: 1,
  1221. bar
  1222. }
  1223. }
  1224. }
  1225. </script>
  1226. `)
  1227. expect(bindings).toStrictEqual({
  1228. foo: BindingTypes.SETUP_MAYBE_REF,
  1229. bar: BindingTypes.SETUP_MAYBE_REF
  1230. })
  1231. expect(bindings!.__isScriptSetup).toBe(false)
  1232. })
  1233. it('recognizes data return', () => {
  1234. const { bindings } = compile(`
  1235. <script>
  1236. const bar = 2
  1237. export default {
  1238. data() {
  1239. return {
  1240. foo: null,
  1241. bar
  1242. }
  1243. }
  1244. }
  1245. </script>
  1246. `)
  1247. expect(bindings).toStrictEqual({
  1248. foo: BindingTypes.DATA,
  1249. bar: BindingTypes.DATA
  1250. })
  1251. })
  1252. it('recognizes methods', () => {
  1253. const { bindings } = compile(`
  1254. <script>
  1255. export default {
  1256. methods: {
  1257. foo() {}
  1258. }
  1259. }
  1260. </script>
  1261. `)
  1262. expect(bindings).toStrictEqual({ foo: BindingTypes.OPTIONS })
  1263. })
  1264. it('recognizes computeds', () => {
  1265. const { bindings } = compile(`
  1266. <script>
  1267. export default {
  1268. computed: {
  1269. foo() {},
  1270. bar: {
  1271. get() {},
  1272. set() {},
  1273. }
  1274. }
  1275. }
  1276. </script>
  1277. `)
  1278. expect(bindings).toStrictEqual({
  1279. foo: BindingTypes.OPTIONS,
  1280. bar: BindingTypes.OPTIONS
  1281. })
  1282. })
  1283. it('recognizes injections array declaration', () => {
  1284. const { bindings } = compile(`
  1285. <script>
  1286. export default {
  1287. inject: ['foo', 'bar']
  1288. }
  1289. </script>
  1290. `)
  1291. expect(bindings).toStrictEqual({
  1292. foo: BindingTypes.OPTIONS,
  1293. bar: BindingTypes.OPTIONS
  1294. })
  1295. })
  1296. it('recognizes injections object declaration', () => {
  1297. const { bindings } = compile(`
  1298. <script>
  1299. export default {
  1300. inject: {
  1301. foo: {},
  1302. bar: {},
  1303. }
  1304. }
  1305. </script>
  1306. `)
  1307. expect(bindings).toStrictEqual({
  1308. foo: BindingTypes.OPTIONS,
  1309. bar: BindingTypes.OPTIONS
  1310. })
  1311. })
  1312. it('works for mixed bindings', () => {
  1313. const { bindings } = compile(`
  1314. <script>
  1315. export default {
  1316. inject: ['foo'],
  1317. props: {
  1318. bar: String,
  1319. },
  1320. setup() {
  1321. return {
  1322. baz: null,
  1323. }
  1324. },
  1325. data() {
  1326. return {
  1327. qux: null
  1328. }
  1329. },
  1330. methods: {
  1331. quux() {}
  1332. },
  1333. computed: {
  1334. quuz() {}
  1335. }
  1336. }
  1337. </script>
  1338. `)
  1339. expect(bindings).toStrictEqual({
  1340. foo: BindingTypes.OPTIONS,
  1341. bar: BindingTypes.PROPS,
  1342. baz: BindingTypes.SETUP_MAYBE_REF,
  1343. qux: BindingTypes.DATA,
  1344. quux: BindingTypes.OPTIONS,
  1345. quuz: BindingTypes.OPTIONS
  1346. })
  1347. })
  1348. it('works for script setup', () => {
  1349. const { bindings } = compile(`
  1350. <script setup>
  1351. import { ref as r } from 'vue'
  1352. defineProps({
  1353. foo: String
  1354. })
  1355. const a = r(1)
  1356. let b = 2
  1357. const c = 3
  1358. const { d } = someFoo()
  1359. let { e } = someBar()
  1360. </script>
  1361. `)
  1362. expect(bindings).toStrictEqual({
  1363. r: BindingTypes.SETUP_CONST,
  1364. a: BindingTypes.SETUP_REF,
  1365. b: BindingTypes.SETUP_LET,
  1366. c: BindingTypes.SETUP_CONST,
  1367. d: BindingTypes.SETUP_MAYBE_REF,
  1368. e: BindingTypes.SETUP_LET,
  1369. foo: BindingTypes.PROPS
  1370. })
  1371. })
  1372. })