compileScript.spec.ts 42 KB

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