2
0

compileScript.spec.ts 46 KB

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