compileScript.spec.ts 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467
  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. union: string | number
  671. literalUnion: 'foo' | 'bar'
  672. literalUnionNumber: 1 | 2 | 3 | 4 | 5
  673. literalUnionMixed: 'foo' | 1 | boolean
  674. intersection: Test & {}
  675. foo: ((item: any) => boolean) | null
  676. }>()
  677. </script>`)
  678. assertCode(content)
  679. expect(content).toMatch(`string: { type: String, required: true }`)
  680. expect(content).toMatch(`number: { type: Number, required: true }`)
  681. expect(content).toMatch(`boolean: { type: Boolean, required: true }`)
  682. expect(content).toMatch(`object: { type: Object, required: true }`)
  683. expect(content).toMatch(`objectLiteral: { type: Object, required: true }`)
  684. expect(content).toMatch(`fn: { type: Function, required: true }`)
  685. expect(content).toMatch(`functionRef: { type: Function, required: true }`)
  686. expect(content).toMatch(`objectRef: { type: Object, required: true }`)
  687. expect(content).toMatch(`dateTime: { type: Date, required: true }`)
  688. expect(content).toMatch(`array: { type: Array, required: true }`)
  689. expect(content).toMatch(`arrayRef: { type: Array, required: true }`)
  690. expect(content).toMatch(`tuple: { type: Array, required: true }`)
  691. expect(content).toMatch(`set: { type: Set, required: true }`)
  692. expect(content).toMatch(`literal: { type: String, required: true }`)
  693. expect(content).toMatch(`optional: { type: null, required: false }`)
  694. expect(content).toMatch(`recordRef: { type: Object, required: true }`)
  695. expect(content).toMatch(`interface: { type: Object, required: true }`)
  696. expect(content).toMatch(`alias: { type: Array, required: true }`)
  697. expect(content).toMatch(`method: { type: Function, required: true }`)
  698. expect(content).toMatch(
  699. `union: { type: [String, Number], required: true }`
  700. )
  701. expect(content).toMatch(`literalUnion: { type: String, required: true }`)
  702. expect(content).toMatch(
  703. `literalUnionNumber: { type: Number, required: true }`
  704. )
  705. expect(content).toMatch(
  706. `literalUnionMixed: { type: [String, Number, Boolean], required: true }`
  707. )
  708. expect(content).toMatch(`intersection: { type: Object, required: true }`)
  709. expect(content).toMatch(`foo: { type: [Function, null], required: true }`)
  710. expect(bindings).toStrictEqual({
  711. string: BindingTypes.PROPS,
  712. number: BindingTypes.PROPS,
  713. boolean: BindingTypes.PROPS,
  714. object: BindingTypes.PROPS,
  715. objectLiteral: BindingTypes.PROPS,
  716. fn: BindingTypes.PROPS,
  717. functionRef: BindingTypes.PROPS,
  718. objectRef: BindingTypes.PROPS,
  719. dateTime: BindingTypes.PROPS,
  720. array: BindingTypes.PROPS,
  721. arrayRef: BindingTypes.PROPS,
  722. tuple: BindingTypes.PROPS,
  723. set: BindingTypes.PROPS,
  724. literal: BindingTypes.PROPS,
  725. optional: BindingTypes.PROPS,
  726. recordRef: BindingTypes.PROPS,
  727. interface: BindingTypes.PROPS,
  728. alias: BindingTypes.PROPS,
  729. method: BindingTypes.PROPS,
  730. union: BindingTypes.PROPS,
  731. literalUnion: BindingTypes.PROPS,
  732. literalUnionNumber: BindingTypes.PROPS,
  733. literalUnionMixed: BindingTypes.PROPS,
  734. intersection: BindingTypes.PROPS,
  735. foo: BindingTypes.PROPS
  736. })
  737. })
  738. test('defineProps w/ interface', () => {
  739. const { content, bindings } = compile(`
  740. <script setup lang="ts">
  741. interface Props { x?: number }
  742. defineProps<Props>()
  743. </script>
  744. `)
  745. assertCode(content)
  746. expect(content).toMatch(`x: { type: Number, required: false }`)
  747. expect(bindings).toStrictEqual({
  748. x: BindingTypes.PROPS
  749. })
  750. })
  751. test('defineProps w/ exported interface', () => {
  752. const { content, bindings } = compile(`
  753. <script setup lang="ts">
  754. export interface Props { x?: number }
  755. defineProps<Props>()
  756. </script>
  757. `)
  758. assertCode(content)
  759. expect(content).toMatch(`x: { type: Number, required: false }`)
  760. expect(bindings).toStrictEqual({
  761. x: BindingTypes.PROPS
  762. })
  763. })
  764. test('defineProps w/ exported interface in normal script', () => {
  765. const { content, bindings } = compile(`
  766. <script lang="ts">
  767. export interface Props { x?: number }
  768. </script>
  769. <script setup lang="ts">
  770. defineProps<Props>()
  771. </script>
  772. `)
  773. assertCode(content)
  774. expect(content).toMatch(`x: { type: Number, required: false }`)
  775. expect(bindings).toStrictEqual({
  776. x: BindingTypes.PROPS
  777. })
  778. })
  779. test('defineProps w/ type alias', () => {
  780. const { content, bindings } = compile(`
  781. <script setup lang="ts">
  782. type Props = { x?: number }
  783. defineProps<Props>()
  784. </script>
  785. `)
  786. assertCode(content)
  787. expect(content).toMatch(`x: { type: Number, required: false }`)
  788. expect(bindings).toStrictEqual({
  789. x: BindingTypes.PROPS
  790. })
  791. })
  792. test('defineProps w/ exported type alias', () => {
  793. const { content, bindings } = compile(`
  794. <script setup lang="ts">
  795. export type Props = { x?: number }
  796. defineProps<Props>()
  797. </script>
  798. `)
  799. assertCode(content)
  800. expect(content).toMatch(`x: { type: Number, required: false }`)
  801. expect(bindings).toStrictEqual({
  802. x: BindingTypes.PROPS
  803. })
  804. })
  805. test('withDefaults (static)', () => {
  806. const { content, bindings } = compile(`
  807. <script setup lang="ts">
  808. const props = withDefaults(defineProps<{
  809. foo?: string
  810. bar?: number;
  811. baz: boolean;
  812. qux?(): number
  813. }>(), {
  814. foo: 'hi',
  815. qux() { return 1 }
  816. })
  817. </script>
  818. `)
  819. assertCode(content)
  820. expect(content).toMatch(
  821. `foo: { type: String, required: false, default: 'hi' }`
  822. )
  823. expect(content).toMatch(`bar: { type: Number, required: false }`)
  824. expect(content).toMatch(`baz: { type: Boolean, required: true }`)
  825. expect(content).toMatch(
  826. `qux: { type: Function, required: false, default() { return 1 } }`
  827. )
  828. expect(content).toMatch(
  829. `{ foo: string, bar?: number, baz: boolean, qux(): number }`
  830. )
  831. expect(content).toMatch(`const props = __props`)
  832. expect(bindings).toStrictEqual({
  833. foo: BindingTypes.PROPS,
  834. bar: BindingTypes.PROPS,
  835. baz: BindingTypes.PROPS,
  836. qux: BindingTypes.PROPS,
  837. props: BindingTypes.SETUP_CONST
  838. })
  839. })
  840. test('withDefaults (dynamic)', () => {
  841. const { content } = compile(`
  842. <script setup lang="ts">
  843. import { defaults } from './foo'
  844. const props = withDefaults(defineProps<{
  845. foo?: string
  846. bar?: number
  847. baz: boolean
  848. }>(), { ...defaults })
  849. </script>
  850. `)
  851. assertCode(content)
  852. expect(content).toMatch(`import { mergeDefaults as _mergeDefaults`)
  853. expect(content).toMatch(
  854. `
  855. _mergeDefaults({
  856. foo: { type: String, required: false },
  857. bar: { type: Number, required: false },
  858. baz: { type: Boolean, required: true }
  859. }, { ...defaults })`.trim()
  860. )
  861. })
  862. test('defineEmits w/ type', () => {
  863. const { content } = compile(`
  864. <script setup lang="ts">
  865. const emit = defineEmits<(e: 'foo' | 'bar') => void>()
  866. </script>
  867. `)
  868. assertCode(content)
  869. expect(content).toMatch(`emit: ((e: 'foo' | 'bar') => void),`)
  870. expect(content).toMatch(`emits: ["foo", "bar"]`)
  871. })
  872. test('defineEmits w/ type (union)', () => {
  873. const type = `((e: 'foo' | 'bar') => void) | ((e: 'baz', id: number) => void)`
  874. expect(() =>
  875. compile(`
  876. <script setup lang="ts">
  877. const emit = defineEmits<${type}>()
  878. </script>
  879. `)
  880. ).toThrow()
  881. })
  882. test('defineEmits w/ type (type literal w/ call signatures)', () => {
  883. const type = `{(e: 'foo' | 'bar'): void; (e: 'baz', id: number): void;}`
  884. const { content } = compile(`
  885. <script setup lang="ts">
  886. const emit = defineEmits<${type}>()
  887. </script>
  888. `)
  889. assertCode(content)
  890. expect(content).toMatch(`emit: (${type}),`)
  891. expect(content).toMatch(`emits: ["foo", "bar", "baz"]`)
  892. })
  893. test('defineEmits w/ type (interface)', () => {
  894. const { content } = compile(`
  895. <script setup lang="ts">
  896. interface Emits { (e: 'foo' | 'bar'): void }
  897. const emit = defineEmits<Emits>()
  898. </script>
  899. `)
  900. assertCode(content)
  901. expect(content).toMatch(`emit: ({ (e: 'foo' | 'bar'): void }),`)
  902. expect(content).toMatch(`emits: ["foo", "bar"]`)
  903. })
  904. test('defineEmits w/ type (exported interface)', () => {
  905. const { content } = compile(`
  906. <script setup lang="ts">
  907. export interface Emits { (e: 'foo' | 'bar'): void }
  908. const emit = defineEmits<Emits>()
  909. </script>
  910. `)
  911. assertCode(content)
  912. expect(content).toMatch(`emit: ({ (e: 'foo' | 'bar'): void }),`)
  913. expect(content).toMatch(`emits: ["foo", "bar"]`)
  914. })
  915. test('defineEmits w/ type (type alias)', () => {
  916. const { content } = compile(`
  917. <script setup lang="ts">
  918. type Emits = { (e: 'foo' | 'bar'): void }
  919. const emit = defineEmits<Emits>()
  920. </script>
  921. `)
  922. assertCode(content)
  923. expect(content).toMatch(`emit: ({ (e: 'foo' | 'bar'): void }),`)
  924. expect(content).toMatch(`emits: ["foo", "bar"]`)
  925. })
  926. test('defineEmits w/ type (exported type alias)', () => {
  927. const { content } = compile(`
  928. <script setup lang="ts">
  929. export type Emits = { (e: 'foo' | 'bar'): void }
  930. const emit = defineEmits<Emits>()
  931. </script>
  932. `)
  933. assertCode(content)
  934. expect(content).toMatch(`emit: ({ (e: 'foo' | 'bar'): void }),`)
  935. expect(content).toMatch(`emits: ["foo", "bar"]`)
  936. })
  937. test('defineEmits w/ type (referenced function type)', () => {
  938. const { content } = compile(`
  939. <script setup lang="ts">
  940. type Emits = (e: 'foo' | 'bar') => void
  941. const emit = defineEmits<Emits>()
  942. </script>
  943. `)
  944. assertCode(content)
  945. expect(content).toMatch(`emit: ((e: 'foo' | 'bar') => void),`)
  946. expect(content).toMatch(`emits: ["foo", "bar"]`)
  947. })
  948. test('defineEmits w/ type (referenced exported function type)', () => {
  949. const { content } = compile(`
  950. <script setup lang="ts">
  951. export type Emits = (e: 'foo' | 'bar') => void
  952. const emit = defineEmits<Emits>()
  953. </script>
  954. `)
  955. assertCode(content)
  956. expect(content).toMatch(`emit: ((e: 'foo' | 'bar') => void),`)
  957. expect(content).toMatch(`emits: ["foo", "bar"]`)
  958. })
  959. test('runtime Enum', () => {
  960. const { content, bindings } = compile(
  961. `<script setup lang="ts">
  962. enum Foo { A = 123 }
  963. </script>`
  964. )
  965. assertCode(content)
  966. expect(bindings).toStrictEqual({
  967. Foo: BindingTypes.SETUP_CONST
  968. })
  969. })
  970. test('const Enum', () => {
  971. const { content, bindings } = compile(
  972. `<script setup lang="ts">
  973. const enum Foo { A = 123 }
  974. </script>`
  975. )
  976. assertCode(content)
  977. expect(bindings).toStrictEqual({
  978. Foo: BindingTypes.SETUP_CONST
  979. })
  980. })
  981. })
  982. describe('async/await detection', () => {
  983. function assertAwaitDetection(
  984. code: string,
  985. expected: string | ((content: string) => boolean),
  986. shouldAsync = true
  987. ) {
  988. const { content } = compile(`<script setup>${code}</script>`, {
  989. refSugar: true
  990. })
  991. if (shouldAsync) {
  992. expect(content).toMatch(`let __temp, __restore`)
  993. }
  994. expect(content).toMatch(`${shouldAsync ? `async ` : ``}setup(`)
  995. if (typeof expected === 'string') {
  996. expect(content).toMatch(expected)
  997. } else {
  998. expect(expected(content)).toBe(true)
  999. }
  1000. }
  1001. test('expression statement', () => {
  1002. assertAwaitDetection(
  1003. `await foo`,
  1004. `;(([__temp,__restore]=_withAsyncContext(()=>(foo))),__temp=await __temp,__restore())`
  1005. )
  1006. })
  1007. test('variable', () => {
  1008. assertAwaitDetection(
  1009. `const a = 1 + (await foo)`,
  1010. `1 + ((([__temp,__restore]=_withAsyncContext(()=>(foo))),__temp=await __temp,__restore(),__temp))`
  1011. )
  1012. })
  1013. test('ref', () => {
  1014. assertAwaitDetection(
  1015. `let a = $ref(1 + (await foo))`,
  1016. `1 + ((([__temp,__restore]=_withAsyncContext(()=>(foo))),__temp=await __temp,__restore(),__temp))`
  1017. )
  1018. })
  1019. test('nested statements', () => {
  1020. assertAwaitDetection(`if (ok) { await foo } else { await bar }`, code => {
  1021. return (
  1022. code.includes(
  1023. `;(([__temp,__restore]=_withAsyncContext(()=>(foo))),__temp=await __temp,__restore())`
  1024. ) &&
  1025. code.includes(
  1026. `;(([__temp,__restore]=_withAsyncContext(()=>(bar))),__temp=await __temp,__restore())`
  1027. )
  1028. )
  1029. })
  1030. })
  1031. test('should ignore await inside functions', () => {
  1032. // function declaration
  1033. assertAwaitDetection(
  1034. `async function foo() { await bar }`,
  1035. `await bar`,
  1036. false
  1037. )
  1038. // function expression
  1039. assertAwaitDetection(
  1040. `const foo = async () => { await bar }`,
  1041. `await bar`,
  1042. false
  1043. )
  1044. // object method
  1045. assertAwaitDetection(
  1046. `const obj = { async method() { await bar }}`,
  1047. `await bar`,
  1048. false
  1049. )
  1050. // class method
  1051. assertAwaitDetection(
  1052. `const cls = class Foo { async method() { await bar }}`,
  1053. `await bar`,
  1054. false
  1055. )
  1056. })
  1057. })
  1058. describe('errors', () => {
  1059. test('<script> and <script setup> must have same lang', () => {
  1060. expect(() =>
  1061. compile(`<script>foo()</script><script setup lang="ts">bar()</script>`)
  1062. ).toThrow(`<script> and <script setup> must have the same language type`)
  1063. })
  1064. const moduleErrorMsg = `cannot contain ES module exports`
  1065. test('non-type named exports', () => {
  1066. expect(() =>
  1067. compile(`<script setup>
  1068. export const a = 1
  1069. </script>`)
  1070. ).toThrow(moduleErrorMsg)
  1071. expect(() =>
  1072. compile(`<script setup>
  1073. export * from './foo'
  1074. </script>`)
  1075. ).toThrow(moduleErrorMsg)
  1076. expect(() =>
  1077. compile(`<script setup>
  1078. const bar = 1
  1079. export { bar as default }
  1080. </script>`)
  1081. ).toThrow(moduleErrorMsg)
  1082. })
  1083. test('defineProps/Emit() w/ both type and non-type args', () => {
  1084. expect(() => {
  1085. compile(`<script setup lang="ts">
  1086. defineProps<{}>({})
  1087. </script>`)
  1088. }).toThrow(`cannot accept both type and non-type arguments`)
  1089. expect(() => {
  1090. compile(`<script setup lang="ts">
  1091. defineEmits<{}>({})
  1092. </script>`)
  1093. }).toThrow(`cannot accept both type and non-type arguments`)
  1094. })
  1095. test('defineProps/Emit() referencing local var', () => {
  1096. expect(() =>
  1097. compile(`<script setup>
  1098. const bar = 1
  1099. defineProps({
  1100. foo: {
  1101. default: () => bar
  1102. }
  1103. })
  1104. </script>`)
  1105. ).toThrow(`cannot reference locally declared variables`)
  1106. expect(() =>
  1107. compile(`<script setup>
  1108. const bar = 'hello'
  1109. defineEmits([bar])
  1110. </script>`)
  1111. ).toThrow(`cannot reference locally declared variables`)
  1112. })
  1113. test('should allow defineProps/Emit() referencing scope var', () => {
  1114. assertCode(
  1115. compile(`<script setup>
  1116. const bar = 1
  1117. defineProps({
  1118. foo: {
  1119. default: bar => bar + 1
  1120. }
  1121. })
  1122. defineEmits({
  1123. foo: bar => bar > 1
  1124. })
  1125. </script>`).content
  1126. )
  1127. })
  1128. test('should allow defineProps/Emit() referencing imported binding', () => {
  1129. assertCode(
  1130. compile(`<script setup>
  1131. import { bar } from './bar'
  1132. defineProps({
  1133. foo: {
  1134. default: () => bar
  1135. }
  1136. })
  1137. defineEmits({
  1138. foo: () => bar > 1
  1139. })
  1140. </script>`).content
  1141. )
  1142. })
  1143. })
  1144. })
  1145. describe('SFC analyze <script> bindings', () => {
  1146. it('can parse decorators syntax in typescript block', () => {
  1147. const { scriptAst } = compile(`
  1148. <script lang="ts">
  1149. import { Options, Vue } from 'vue-class-component';
  1150. @Options({
  1151. components: {
  1152. HelloWorld,
  1153. },
  1154. props: ['foo', 'bar']
  1155. })
  1156. export default class Home extends Vue {}
  1157. </script>
  1158. `)
  1159. expect(scriptAst).toBeDefined()
  1160. })
  1161. it('recognizes props array declaration', () => {
  1162. const { bindings } = compile(`
  1163. <script>
  1164. export default {
  1165. props: ['foo', 'bar']
  1166. }
  1167. </script>
  1168. `)
  1169. expect(bindings).toStrictEqual({
  1170. foo: BindingTypes.PROPS,
  1171. bar: BindingTypes.PROPS
  1172. })
  1173. expect(bindings!.__isScriptSetup).toBe(false)
  1174. })
  1175. it('recognizes props object declaration', () => {
  1176. const { bindings } = compile(`
  1177. <script>
  1178. export default {
  1179. props: {
  1180. foo: String,
  1181. bar: {
  1182. type: String,
  1183. },
  1184. baz: null,
  1185. qux: [String, Number]
  1186. }
  1187. }
  1188. </script>
  1189. `)
  1190. expect(bindings).toStrictEqual({
  1191. foo: BindingTypes.PROPS,
  1192. bar: BindingTypes.PROPS,
  1193. baz: BindingTypes.PROPS,
  1194. qux: BindingTypes.PROPS
  1195. })
  1196. expect(bindings!.__isScriptSetup).toBe(false)
  1197. })
  1198. it('recognizes setup return', () => {
  1199. const { bindings } = compile(`
  1200. <script>
  1201. const bar = 2
  1202. export default {
  1203. setup() {
  1204. return {
  1205. foo: 1,
  1206. bar
  1207. }
  1208. }
  1209. }
  1210. </script>
  1211. `)
  1212. expect(bindings).toStrictEqual({
  1213. foo: BindingTypes.SETUP_MAYBE_REF,
  1214. bar: BindingTypes.SETUP_MAYBE_REF
  1215. })
  1216. expect(bindings!.__isScriptSetup).toBe(false)
  1217. })
  1218. it('recognizes async setup return', () => {
  1219. const { bindings } = compile(`
  1220. <script>
  1221. const bar = 2
  1222. export default {
  1223. async setup() {
  1224. return {
  1225. foo: 1,
  1226. bar
  1227. }
  1228. }
  1229. }
  1230. </script>
  1231. `)
  1232. expect(bindings).toStrictEqual({
  1233. foo: BindingTypes.SETUP_MAYBE_REF,
  1234. bar: BindingTypes.SETUP_MAYBE_REF
  1235. })
  1236. expect(bindings!.__isScriptSetup).toBe(false)
  1237. })
  1238. it('recognizes data return', () => {
  1239. const { bindings } = compile(`
  1240. <script>
  1241. const bar = 2
  1242. export default {
  1243. data() {
  1244. return {
  1245. foo: null,
  1246. bar
  1247. }
  1248. }
  1249. }
  1250. </script>
  1251. `)
  1252. expect(bindings).toStrictEqual({
  1253. foo: BindingTypes.DATA,
  1254. bar: BindingTypes.DATA
  1255. })
  1256. })
  1257. it('recognizes methods', () => {
  1258. const { bindings } = compile(`
  1259. <script>
  1260. export default {
  1261. methods: {
  1262. foo() {}
  1263. }
  1264. }
  1265. </script>
  1266. `)
  1267. expect(bindings).toStrictEqual({ foo: BindingTypes.OPTIONS })
  1268. })
  1269. it('recognizes computeds', () => {
  1270. const { bindings } = compile(`
  1271. <script>
  1272. export default {
  1273. computed: {
  1274. foo() {},
  1275. bar: {
  1276. get() {},
  1277. set() {},
  1278. }
  1279. }
  1280. }
  1281. </script>
  1282. `)
  1283. expect(bindings).toStrictEqual({
  1284. foo: BindingTypes.OPTIONS,
  1285. bar: BindingTypes.OPTIONS
  1286. })
  1287. })
  1288. it('recognizes injections array declaration', () => {
  1289. const { bindings } = compile(`
  1290. <script>
  1291. export default {
  1292. inject: ['foo', 'bar']
  1293. }
  1294. </script>
  1295. `)
  1296. expect(bindings).toStrictEqual({
  1297. foo: BindingTypes.OPTIONS,
  1298. bar: BindingTypes.OPTIONS
  1299. })
  1300. })
  1301. it('recognizes injections object declaration', () => {
  1302. const { bindings } = compile(`
  1303. <script>
  1304. export default {
  1305. inject: {
  1306. foo: {},
  1307. bar: {},
  1308. }
  1309. }
  1310. </script>
  1311. `)
  1312. expect(bindings).toStrictEqual({
  1313. foo: BindingTypes.OPTIONS,
  1314. bar: BindingTypes.OPTIONS
  1315. })
  1316. })
  1317. it('works for mixed bindings', () => {
  1318. const { bindings } = compile(`
  1319. <script>
  1320. export default {
  1321. inject: ['foo'],
  1322. props: {
  1323. bar: String,
  1324. },
  1325. setup() {
  1326. return {
  1327. baz: null,
  1328. }
  1329. },
  1330. data() {
  1331. return {
  1332. qux: null
  1333. }
  1334. },
  1335. methods: {
  1336. quux() {}
  1337. },
  1338. computed: {
  1339. quuz() {}
  1340. }
  1341. }
  1342. </script>
  1343. `)
  1344. expect(bindings).toStrictEqual({
  1345. foo: BindingTypes.OPTIONS,
  1346. bar: BindingTypes.PROPS,
  1347. baz: BindingTypes.SETUP_MAYBE_REF,
  1348. qux: BindingTypes.DATA,
  1349. quux: BindingTypes.OPTIONS,
  1350. quuz: BindingTypes.OPTIONS
  1351. })
  1352. })
  1353. it('works for script setup', () => {
  1354. const { bindings } = compile(`
  1355. <script setup>
  1356. import { ref as r } from 'vue'
  1357. defineProps({
  1358. foo: String
  1359. })
  1360. const a = r(1)
  1361. let b = 2
  1362. const c = 3
  1363. const { d } = someFoo()
  1364. let { e } = someBar()
  1365. </script>
  1366. `)
  1367. expect(bindings).toStrictEqual({
  1368. r: BindingTypes.SETUP_CONST,
  1369. a: BindingTypes.SETUP_REF,
  1370. b: BindingTypes.SETUP_LET,
  1371. c: BindingTypes.SETUP_CONST,
  1372. d: BindingTypes.SETUP_MAYBE_REF,
  1373. e: BindingTypes.SETUP_LET,
  1374. foo: BindingTypes.PROPS
  1375. })
  1376. })
  1377. })