compileScript.spec.ts 45 KB

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