compileScript.spec.ts 41 KB

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