compileScript.spec.ts 45 KB

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