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. describe('<script> and <script setup> co-usage', () => {
  67. test('script first', () => {
  68. const { content } = compile(`
  69. <script>
  70. export const n = 1
  71. export default {}
  72. </script>
  73. <script setup>
  74. import { x } from './x'
  75. x()
  76. </script>
  77. `)
  78. assertCode(content)
  79. })
  80. test('script setup first', () => {
  81. const { content } = compile(`
  82. <script setup>
  83. import { x } from './x'
  84. x()
  85. </script>
  86. <script>
  87. export const n = 1
  88. export default {}
  89. </script>
  90. `)
  91. assertCode(content)
  92. })
  93. // #7805
  94. test('keep original semi style', () => {
  95. const { content } = compile(`
  96. <script setup>
  97. console.log('test')
  98. const props = defineProps(['item']);
  99. const emit = defineEmits(['change']);
  100. (function () {})()
  101. </script>
  102. `)
  103. assertCode(content)
  104. expect(content).toMatch(`console.log('test')`)
  105. expect(content).toMatch(`const props = __props;`)
  106. expect(content).toMatch(`const emit = __emit;`)
  107. expect(content).toMatch(`(function () {})()`)
  108. })
  109. test('script setup first, named default export', () => {
  110. const { content } = compile(`
  111. <script setup>
  112. import { x } from './x'
  113. x()
  114. </script>
  115. <script>
  116. export const n = 1
  117. const def = {}
  118. export { def as default }
  119. </script>
  120. `)
  121. assertCode(content)
  122. })
  123. // #4395
  124. test('script setup first, lang="ts", script block content export default', () => {
  125. const { content } = compile(`
  126. <script setup lang="ts">
  127. import { x } from './x'
  128. x()
  129. </script>
  130. <script lang="ts">
  131. export default {
  132. name: "test"
  133. }
  134. </script>
  135. `)
  136. // ensure __default__ is declared before used
  137. expect(content).toMatch(/const __default__[\S\s]*\.\.\.__default__/m)
  138. assertCode(content)
  139. })
  140. describe('spaces in ExportDefaultDeclaration node', () => {
  141. // #4371
  142. test('with many spaces and newline', () => {
  143. // #4371
  144. const { content } = compile(`
  145. <script>
  146. export const n = 1
  147. export default
  148. {
  149. some:'option'
  150. }
  151. </script>
  152. <script setup>
  153. import { x } from './x'
  154. x()
  155. </script>
  156. `)
  157. assertCode(content)
  158. })
  159. test('with minimal spaces', () => {
  160. const { content } = compile(`
  161. <script>
  162. export const n = 1
  163. export default{
  164. some:'option'
  165. }
  166. </script>
  167. <script setup>
  168. import { x } from './x'
  169. x()
  170. </script>
  171. `)
  172. assertCode(content)
  173. })
  174. })
  175. test('export call expression as default', () => {
  176. const { content } = compile(`
  177. <script>
  178. function fn() {
  179. return "hello, world";
  180. }
  181. export default fn();
  182. </script>
  183. <script setup>
  184. console.log('foo')
  185. </script>
  186. `)
  187. assertCode(content)
  188. })
  189. })
  190. describe('imports', () => {
  191. test('should hoist and expose imports', () => {
  192. assertCode(
  193. compile(`<script setup>
  194. import { ref } from 'vue'
  195. import 'foo/css'
  196. </script>`).content
  197. )
  198. })
  199. test('should extract comment for import or type declarations', () => {
  200. assertCode(
  201. compile(`
  202. <script setup>
  203. import a from 'a' // comment
  204. import b from 'b'
  205. </script>
  206. `).content
  207. )
  208. })
  209. // #2740
  210. test('should allow defineProps/Emit at the start of imports', () => {
  211. assertCode(
  212. compile(`<script setup>
  213. import { ref } from 'vue'
  214. defineProps(['foo'])
  215. defineEmits(['bar'])
  216. const r = ref(0)
  217. </script>`).content
  218. )
  219. })
  220. test('dedupe between user & helper', () => {
  221. const { content } = compile(
  222. `
  223. <script setup>
  224. import { ref } from 'vue'
  225. let foo = $ref(1)
  226. </script>
  227. `,
  228. { reactivityTransform: true }
  229. )
  230. assertCode(content)
  231. expect(content).toMatch(`import { ref } from 'vue'`)
  232. })
  233. test('import dedupe between <script> and <script setup>', () => {
  234. const { content } = compile(`
  235. <script>
  236. import { x } from './x'
  237. </script>
  238. <script setup>
  239. import { x } from './x'
  240. x()
  241. </script>
  242. `)
  243. assertCode(content)
  244. expect(content.indexOf(`import { x }`)).toEqual(
  245. content.lastIndexOf(`import { x }`)
  246. )
  247. })
  248. describe('import ref/reactive function from other place', () => {
  249. test('import directly', () => {
  250. const { bindings } = compile(`
  251. <script setup>
  252. import { ref, reactive } from './foo'
  253. const foo = ref(1)
  254. const bar = reactive(1)
  255. </script>
  256. `)
  257. expect(bindings).toStrictEqual({
  258. ref: BindingTypes.SETUP_MAYBE_REF,
  259. reactive: BindingTypes.SETUP_MAYBE_REF,
  260. foo: BindingTypes.SETUP_MAYBE_REF,
  261. bar: BindingTypes.SETUP_MAYBE_REF
  262. })
  263. })
  264. test('import w/ alias', () => {
  265. const { bindings } = compile(`
  266. <script setup>
  267. import { ref as _ref, reactive as _reactive } from './foo'
  268. const foo = ref(1)
  269. const bar = reactive(1)
  270. </script>
  271. `)
  272. expect(bindings).toStrictEqual({
  273. _reactive: BindingTypes.SETUP_MAYBE_REF,
  274. _ref: BindingTypes.SETUP_MAYBE_REF,
  275. foo: BindingTypes.SETUP_MAYBE_REF,
  276. bar: BindingTypes.SETUP_MAYBE_REF
  277. })
  278. })
  279. test('aliased usage before import site', () => {
  280. const { bindings } = compile(`
  281. <script setup>
  282. const bar = x(1)
  283. import { reactive as x } from 'vue'
  284. </script>
  285. `)
  286. expect(bindings).toStrictEqual({
  287. bar: BindingTypes.SETUP_REACTIVE_CONST,
  288. x: BindingTypes.SETUP_CONST
  289. })
  290. })
  291. })
  292. test('should support module string names syntax', () => {
  293. const { content, bindings } = compile(`
  294. <script>
  295. import { "😏" as foo } from './foo'
  296. </script>
  297. <script setup>
  298. import { "😏" as foo } from './foo'
  299. </script>
  300. `)
  301. assertCode(content)
  302. expect(bindings).toStrictEqual({
  303. foo: BindingTypes.SETUP_MAYBE_REF
  304. })
  305. })
  306. })
  307. // in dev mode, declared bindings are returned as an object from setup()
  308. // when using TS, users may import types which should not be returned as
  309. // values, so we need to check import usage in the template to determine
  310. // what to be returned.
  311. describe('dev mode import usage check', () => {
  312. test('components', () => {
  313. const { content } = compile(`
  314. <script setup lang="ts">
  315. import { FooBar, FooBaz, FooQux, foo } from './x'
  316. const fooBar: FooBar = 1
  317. </script>
  318. <template>
  319. <FooBaz></FooBaz>
  320. <foo-qux/>
  321. <foo/>
  322. FooBar
  323. </template>
  324. `)
  325. // FooBar: should not be matched by plain text or incorrect case
  326. // FooBaz: used as PascalCase component
  327. // FooQux: used as kebab-case component
  328. // foo: lowercase component
  329. expect(content).toMatch(
  330. `return { fooBar, get FooBaz() { return FooBaz }, ` +
  331. `get FooQux() { return FooQux }, get foo() { return foo } }`
  332. )
  333. assertCode(content)
  334. })
  335. test('directive', () => {
  336. const { content } = compile(`
  337. <script setup lang="ts">
  338. import { vMyDir } from './x'
  339. </script>
  340. <template>
  341. <div v-my-dir></div>
  342. </template>
  343. `)
  344. expect(content).toMatch(`return { get vMyDir() { return vMyDir } }`)
  345. assertCode(content)
  346. })
  347. test('dynamic arguments', () => {
  348. const { content } = compile(`
  349. <script setup lang="ts">
  350. import { FooBar, foo, bar, unused, baz } from './x'
  351. </script>
  352. <template>
  353. <FooBar #[foo.slotName] />
  354. <FooBar #unused />
  355. <div :[bar.attrName]="15"></div>
  356. <div unused="unused"></div>
  357. <div #[\`item:\${baz.key}\`]="{ value }"></div>
  358. </template>
  359. `)
  360. expect(content).toMatch(
  361. `return { get FooBar() { return FooBar }, get foo() { return foo }, ` +
  362. `get bar() { return bar }, get baz() { return baz } }`
  363. )
  364. assertCode(content)
  365. })
  366. // https://github.com/vuejs/core/issues/4599
  367. test('attribute expressions', () => {
  368. const { content } = compile(`
  369. <script setup lang="ts">
  370. import { bar, baz } from './x'
  371. const cond = true
  372. </script>
  373. <template>
  374. <div :class="[cond ? '' : bar(), 'default']" :style="baz"></div>
  375. </template>
  376. `)
  377. expect(content).toMatch(
  378. `return { cond, get bar() { return bar }, get baz() { return baz } }`
  379. )
  380. assertCode(content)
  381. })
  382. test('vue interpolations', () => {
  383. const { content } = compile(`
  384. <script setup lang="ts">
  385. import { x, y, z, x$y } from './x'
  386. </script>
  387. <template>
  388. <div :id="z + 'y'">{{ x }} {{ yy }} {{ x$y }}</div>
  389. </template>
  390. `)
  391. // x: used in interpolation
  392. // y: should not be matched by {{ yy }} or 'y' in binding exps
  393. // x$y: #4274 should escape special chars when creating Regex
  394. expect(content).toMatch(
  395. `return { get x() { return x }, get z() { return z }, get x$y() { return x$y } }`
  396. )
  397. assertCode(content)
  398. })
  399. // #4340 interpolations in template strings
  400. test('js template string interpolations', () => {
  401. const { content } = compile(`
  402. <script setup lang="ts">
  403. import { VAR, VAR2, VAR3 } from './x'
  404. </script>
  405. <template>
  406. {{ \`\${VAR}VAR2\${VAR3}\` }}
  407. </template>
  408. `)
  409. // VAR2 should not be matched
  410. expect(content).toMatch(
  411. `return { get VAR() { return VAR }, get VAR3() { return VAR3 } }`
  412. )
  413. assertCode(content)
  414. })
  415. // edge case: last tag in template
  416. test('last tag', () => {
  417. const { content } = compile(`
  418. <script setup lang="ts">
  419. import { FooBaz, Last } from './x'
  420. </script>
  421. <template>
  422. <FooBaz></FooBaz>
  423. <Last/>
  424. </template>
  425. `)
  426. expect(content).toMatch(
  427. `return { get FooBaz() { return FooBaz }, get Last() { return Last } }`
  428. )
  429. assertCode(content)
  430. })
  431. test('TS annotations', () => {
  432. const { content } = compile(`
  433. <script setup lang="ts">
  434. import { Foo, Bar, Baz, Qux, Fred } from './x'
  435. const a = 1
  436. function b() {}
  437. </script>
  438. <template>
  439. {{ a as Foo }}
  440. {{ b<Bar>() }}
  441. {{ Baz }}
  442. <Comp v-slot="{ data }: Qux">{{ data }}</Comp>
  443. <div v-for="{ z = x as Qux } in list as Fred"/>
  444. </template>
  445. `)
  446. expect(content).toMatch(`return { a, b, get Baz() { return Baz } }`)
  447. assertCode(content)
  448. })
  449. // vuejs/vue#12591
  450. test('v-on inline statement', () => {
  451. // should not error
  452. compile(`
  453. <script setup lang="ts">
  454. import { foo } from './foo'
  455. </script>
  456. <template>
  457. <div @click="$emit('update:a');"></div>
  458. </template>
  459. `)
  460. })
  461. test('template ref', () => {
  462. const { content } = compile(`
  463. <script setup lang="ts">
  464. import { foo, bar, Baz } from './foo'
  465. </script>
  466. <template>
  467. <div ref="foo"></div>
  468. <div ref=""></div>
  469. <Baz ref="bar" />
  470. </template>
  471. `)
  472. expect(content).toMatch(
  473. 'return { get foo() { return foo }, get bar() { return bar }, get Baz() { return Baz } }'
  474. )
  475. assertCode(content)
  476. })
  477. })
  478. describe('inlineTemplate mode', () => {
  479. test('should work', () => {
  480. const { content } = compile(
  481. `
  482. <script setup>
  483. import { ref } from 'vue'
  484. const count = ref(0)
  485. </script>
  486. <template>
  487. <div>{{ count }}</div>
  488. <div>static</div>
  489. </template>
  490. `,
  491. { inlineTemplate: true }
  492. )
  493. // check snapshot and make sure helper imports and
  494. // hoists are placed correctly.
  495. assertCode(content)
  496. // in inline mode, no need to call expose() since nothing is exposed
  497. // anyway!
  498. expect(content).not.toMatch(`expose()`)
  499. })
  500. test('with defineExpose()', () => {
  501. const { content } = compile(
  502. `
  503. <script setup>
  504. const count = ref(0)
  505. defineExpose({ count })
  506. </script>
  507. `,
  508. { inlineTemplate: true }
  509. )
  510. assertCode(content)
  511. expect(content).toMatch(`setup(__props, { expose: __expose })`)
  512. expect(content).toMatch(`expose({ count })`)
  513. })
  514. test('referencing scope components and directives', () => {
  515. const { content } = compile(
  516. `
  517. <script setup>
  518. import ChildComp from './Child.vue'
  519. import SomeOtherComp from './Other.vue'
  520. import vMyDir from './my-dir'
  521. </script>
  522. <template>
  523. <div v-my-dir></div>
  524. <ChildComp/>
  525. <some-other-comp/>
  526. </template>
  527. `,
  528. { inlineTemplate: true }
  529. )
  530. expect(content).toMatch('[_unref(vMyDir)]')
  531. expect(content).toMatch('_createVNode(ChildComp)')
  532. // kebab-case component support
  533. expect(content).toMatch('_createVNode(SomeOtherComp)')
  534. assertCode(content)
  535. })
  536. test('avoid unref() when necessary', () => {
  537. // function, const, component import
  538. const { content } = compile(
  539. `<script setup>
  540. import { ref } from 'vue'
  541. import Foo, { bar } from './Foo.vue'
  542. import other from './util'
  543. import * as tree from './tree'
  544. const count = ref(0)
  545. const constant = {}
  546. const maybe = foo()
  547. let lett = 1
  548. function fn() {}
  549. </script>
  550. <template>
  551. <Foo>{{ bar }}</Foo>
  552. <div @click="fn">{{ count }} {{ constant }} {{ maybe }} {{ lett }} {{ other }}</div>
  553. {{ tree.foo() }}
  554. </template>
  555. `,
  556. { inlineTemplate: true }
  557. )
  558. // no need to unref vue component import
  559. expect(content).toMatch(`createVNode(Foo,`)
  560. // #2699 should unref named imports from .vue
  561. expect(content).toMatch(`unref(bar)`)
  562. // should unref other imports
  563. expect(content).toMatch(`unref(other)`)
  564. // no need to unref constant literals
  565. expect(content).not.toMatch(`unref(constant)`)
  566. // should directly use .value for known refs
  567. expect(content).toMatch(`count.value`)
  568. // should unref() on const bindings that may be refs
  569. expect(content).toMatch(`unref(maybe)`)
  570. // should unref() on let bindings
  571. expect(content).toMatch(`unref(lett)`)
  572. // no need to unref namespace import (this also preserves tree-shaking)
  573. expect(content).toMatch(`tree.foo()`)
  574. // no need to unref function declarations
  575. expect(content).toMatch(`{ onClick: fn }`)
  576. // no need to mark constant fns in patch flag
  577. expect(content).not.toMatch(`PROPS`)
  578. assertCode(content)
  579. })
  580. test('v-model codegen', () => {
  581. const { content } = compile(
  582. `<script setup>
  583. import { ref } from 'vue'
  584. const count = ref(0)
  585. const maybe = foo()
  586. let lett = 1
  587. </script>
  588. <template>
  589. <input v-model="count">
  590. <input v-model="maybe">
  591. <input v-model="lett">
  592. </template>
  593. `,
  594. { inlineTemplate: true }
  595. )
  596. // known const ref: set value
  597. expect(content).toMatch(`(count).value = $event`)
  598. // const but maybe ref: assign if ref, otherwise do nothing
  599. expect(content).toMatch(`_isRef(maybe) ? (maybe).value = $event : null`)
  600. // let: handle both cases
  601. expect(content).toMatch(
  602. `_isRef(lett) ? (lett).value = $event : lett = $event`
  603. )
  604. assertCode(content)
  605. })
  606. test('v-model should not generate ref assignment code for non-setup bindings', () => {
  607. const { content } = compile(
  608. `<script setup>
  609. import { ref } from 'vue'
  610. const count = ref(0)
  611. </script>
  612. <script>
  613. export default {
  614. data() { return { foo: 123 } }
  615. }
  616. </script>
  617. <template>
  618. <input v-model="foo">
  619. </template>
  620. `,
  621. { inlineTemplate: true }
  622. )
  623. expect(content).not.toMatch(`_isRef(foo)`)
  624. })
  625. test('template assignment expression codegen', () => {
  626. const { content } = compile(
  627. `<script setup>
  628. import { ref } from 'vue'
  629. const count = ref(0)
  630. const maybe = foo()
  631. let lett = 1
  632. let v = ref(1)
  633. </script>
  634. <template>
  635. <div @click="count = 1"/>
  636. <div @click="maybe = count"/>
  637. <div @click="lett = count"/>
  638. <div @click="v += 1"/>
  639. <div @click="v -= 1"/>
  640. <div @click="() => {
  641. let a = '' + lett
  642. v = a
  643. }"/>
  644. <div @click="() => {
  645. // nested scopes
  646. (()=>{
  647. let x = a
  648. (()=>{
  649. let z = x
  650. let z2 = z
  651. })
  652. let lz = z
  653. })
  654. v = a
  655. }"/>
  656. </template>
  657. `,
  658. { inlineTemplate: true }
  659. )
  660. // known const ref: set value
  661. expect(content).toMatch(`count.value = 1`)
  662. // const but maybe ref: only assign after check
  663. expect(content).toMatch(`maybe.value = count.value`)
  664. // let: handle both cases
  665. expect(content).toMatch(
  666. `_isRef(lett) ? lett.value = count.value : lett = count.value`
  667. )
  668. expect(content).toMatch(`_isRef(v) ? v.value += 1 : v += 1`)
  669. expect(content).toMatch(`_isRef(v) ? v.value -= 1 : v -= 1`)
  670. expect(content).toMatch(`_isRef(v) ? v.value = a : v = a`)
  671. expect(content).toMatch(`_isRef(v) ? v.value = _ctx.a : v = _ctx.a`)
  672. assertCode(content)
  673. })
  674. test('template update expression codegen', () => {
  675. const { content } = compile(
  676. `<script setup>
  677. import { ref } from 'vue'
  678. const count = ref(0)
  679. const maybe = foo()
  680. let lett = 1
  681. </script>
  682. <template>
  683. <div @click="count++"/>
  684. <div @click="--count"/>
  685. <div @click="maybe++"/>
  686. <div @click="--maybe"/>
  687. <div @click="lett++"/>
  688. <div @click="--lett"/>
  689. </template>
  690. `,
  691. { inlineTemplate: true }
  692. )
  693. // known const ref: set value
  694. expect(content).toMatch(`count.value++`)
  695. expect(content).toMatch(`--count.value`)
  696. // const but maybe ref (non-ref case ignored)
  697. expect(content).toMatch(`maybe.value++`)
  698. expect(content).toMatch(`--maybe.value`)
  699. // let: handle both cases
  700. expect(content).toMatch(`_isRef(lett) ? lett.value++ : lett++`)
  701. expect(content).toMatch(`_isRef(lett) ? --lett.value : --lett`)
  702. assertCode(content)
  703. })
  704. test('template destructure assignment codegen', () => {
  705. const { content } = compile(
  706. `<script setup>
  707. import { ref } from 'vue'
  708. const val = {}
  709. const count = ref(0)
  710. const maybe = foo()
  711. let lett = 1
  712. </script>
  713. <template>
  714. <div @click="({ count } = val)"/>
  715. <div @click="[maybe] = val"/>
  716. <div @click="({ lett } = val)"/>
  717. </template>
  718. `,
  719. { inlineTemplate: true }
  720. )
  721. // known const ref: set value
  722. expect(content).toMatch(`({ count: count.value } = val)`)
  723. // const but maybe ref (non-ref case ignored)
  724. expect(content).toMatch(`[maybe.value] = val`)
  725. // let: assumes non-ref
  726. expect(content).toMatch(`{ lett: lett } = val`)
  727. assertCode(content)
  728. })
  729. test('ssr codegen', () => {
  730. const { content } = compile(
  731. `
  732. <script setup>
  733. import { ref } from 'vue'
  734. const count = ref(0)
  735. const style = { color: 'red' }
  736. </script>
  737. <template>
  738. <div>{{ count }}</div>
  739. <div>static</div>
  740. </template>
  741. <style>
  742. div { color: v-bind(count) }
  743. span { color: v-bind(style.color) }
  744. </style>
  745. `,
  746. {
  747. inlineTemplate: true,
  748. templateOptions: {
  749. ssr: true
  750. }
  751. }
  752. )
  753. expect(content).toMatch(`\n __ssrInlineRender: true,\n`)
  754. expect(content).toMatch(`return (_ctx, _push`)
  755. expect(content).toMatch(`ssrInterpolate`)
  756. expect(content).not.toMatch(`useCssVars`)
  757. expect(content).toMatch(`"--${mockId}-count": (count.value)`)
  758. expect(content).toMatch(`"--${mockId}-style\\\\.color": (style.color)`)
  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. })