compileScript.spec.ts 43 KB

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