compileScript.spec.ts 47 KB

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