compileScript.spec.ts 45 KB

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