compileScript.spec.ts 40 KB

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