compileScript.spec.ts 41 KB

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