compileScript.spec.ts 50 KB

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