2
0

compileScript.spec.ts 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182
  1. import { BindingTypes } from '@vue/compiler-core'
  2. import { compileSFCScript as compile, assertCode, mockId } from './utils'
  3. describe('SFC compile <script setup>', () => {
  4. test('should compile JS syntax', () => {
  5. const { content } = compile(`
  6. <script setup lang='js'>
  7. const a = 1
  8. const b = 2
  9. </script>
  10. `)
  11. expect(content).toMatch(`return { a, b }`)
  12. assertCode(content)
  13. })
  14. test('should expose top level declarations', () => {
  15. const { content, bindings } = compile(`
  16. <script setup>
  17. import { x } from './x'
  18. let a = 1
  19. const b = 2
  20. function c() {}
  21. class d {}
  22. </script>
  23. <script>
  24. import { xx } from './x'
  25. let aa = 1
  26. const bb = 2
  27. function cc() {}
  28. class dd {}
  29. </script>
  30. `)
  31. expect(content).toMatch(
  32. `return { get aa() { return aa }, set aa(v) { aa = v }, ` +
  33. `bb, cc, dd, get a() { return a }, set a(v) { a = v }, b, c, d, ` +
  34. `get xx() { return xx }, get x() { return x } }`
  35. )
  36. expect(bindings).toStrictEqual({
  37. x: BindingTypes.SETUP_MAYBE_REF,
  38. a: BindingTypes.SETUP_LET,
  39. b: BindingTypes.LITERAL_CONST,
  40. c: BindingTypes.SETUP_CONST,
  41. d: BindingTypes.SETUP_CONST,
  42. xx: BindingTypes.SETUP_MAYBE_REF,
  43. aa: BindingTypes.SETUP_LET,
  44. bb: BindingTypes.LITERAL_CONST,
  45. cc: BindingTypes.SETUP_CONST,
  46. dd: BindingTypes.SETUP_CONST
  47. })
  48. assertCode(content)
  49. })
  50. test('binding analysis for destructure', () => {
  51. const { content, bindings } = compile(`
  52. <script setup>
  53. const { foo, b: bar, ['x' + 'y']: baz, x: { y, zz: { z }}} = {}
  54. </script>
  55. `)
  56. expect(content).toMatch('return { foo, bar, baz, y, z }')
  57. expect(bindings).toStrictEqual({
  58. foo: BindingTypes.SETUP_MAYBE_REF,
  59. bar: BindingTypes.SETUP_MAYBE_REF,
  60. baz: BindingTypes.SETUP_MAYBE_REF,
  61. y: BindingTypes.SETUP_MAYBE_REF,
  62. z: BindingTypes.SETUP_MAYBE_REF
  63. })
  64. assertCode(content)
  65. })
  66. test('defineProps()', () => {
  67. const { content, bindings } = compile(`
  68. <script setup>
  69. const props = defineProps({
  70. foo: String
  71. })
  72. const bar = 1
  73. </script>
  74. `)
  75. // should generate working code
  76. assertCode(content)
  77. // should analyze bindings
  78. expect(bindings).toStrictEqual({
  79. foo: BindingTypes.PROPS,
  80. bar: BindingTypes.LITERAL_CONST,
  81. props: BindingTypes.SETUP_REACTIVE_CONST
  82. })
  83. // should remove defineOptions import and call
  84. expect(content).not.toMatch('defineProps')
  85. // should generate correct setup signature
  86. expect(content).toMatch(`setup(__props, { expose: __expose }) {`)
  87. // should assign user identifier to it
  88. expect(content).toMatch(`const props = __props`)
  89. // should include context options in default export
  90. expect(content).toMatch(`export default {
  91. props: {
  92. foo: String
  93. },`)
  94. })
  95. test('defineProps w/ external definition', () => {
  96. const { content } = compile(`
  97. <script setup>
  98. import { propsModel } from './props'
  99. const props = defineProps(propsModel)
  100. </script>
  101. `)
  102. assertCode(content)
  103. expect(content).toMatch(`export default {
  104. props: propsModel,`)
  105. })
  106. // #4764
  107. test('defineProps w/ leading code', () => {
  108. const { content } = compile(`
  109. <script setup>import { x } from './x'
  110. const props = defineProps({})
  111. </script>
  112. `)
  113. // props declaration should be inside setup, not moved along with the import
  114. expect(content).not.toMatch(`const props = __props\nimport`)
  115. assertCode(content)
  116. })
  117. test('defineEmits()', () => {
  118. const { content, bindings } = compile(`
  119. <script setup>
  120. const myEmit = defineEmits(['foo', 'bar'])
  121. </script>
  122. `)
  123. assertCode(content)
  124. expect(bindings).toStrictEqual({
  125. myEmit: BindingTypes.SETUP_CONST
  126. })
  127. // should remove defineOptions import and call
  128. expect(content).not.toMatch('defineEmits')
  129. // should generate correct setup signature
  130. expect(content).toMatch(
  131. `setup(__props, { expose: __expose, emit: myEmit }) {`
  132. )
  133. // should include context options in default export
  134. expect(content).toMatch(`export default {
  135. emits: ['foo', 'bar'],`)
  136. })
  137. test('defineProps/defineEmits in multi-variable declaration', () => {
  138. const { content } = compile(`
  139. <script setup>
  140. const props = defineProps(['item']),
  141. a = 1,
  142. emit = defineEmits(['a']);
  143. </script>
  144. `)
  145. assertCode(content)
  146. expect(content).toMatch(`const a = 1;`) // test correct removal
  147. expect(content).toMatch(`props: ['item'],`)
  148. expect(content).toMatch(`emits: ['a'],`)
  149. })
  150. // #6757
  151. test('defineProps/defineEmits in multi-variable declaration fix #6757 ', () => {
  152. const { content } = compile(`
  153. <script setup>
  154. const a = 1,
  155. props = defineProps(['item']),
  156. emit = defineEmits(['a']);
  157. </script>
  158. `)
  159. assertCode(content)
  160. expect(content).toMatch(`const a = 1;`) // test correct removal
  161. expect(content).toMatch(`props: ['item'],`)
  162. expect(content).toMatch(`emits: ['a'],`)
  163. })
  164. // #7422
  165. test('defineProps/defineEmits in multi-variable declaration fix #7422', () => {
  166. const { content } = compile(`
  167. <script setup>
  168. const props = defineProps(['item']),
  169. emits = defineEmits(['foo']),
  170. a = 0,
  171. b = 0;
  172. </script>
  173. `)
  174. assertCode(content)
  175. expect(content).toMatch(`props: ['item'],`)
  176. expect(content).toMatch(`emits: ['foo'],`)
  177. expect(content).toMatch(`const a = 0,`)
  178. expect(content).toMatch(`b = 0;`)
  179. })
  180. test('defineProps/defineEmits in multi-variable declaration (full removal)', () => {
  181. const { content } = compile(`
  182. <script setup>
  183. const props = defineProps(['item']),
  184. emit = defineEmits(['a']);
  185. </script>
  186. `)
  187. assertCode(content)
  188. expect(content).toMatch(`props: ['item'],`)
  189. expect(content).toMatch(`emits: ['a'],`)
  190. })
  191. describe('defineOptions()', () => {
  192. test('basic usage', () => {
  193. const { content } = compile(`
  194. <script setup>
  195. defineOptions({ name: 'FooApp' })
  196. </script>
  197. `)
  198. assertCode(content)
  199. // should remove defineOptions import and call
  200. expect(content).not.toMatch('defineOptions')
  201. // should include context options in default export
  202. expect(content).toMatch(
  203. `export default /*#__PURE__*/Object.assign({ name: 'FooApp' }, `
  204. )
  205. })
  206. it('should emit an error with two defineProps', () => {
  207. expect(() =>
  208. compile(`
  209. <script setup>
  210. defineOptions({ name: 'FooApp' })
  211. defineOptions({ name: 'BarApp' })
  212. </script>
  213. `)
  214. ).toThrowError('[@vue/compiler-sfc] duplicate defineOptions() call')
  215. })
  216. it('should emit an error with props or emits property', () => {
  217. expect(() =>
  218. compile(`
  219. <script setup>
  220. defineOptions({ props: { foo: String } })
  221. </script>
  222. `)
  223. ).toThrowError(
  224. '[@vue/compiler-sfc] defineOptions() cannot be used to declare props. Use defineProps() instead.'
  225. )
  226. expect(() =>
  227. compile(`
  228. <script setup>
  229. defineOptions({ emits: ['update'] })
  230. </script>
  231. `)
  232. ).toThrowError(
  233. '[@vue/compiler-sfc] defineOptions() cannot be used to declare emits. Use defineEmits() instead.'
  234. )
  235. })
  236. it('should emit an error with type generic', () => {
  237. expect(() =>
  238. compile(`
  239. <script setup lang="ts">
  240. defineOptions<{ name: 'FooApp' }>()
  241. </script>
  242. `)
  243. ).toThrowError(
  244. '[@vue/compiler-sfc] defineOptions() cannot accept type arguments'
  245. )
  246. })
  247. })
  248. test('defineExpose()', () => {
  249. const { content } = compile(`
  250. <script setup>
  251. defineExpose({ foo: 123 })
  252. </script>
  253. `)
  254. assertCode(content)
  255. // should remove defineOptions import and call
  256. expect(content).not.toMatch('defineExpose')
  257. // should generate correct setup signature
  258. expect(content).toMatch(`setup(__props, { expose: __expose }) {`)
  259. // should replace callee
  260. expect(content).toMatch(/\bexpose\(\{ foo: 123 \}\)/)
  261. })
  262. test('<script> after <script setup> the script content not end with `\\n`', () => {
  263. const { content } = compile(`
  264. <script setup>
  265. import { x } from './x'
  266. </script>
  267. <script>const n = 1</script>
  268. `)
  269. assertCode(content)
  270. })
  271. describe('<script> and <script setup> co-usage', () => {
  272. test('script first', () => {
  273. const { content } = compile(`
  274. <script>
  275. export const n = 1
  276. export default {}
  277. </script>
  278. <script setup>
  279. import { x } from './x'
  280. x()
  281. </script>
  282. `)
  283. assertCode(content)
  284. })
  285. test('script setup first', () => {
  286. const { content } = compile(`
  287. <script setup>
  288. import { x } from './x'
  289. x()
  290. </script>
  291. <script>
  292. export const n = 1
  293. export default {}
  294. </script>
  295. `)
  296. assertCode(content)
  297. })
  298. test('script setup first, named default export', () => {
  299. const { content } = compile(`
  300. <script setup>
  301. import { x } from './x'
  302. x()
  303. </script>
  304. <script>
  305. export const n = 1
  306. const def = {}
  307. export { def as default }
  308. </script>
  309. `)
  310. assertCode(content)
  311. })
  312. // #4395
  313. test('script setup first, lang="ts", script block content export default', () => {
  314. const { content } = compile(`
  315. <script setup lang="ts">
  316. import { x } from './x'
  317. x()
  318. </script>
  319. <script lang="ts">
  320. export default {
  321. name: "test"
  322. }
  323. </script>
  324. `)
  325. // ensure __default__ is declared before used
  326. expect(content).toMatch(/const __default__[\S\s]*\.\.\.__default__/m)
  327. assertCode(content)
  328. })
  329. describe('spaces in ExportDefaultDeclaration node', () => {
  330. // #4371
  331. test('with many spaces and newline', () => {
  332. // #4371
  333. const { content } = compile(`
  334. <script>
  335. export const n = 1
  336. export default
  337. {
  338. some:'option'
  339. }
  340. </script>
  341. <script setup>
  342. import { x } from './x'
  343. x()
  344. </script>
  345. `)
  346. assertCode(content)
  347. })
  348. test('with minimal spaces', () => {
  349. const { content } = compile(`
  350. <script>
  351. export const n = 1
  352. export default{
  353. some:'option'
  354. }
  355. </script>
  356. <script setup>
  357. import { x } from './x'
  358. x()
  359. </script>
  360. `)
  361. assertCode(content)
  362. })
  363. })
  364. test('export call expression as default', () => {
  365. const { content } = compile(`
  366. <script>
  367. function fn() {
  368. return "hello, world";
  369. }
  370. export default fn();
  371. </script>
  372. <script setup>
  373. console.log('foo')
  374. </script>
  375. `)
  376. assertCode(content)
  377. })
  378. })
  379. describe('imports', () => {
  380. test('should hoist and expose imports', () => {
  381. assertCode(
  382. compile(`<script setup>
  383. import { ref } from 'vue'
  384. import 'foo/css'
  385. </script>`).content
  386. )
  387. })
  388. test('should extract comment for import or type declarations', () => {
  389. assertCode(
  390. compile(`
  391. <script setup>
  392. import a from 'a' // comment
  393. import b from 'b'
  394. </script>
  395. `).content
  396. )
  397. })
  398. // #2740
  399. test('should allow defineProps/Emit at the start of imports', () => {
  400. assertCode(
  401. compile(`<script setup>
  402. import { ref } from 'vue'
  403. defineProps(['foo'])
  404. defineEmits(['bar'])
  405. const r = ref(0)
  406. </script>`).content
  407. )
  408. })
  409. test('dedupe between user & helper', () => {
  410. const { content } = compile(
  411. `
  412. <script setup>
  413. import { ref } from 'vue'
  414. let foo = $ref(1)
  415. </script>
  416. `,
  417. { reactivityTransform: true }
  418. )
  419. assertCode(content)
  420. expect(content).toMatch(`import { ref } from 'vue'`)
  421. })
  422. test('import dedupe between <script> and <script setup>', () => {
  423. const { content } = compile(`
  424. <script>
  425. import { x } from './x'
  426. </script>
  427. <script setup>
  428. import { x } from './x'
  429. x()
  430. </script>
  431. `)
  432. assertCode(content)
  433. expect(content.indexOf(`import { x }`)).toEqual(
  434. content.lastIndexOf(`import { x }`)
  435. )
  436. })
  437. describe('import ref/reactive function from other place', () => {
  438. test('import directly', () => {
  439. const { bindings } = compile(`
  440. <script setup>
  441. import { ref, reactive } from './foo'
  442. const foo = ref(1)
  443. const bar = reactive(1)
  444. </script>
  445. `)
  446. expect(bindings).toStrictEqual({
  447. ref: BindingTypes.SETUP_MAYBE_REF,
  448. reactive: BindingTypes.SETUP_MAYBE_REF,
  449. foo: BindingTypes.SETUP_MAYBE_REF,
  450. bar: BindingTypes.SETUP_MAYBE_REF
  451. })
  452. })
  453. test('import w/ alias', () => {
  454. const { bindings } = compile(`
  455. <script setup>
  456. import { ref as _ref, reactive as _reactive } from './foo'
  457. const foo = ref(1)
  458. const bar = reactive(1)
  459. </script>
  460. `)
  461. expect(bindings).toStrictEqual({
  462. _reactive: BindingTypes.SETUP_MAYBE_REF,
  463. _ref: BindingTypes.SETUP_MAYBE_REF,
  464. foo: BindingTypes.SETUP_MAYBE_REF,
  465. bar: BindingTypes.SETUP_MAYBE_REF
  466. })
  467. })
  468. test('aliased usage before import site', () => {
  469. const { bindings } = compile(`
  470. <script setup>
  471. const bar = x(1)
  472. import { reactive as x } from 'vue'
  473. </script>
  474. `)
  475. expect(bindings).toStrictEqual({
  476. bar: BindingTypes.SETUP_REACTIVE_CONST,
  477. x: BindingTypes.SETUP_CONST
  478. })
  479. })
  480. })
  481. test('should support module string names syntax', () => {
  482. const { content, bindings } = compile(`
  483. <script>
  484. import { "😏" as foo } from './foo'
  485. </script>
  486. <script setup>
  487. import { "😏" as foo } from './foo'
  488. </script>
  489. `)
  490. assertCode(content)
  491. expect(bindings).toStrictEqual({
  492. foo: BindingTypes.SETUP_MAYBE_REF
  493. })
  494. })
  495. })
  496. // in dev mode, declared bindings are returned as an object from setup()
  497. // when using TS, users may import types which should not be returned as
  498. // values, so we need to check import usage in the template to determine
  499. // what to be returned.
  500. describe('dev mode import usage check', () => {
  501. test('components', () => {
  502. const { content } = compile(`
  503. <script setup lang="ts">
  504. import { FooBar, FooBaz, FooQux, foo } from './x'
  505. const fooBar: FooBar = 1
  506. </script>
  507. <template>
  508. <FooBaz></FooBaz>
  509. <foo-qux/>
  510. <foo/>
  511. FooBar
  512. </template>
  513. `)
  514. // FooBar: should not be matched by plain text or incorrect case
  515. // FooBaz: used as PascalCase component
  516. // FooQux: used as kebab-case component
  517. // foo: lowercase component
  518. expect(content).toMatch(
  519. `return { fooBar, get FooBaz() { return FooBaz }, ` +
  520. `get FooQux() { return FooQux }, get foo() { return foo } }`
  521. )
  522. assertCode(content)
  523. })
  524. test('directive', () => {
  525. const { content } = compile(`
  526. <script setup lang="ts">
  527. import { vMyDir } from './x'
  528. </script>
  529. <template>
  530. <div v-my-dir></div>
  531. </template>
  532. `)
  533. expect(content).toMatch(`return { get vMyDir() { return vMyDir } }`)
  534. assertCode(content)
  535. })
  536. // https://github.com/vuejs/core/issues/4599
  537. test('attribute expressions', () => {
  538. const { content } = compile(`
  539. <script setup lang="ts">
  540. import { bar, baz } from './x'
  541. const cond = true
  542. </script>
  543. <template>
  544. <div :class="[cond ? '' : bar(), 'default']" :style="baz"></div>
  545. </template>
  546. `)
  547. expect(content).toMatch(
  548. `return { cond, get bar() { return bar }, get baz() { return baz } }`
  549. )
  550. assertCode(content)
  551. })
  552. test('vue interpolations', () => {
  553. const { content } = compile(`
  554. <script setup lang="ts">
  555. import { x, y, z, x$y } from './x'
  556. </script>
  557. <template>
  558. <div :id="z + 'y'">{{ x }} {{ yy }} {{ x$y }}</div>
  559. </template>
  560. `)
  561. // x: used in interpolation
  562. // y: should not be matched by {{ yy }} or 'y' in binding exps
  563. // x$y: #4274 should escape special chars when creating Regex
  564. expect(content).toMatch(
  565. `return { get x() { return x }, get z() { return z }, get x$y() { return x$y } }`
  566. )
  567. assertCode(content)
  568. })
  569. // #4340 interpolations in template strings
  570. test('js template string interpolations', () => {
  571. const { content } = compile(`
  572. <script setup lang="ts">
  573. import { VAR, VAR2, VAR3 } from './x'
  574. </script>
  575. <template>
  576. {{ \`\${VAR}VAR2\${VAR3}\` }}
  577. </template>
  578. `)
  579. // VAR2 should not be matched
  580. expect(content).toMatch(
  581. `return { get VAR() { return VAR }, get VAR3() { return VAR3 } }`
  582. )
  583. assertCode(content)
  584. })
  585. // edge case: last tag in template
  586. test('last tag', () => {
  587. const { content } = compile(`
  588. <script setup lang="ts">
  589. import { FooBaz, Last } from './x'
  590. </script>
  591. <template>
  592. <FooBaz></FooBaz>
  593. <Last/>
  594. </template>
  595. `)
  596. expect(content).toMatch(
  597. `return { get FooBaz() { return FooBaz }, get Last() { return Last } }`
  598. )
  599. assertCode(content)
  600. })
  601. test('TS annotations', () => {
  602. const { content } = compile(`
  603. <script setup lang="ts">
  604. import { Foo, Bar, Baz, Qux, Fred } from './x'
  605. const a = 1
  606. function b() {}
  607. </script>
  608. <template>
  609. {{ a as Foo }}
  610. {{ b<Bar>() }}
  611. {{ Baz }}
  612. <Comp v-slot="{ data }: Qux">{{ data }}</Comp>
  613. <div v-for="{ z = x as Qux } in list as Fred"/>
  614. </template>
  615. `)
  616. expect(content).toMatch(`return { a, b, get Baz() { return Baz } }`)
  617. assertCode(content)
  618. })
  619. // vuejs/vue#12591
  620. test('v-on inline statement', () => {
  621. // should not error
  622. compile(`
  623. <script setup lang="ts">
  624. import { foo } from './foo'
  625. </script>
  626. <template>
  627. <div @click="$emit('update:a');"></div>
  628. </template>
  629. `)
  630. })
  631. })
  632. describe('inlineTemplate mode', () => {
  633. test('should work', () => {
  634. const { content } = compile(
  635. `
  636. <script setup>
  637. import { ref } from 'vue'
  638. const count = ref(0)
  639. </script>
  640. <template>
  641. <div>{{ count }}</div>
  642. <div>static</div>
  643. </template>
  644. `,
  645. { inlineTemplate: true }
  646. )
  647. // check snapshot and make sure helper imports and
  648. // hoists are placed correctly.
  649. assertCode(content)
  650. // in inline mode, no need to call expose() since nothing is exposed
  651. // anyway!
  652. expect(content).not.toMatch(`expose()`)
  653. })
  654. test('with defineExpose()', () => {
  655. const { content } = compile(
  656. `
  657. <script setup>
  658. const count = ref(0)
  659. defineExpose({ count })
  660. </script>
  661. `,
  662. { inlineTemplate: true }
  663. )
  664. assertCode(content)
  665. expect(content).toMatch(`setup(__props, { expose: __expose })`)
  666. expect(content).toMatch(`expose({ count })`)
  667. })
  668. test('referencing scope components and directives', () => {
  669. const { content } = compile(
  670. `
  671. <script setup>
  672. import ChildComp from './Child.vue'
  673. import SomeOtherComp from './Other.vue'
  674. import vMyDir from './my-dir'
  675. </script>
  676. <template>
  677. <div v-my-dir></div>
  678. <ChildComp/>
  679. <some-other-comp/>
  680. </template>
  681. `,
  682. { inlineTemplate: true }
  683. )
  684. expect(content).toMatch('[_unref(vMyDir)]')
  685. expect(content).toMatch('_createVNode(ChildComp)')
  686. // kebab-case component support
  687. expect(content).toMatch('_createVNode(SomeOtherComp)')
  688. assertCode(content)
  689. })
  690. test('avoid unref() when necessary', () => {
  691. // function, const, component import
  692. const { content } = compile(
  693. `<script setup>
  694. import { ref } from 'vue'
  695. import Foo, { bar } from './Foo.vue'
  696. import other from './util'
  697. import * as tree from './tree'
  698. const count = ref(0)
  699. const constant = {}
  700. const maybe = foo()
  701. let lett = 1
  702. function fn() {}
  703. </script>
  704. <template>
  705. <Foo>{{ bar }}</Foo>
  706. <div @click="fn">{{ count }} {{ constant }} {{ maybe }} {{ lett }} {{ other }}</div>
  707. {{ tree.foo() }}
  708. </template>
  709. `,
  710. { inlineTemplate: true }
  711. )
  712. // no need to unref vue component import
  713. expect(content).toMatch(`createVNode(Foo,`)
  714. // #2699 should unref named imports from .vue
  715. expect(content).toMatch(`unref(bar)`)
  716. // should unref other imports
  717. expect(content).toMatch(`unref(other)`)
  718. // no need to unref constant literals
  719. expect(content).not.toMatch(`unref(constant)`)
  720. // should directly use .value for known refs
  721. expect(content).toMatch(`count.value`)
  722. // should unref() on const bindings that may be refs
  723. expect(content).toMatch(`unref(maybe)`)
  724. // should unref() on let bindings
  725. expect(content).toMatch(`unref(lett)`)
  726. // no need to unref namespace import (this also preserves tree-shaking)
  727. expect(content).toMatch(`tree.foo()`)
  728. // no need to unref function declarations
  729. expect(content).toMatch(`{ onClick: fn }`)
  730. // no need to mark constant fns in patch flag
  731. expect(content).not.toMatch(`PROPS`)
  732. assertCode(content)
  733. })
  734. test('v-model codegen', () => {
  735. const { content } = compile(
  736. `<script setup>
  737. import { ref } from 'vue'
  738. const count = ref(0)
  739. const maybe = foo()
  740. let lett = 1
  741. </script>
  742. <template>
  743. <input v-model="count">
  744. <input v-model="maybe">
  745. <input v-model="lett">
  746. </template>
  747. `,
  748. { inlineTemplate: true }
  749. )
  750. // known const ref: set value
  751. expect(content).toMatch(`(count).value = $event`)
  752. // const but maybe ref: assign if ref, otherwise do nothing
  753. expect(content).toMatch(`_isRef(maybe) ? (maybe).value = $event : null`)
  754. // let: handle both cases
  755. expect(content).toMatch(
  756. `_isRef(lett) ? (lett).value = $event : lett = $event`
  757. )
  758. assertCode(content)
  759. })
  760. test('v-model should not generate ref assignment code for non-setup bindings', () => {
  761. const { content } = compile(
  762. `<script setup>
  763. import { ref } from 'vue'
  764. const count = ref(0)
  765. </script>
  766. <script>
  767. export default {
  768. data() { return { foo: 123 } }
  769. }
  770. </script>
  771. <template>
  772. <input v-model="foo">
  773. </template>
  774. `,
  775. { inlineTemplate: true }
  776. )
  777. expect(content).not.toMatch(`_isRef(foo)`)
  778. })
  779. test('template assignment expression codegen', () => {
  780. const { content } = compile(
  781. `<script setup>
  782. import { ref } from 'vue'
  783. const count = ref(0)
  784. const maybe = foo()
  785. let lett = 1
  786. let v = ref(1)
  787. </script>
  788. <template>
  789. <div @click="count = 1"/>
  790. <div @click="maybe = count"/>
  791. <div @click="lett = count"/>
  792. <div @click="v += 1"/>
  793. <div @click="v -= 1"/>
  794. <div @click="() => {
  795. let a = '' + lett
  796. v = a
  797. }"/>
  798. <div @click="() => {
  799. // nested scopes
  800. (()=>{
  801. let x = a
  802. (()=>{
  803. let z = x
  804. let z2 = z
  805. })
  806. let lz = z
  807. })
  808. v = a
  809. }"/>
  810. </template>
  811. `,
  812. { inlineTemplate: true }
  813. )
  814. // known const ref: set value
  815. expect(content).toMatch(`count.value = 1`)
  816. // const but maybe ref: only assign after check
  817. expect(content).toMatch(`maybe.value = count.value`)
  818. // let: handle both cases
  819. expect(content).toMatch(
  820. `_isRef(lett) ? lett.value = count.value : lett = count.value`
  821. )
  822. expect(content).toMatch(`_isRef(v) ? v.value += 1 : v += 1`)
  823. expect(content).toMatch(`_isRef(v) ? v.value -= 1 : v -= 1`)
  824. expect(content).toMatch(`_isRef(v) ? v.value = a : v = a`)
  825. expect(content).toMatch(`_isRef(v) ? v.value = _ctx.a : v = _ctx.a`)
  826. assertCode(content)
  827. })
  828. test('template update expression codegen', () => {
  829. const { content } = compile(
  830. `<script setup>
  831. import { ref } from 'vue'
  832. const count = ref(0)
  833. const maybe = foo()
  834. let lett = 1
  835. </script>
  836. <template>
  837. <div @click="count++"/>
  838. <div @click="--count"/>
  839. <div @click="maybe++"/>
  840. <div @click="--maybe"/>
  841. <div @click="lett++"/>
  842. <div @click="--lett"/>
  843. </template>
  844. `,
  845. { inlineTemplate: true }
  846. )
  847. // known const ref: set value
  848. expect(content).toMatch(`count.value++`)
  849. expect(content).toMatch(`--count.value`)
  850. // const but maybe ref (non-ref case ignored)
  851. expect(content).toMatch(`maybe.value++`)
  852. expect(content).toMatch(`--maybe.value`)
  853. // let: handle both cases
  854. expect(content).toMatch(`_isRef(lett) ? lett.value++ : lett++`)
  855. expect(content).toMatch(`_isRef(lett) ? --lett.value : --lett`)
  856. assertCode(content)
  857. })
  858. test('template destructure assignment codegen', () => {
  859. const { content } = compile(
  860. `<script setup>
  861. import { ref } from 'vue'
  862. const val = {}
  863. const count = ref(0)
  864. const maybe = foo()
  865. let lett = 1
  866. </script>
  867. <template>
  868. <div @click="({ count } = val)"/>
  869. <div @click="[maybe] = val"/>
  870. <div @click="({ lett } = val)"/>
  871. </template>
  872. `,
  873. { inlineTemplate: true }
  874. )
  875. // known const ref: set value
  876. expect(content).toMatch(`({ count: count.value } = val)`)
  877. // const but maybe ref (non-ref case ignored)
  878. expect(content).toMatch(`[maybe.value] = val`)
  879. // let: assumes non-ref
  880. expect(content).toMatch(`{ lett: lett } = val`)
  881. assertCode(content)
  882. })
  883. test('ssr codegen', () => {
  884. const { content } = compile(
  885. `
  886. <script setup>
  887. import { ref } from 'vue'
  888. const count = ref(0)
  889. </script>
  890. <template>
  891. <div>{{ count }}</div>
  892. <div>static</div>
  893. </template>
  894. <style>
  895. div { color: v-bind(count) }
  896. </style>
  897. `,
  898. {
  899. inlineTemplate: true,
  900. templateOptions: {
  901. ssr: true
  902. }
  903. }
  904. )
  905. expect(content).toMatch(`\n __ssrInlineRender: true,\n`)
  906. expect(content).toMatch(`return (_ctx, _push`)
  907. expect(content).toMatch(`ssrInterpolate`)
  908. expect(content).not.toMatch(`useCssVars`)
  909. expect(content).toMatch(`"--${mockId}-count": (count.value)`)
  910. assertCode(content)
  911. })
  912. })
  913. describe('with TypeScript', () => {
  914. test('hoist type declarations', () => {
  915. const { content } = compile(`
  916. <script setup lang="ts">
  917. export interface Foo {}
  918. type Bar = {}
  919. </script>`)
  920. assertCode(content)
  921. })
  922. test('defineProps/Emit w/ runtime options', () => {
  923. const { content } = compile(`
  924. <script setup lang="ts">
  925. const props = defineProps({ foo: String })
  926. const emit = defineEmits(['a', 'b'])
  927. </script>
  928. `)
  929. assertCode(content)
  930. expect(content).toMatch(`export default /*#__PURE__*/_defineComponent({
  931. props: { foo: String },
  932. emits: ['a', 'b'],
  933. setup(__props, { expose: __expose, emit }) {`)
  934. })
  935. test('defineProps w/ type', () => {
  936. const { content, bindings } = compile(`
  937. <script setup lang="ts">
  938. interface Test {}
  939. type Alias = number[]
  940. defineProps<{
  941. string: string
  942. number: number
  943. boolean: boolean
  944. object: object
  945. objectLiteral: { a: number }
  946. fn: (n: number) => void
  947. functionRef: Function
  948. objectRef: Object
  949. dateTime: Date
  950. array: string[]
  951. arrayRef: Array<any>
  952. tuple: [number, number]
  953. set: Set<string>
  954. literal: 'foo'
  955. optional?: any
  956. recordRef: Record<string, null>
  957. interface: Test
  958. alias: Alias
  959. method(): void
  960. symbol: symbol
  961. extract: Extract<1 | 2 | boolean, 2>
  962. exclude: Exclude<1 | 2 | boolean, 2>
  963. uppercase: Uppercase<'foo'>
  964. params: Parameters<(foo: any) => void>
  965. nonNull: NonNullable<string | null>
  966. objectOrFn: {
  967. (): void
  968. foo: string
  969. }
  970. union: string | number
  971. literalUnion: 'foo' | 'bar'
  972. literalUnionNumber: 1 | 2 | 3 | 4 | 5
  973. literalUnionMixed: 'foo' | 1 | boolean
  974. intersection: Test & {}
  975. intersection2: 'foo' & ('foo' | 'bar')
  976. foo: ((item: any) => boolean) | null
  977. unknown: UnknownType
  978. unknownUnion: UnknownType | string
  979. unknownIntersection: UnknownType & Object
  980. }>()
  981. </script>`)
  982. assertCode(content)
  983. expect(content).toMatch(`string: { type: String, required: true }`)
  984. expect(content).toMatch(`number: { type: Number, required: true }`)
  985. expect(content).toMatch(`boolean: { type: Boolean, required: true }`)
  986. expect(content).toMatch(`object: { type: Object, required: true }`)
  987. expect(content).toMatch(`objectLiteral: { type: Object, required: true }`)
  988. expect(content).toMatch(`fn: { type: Function, required: true }`)
  989. expect(content).toMatch(`functionRef: { type: Function, required: true }`)
  990. expect(content).toMatch(`objectRef: { type: Object, required: true }`)
  991. expect(content).toMatch(`dateTime: { type: Date, required: true }`)
  992. expect(content).toMatch(`array: { type: Array, required: true }`)
  993. expect(content).toMatch(`arrayRef: { type: Array, required: true }`)
  994. expect(content).toMatch(`tuple: { type: Array, required: true }`)
  995. expect(content).toMatch(`set: { type: Set, required: true }`)
  996. expect(content).toMatch(`literal: { type: String, required: true }`)
  997. expect(content).toMatch(`optional: { type: null, required: false }`)
  998. expect(content).toMatch(`recordRef: { type: Object, required: true }`)
  999. expect(content).toMatch(`interface: { type: Object, required: true }`)
  1000. expect(content).toMatch(`alias: { type: Array, required: true }`)
  1001. expect(content).toMatch(`method: { type: Function, required: true }`)
  1002. expect(content).toMatch(`symbol: { type: Symbol, required: true }`)
  1003. expect(content).toMatch(
  1004. `objectOrFn: { type: [Function, Object], required: true },`
  1005. )
  1006. expect(content).toMatch(`extract: { type: Number, required: true }`)
  1007. expect(content).toMatch(
  1008. `exclude: { type: [Number, Boolean], required: true }`
  1009. )
  1010. expect(content).toMatch(`uppercase: { type: String, required: true }`)
  1011. expect(content).toMatch(`params: { type: Array, required: true }`)
  1012. expect(content).toMatch(`nonNull: { type: String, required: true }`)
  1013. expect(content).toMatch(
  1014. `union: { type: [String, Number], required: true }`
  1015. )
  1016. expect(content).toMatch(`literalUnion: { type: String, required: true }`)
  1017. expect(content).toMatch(
  1018. `literalUnionNumber: { type: Number, required: true }`
  1019. )
  1020. expect(content).toMatch(
  1021. `literalUnionMixed: { type: [String, Number, Boolean], required: true }`
  1022. )
  1023. expect(content).toMatch(`intersection: { type: Object, required: true }`)
  1024. expect(content).toMatch(`intersection2: { type: String, required: true }`)
  1025. expect(content).toMatch(`foo: { type: [Function, null], required: true }`)
  1026. expect(content).toMatch(`unknown: { type: null, required: true }`)
  1027. // uninon containing unknown type: skip check
  1028. expect(content).toMatch(`unknownUnion: { type: null, required: true }`)
  1029. // intersection containing unknown type: narrow to the known types
  1030. expect(content).toMatch(
  1031. `unknownIntersection: { type: Object, required: true }`
  1032. )
  1033. expect(bindings).toStrictEqual({
  1034. string: BindingTypes.PROPS,
  1035. number: BindingTypes.PROPS,
  1036. boolean: BindingTypes.PROPS,
  1037. object: BindingTypes.PROPS,
  1038. objectLiteral: BindingTypes.PROPS,
  1039. fn: BindingTypes.PROPS,
  1040. functionRef: BindingTypes.PROPS,
  1041. objectRef: BindingTypes.PROPS,
  1042. dateTime: BindingTypes.PROPS,
  1043. array: BindingTypes.PROPS,
  1044. arrayRef: BindingTypes.PROPS,
  1045. tuple: BindingTypes.PROPS,
  1046. set: BindingTypes.PROPS,
  1047. literal: BindingTypes.PROPS,
  1048. optional: BindingTypes.PROPS,
  1049. recordRef: BindingTypes.PROPS,
  1050. interface: BindingTypes.PROPS,
  1051. alias: BindingTypes.PROPS,
  1052. method: BindingTypes.PROPS,
  1053. symbol: BindingTypes.PROPS,
  1054. objectOrFn: BindingTypes.PROPS,
  1055. extract: BindingTypes.PROPS,
  1056. exclude: BindingTypes.PROPS,
  1057. union: BindingTypes.PROPS,
  1058. literalUnion: BindingTypes.PROPS,
  1059. literalUnionNumber: BindingTypes.PROPS,
  1060. literalUnionMixed: BindingTypes.PROPS,
  1061. intersection: BindingTypes.PROPS,
  1062. intersection2: BindingTypes.PROPS,
  1063. foo: BindingTypes.PROPS,
  1064. uppercase: BindingTypes.PROPS,
  1065. params: BindingTypes.PROPS,
  1066. nonNull: BindingTypes.PROPS,
  1067. unknown: BindingTypes.PROPS,
  1068. unknownUnion: BindingTypes.PROPS,
  1069. unknownIntersection: BindingTypes.PROPS
  1070. })
  1071. })
  1072. test('defineProps w/ interface', () => {
  1073. const { content, bindings } = compile(`
  1074. <script setup lang="ts">
  1075. interface Props { x?: number }
  1076. defineProps<Props>()
  1077. </script>
  1078. `)
  1079. assertCode(content)
  1080. expect(content).toMatch(`x: { type: Number, required: false }`)
  1081. expect(bindings).toStrictEqual({
  1082. x: BindingTypes.PROPS
  1083. })
  1084. })
  1085. test('defineProps w/ extends interface', () => {
  1086. const { content, bindings } = compile(`
  1087. <script lang="ts">
  1088. interface Foo { x?: number }
  1089. </script>
  1090. <script setup lang="ts">
  1091. interface Bar extends Foo { y?: number }
  1092. interface Props extends Bar {
  1093. z: number
  1094. y: string
  1095. }
  1096. defineProps<Props>()
  1097. </script>
  1098. `)
  1099. assertCode(content)
  1100. expect(content).toMatch(`z: { type: Number, required: true }`)
  1101. expect(content).toMatch(`y: { type: String, required: true }`)
  1102. expect(content).toMatch(`x: { type: Number, required: false }`)
  1103. expect(bindings).toStrictEqual({
  1104. x: BindingTypes.PROPS,
  1105. y: BindingTypes.PROPS,
  1106. z: BindingTypes.PROPS
  1107. })
  1108. })
  1109. test('defineProps w/ exported interface', () => {
  1110. const { content, bindings } = compile(`
  1111. <script setup lang="ts">
  1112. export interface Props { x?: number }
  1113. defineProps<Props>()
  1114. </script>
  1115. `)
  1116. assertCode(content)
  1117. expect(content).toMatch(`x: { type: Number, required: false }`)
  1118. expect(bindings).toStrictEqual({
  1119. x: BindingTypes.PROPS
  1120. })
  1121. })
  1122. test('defineProps w/ exported interface in normal script', () => {
  1123. const { content, bindings } = compile(`
  1124. <script lang="ts">
  1125. export interface Props { x?: number }
  1126. </script>
  1127. <script setup lang="ts">
  1128. defineProps<Props>()
  1129. </script>
  1130. `)
  1131. assertCode(content)
  1132. expect(content).toMatch(`x: { type: Number, required: false }`)
  1133. expect(bindings).toStrictEqual({
  1134. x: BindingTypes.PROPS
  1135. })
  1136. })
  1137. test('defineProps w/ type alias', () => {
  1138. const { content, bindings } = compile(`
  1139. <script setup lang="ts">
  1140. type Props = { x?: number }
  1141. defineProps<Props>()
  1142. </script>
  1143. `)
  1144. assertCode(content)
  1145. expect(content).toMatch(`x: { type: Number, required: false }`)
  1146. expect(bindings).toStrictEqual({
  1147. x: BindingTypes.PROPS
  1148. })
  1149. })
  1150. test('defineProps w/ exported type alias', () => {
  1151. const { content, bindings } = compile(`
  1152. <script setup lang="ts">
  1153. export type Props = { x?: number }
  1154. defineProps<Props>()
  1155. </script>
  1156. `)
  1157. assertCode(content)
  1158. expect(content).toMatch(`x: { type: Number, required: false }`)
  1159. expect(bindings).toStrictEqual({
  1160. x: BindingTypes.PROPS
  1161. })
  1162. })
  1163. test('defineProps w/ TS assertion', () => {
  1164. const { content, bindings } = compile(`
  1165. <script setup lang="ts">
  1166. defineProps(['foo'])! as any
  1167. </script>
  1168. `)
  1169. expect(content).toMatch(`props: ['foo']`)
  1170. assertCode(content)
  1171. expect(bindings).toStrictEqual({
  1172. foo: BindingTypes.PROPS
  1173. })
  1174. })
  1175. test('withDefaults (static)', () => {
  1176. const { content, bindings } = compile(`
  1177. <script setup lang="ts">
  1178. const props = withDefaults(defineProps<{
  1179. foo?: string
  1180. bar?: number;
  1181. baz: boolean;
  1182. qux?(): number;
  1183. quux?(): void
  1184. quuxx?: Promise<string>;
  1185. fred?: string
  1186. }>(), {
  1187. foo: 'hi',
  1188. qux() { return 1 },
  1189. ['quux']() { },
  1190. async quuxx() { return await Promise.resolve('hi') },
  1191. get fred() { return 'fred' }
  1192. })
  1193. </script>
  1194. `)
  1195. assertCode(content)
  1196. expect(content).toMatch(
  1197. `foo: { type: String, required: false, default: 'hi' }`
  1198. )
  1199. expect(content).toMatch(`bar: { type: Number, required: false }`)
  1200. expect(content).toMatch(`baz: { type: Boolean, required: true }`)
  1201. expect(content).toMatch(
  1202. `qux: { type: Function, required: false, default() { return 1 } }`
  1203. )
  1204. expect(content).toMatch(
  1205. `quux: { type: Function, required: false, default() { } }`
  1206. )
  1207. expect(content).toMatch(
  1208. `quuxx: { type: Promise, required: false, async default() { return await Promise.resolve('hi') } }`
  1209. )
  1210. expect(content).toMatch(
  1211. `fred: { type: String, required: false, get default() { return 'fred' } }`
  1212. )
  1213. expect(content).toMatch(
  1214. `{ foo: string, bar?: number, baz: boolean, qux(): number, quux(): void, quuxx: Promise<string>, fred: string }`
  1215. )
  1216. expect(content).toMatch(`const props = __props`)
  1217. expect(bindings).toStrictEqual({
  1218. foo: BindingTypes.PROPS,
  1219. bar: BindingTypes.PROPS,
  1220. baz: BindingTypes.PROPS,
  1221. qux: BindingTypes.PROPS,
  1222. quux: BindingTypes.PROPS,
  1223. quuxx: BindingTypes.PROPS,
  1224. fred: BindingTypes.PROPS,
  1225. props: BindingTypes.SETUP_CONST
  1226. })
  1227. })
  1228. test('withDefaults (static) + normal script', () => {
  1229. const { content } = compile(`
  1230. <script lang="ts">
  1231. interface Props {
  1232. a?: string;
  1233. }
  1234. </script>
  1235. <script setup lang="ts">
  1236. const props = withDefaults(defineProps<Props>(), {
  1237. a: "a",
  1238. });
  1239. </script>
  1240. `)
  1241. assertCode(content)
  1242. })
  1243. // #7111
  1244. test('withDefaults (static) w/ production mode', () => {
  1245. const { content } = compile(
  1246. `
  1247. <script setup lang="ts">
  1248. const props = withDefaults(defineProps<{
  1249. foo: () => void
  1250. bar: boolean
  1251. baz: boolean | (() => void)
  1252. qux: string | number
  1253. }>(), {
  1254. baz: true,
  1255. qux: 'hi'
  1256. })
  1257. </script>
  1258. `,
  1259. { isProd: true }
  1260. )
  1261. assertCode(content)
  1262. expect(content).toMatch(`const props = __props`)
  1263. // foo has no default value, the Function can be dropped
  1264. expect(content).toMatch(`foo: null`)
  1265. expect(content).toMatch(`bar: { type: Boolean }`)
  1266. expect(content).toMatch(
  1267. `baz: { type: [Boolean, Function], default: true }`
  1268. )
  1269. expect(content).toMatch(`qux: { default: 'hi' }`)
  1270. })
  1271. test('withDefaults (dynamic)', () => {
  1272. const { content } = compile(`
  1273. <script setup lang="ts">
  1274. import { defaults } from './foo'
  1275. const props = withDefaults(defineProps<{
  1276. foo?: string
  1277. bar?: number
  1278. baz: boolean
  1279. }>(), { ...defaults })
  1280. </script>
  1281. `)
  1282. assertCode(content)
  1283. expect(content).toMatch(`import { mergeDefaults as _mergeDefaults`)
  1284. expect(content).toMatch(
  1285. `
  1286. _mergeDefaults({
  1287. foo: { type: String, required: false },
  1288. bar: { type: Number, required: false },
  1289. baz: { type: Boolean, required: true }
  1290. }, { ...defaults })`.trim()
  1291. )
  1292. })
  1293. // #7111
  1294. test('withDefaults (dynamic) w/ production mode', () => {
  1295. const { content } = compile(
  1296. `
  1297. <script setup lang="ts">
  1298. import { defaults } from './foo'
  1299. const props = withDefaults(defineProps<{
  1300. foo: () => void
  1301. bar: boolean
  1302. baz: boolean | (() => void)
  1303. qux: string | number
  1304. }>(), { ...defaults })
  1305. </script>
  1306. `,
  1307. { isProd: true }
  1308. )
  1309. assertCode(content)
  1310. expect(content).toMatch(`import { mergeDefaults as _mergeDefaults`)
  1311. expect(content).toMatch(
  1312. `
  1313. _mergeDefaults({
  1314. foo: { type: Function },
  1315. bar: { type: Boolean },
  1316. baz: { type: [Boolean, Function] },
  1317. qux: null
  1318. }, { ...defaults })`.trim()
  1319. )
  1320. })
  1321. test('defineEmits w/ type', () => {
  1322. const { content } = compile(`
  1323. <script setup lang="ts">
  1324. const emit = defineEmits<(e: 'foo' | 'bar') => void>()
  1325. </script>
  1326. `)
  1327. assertCode(content)
  1328. expect(content).toMatch(`emit: ((e: 'foo' | 'bar') => void),`)
  1329. expect(content).toMatch(`emits: ["foo", "bar"]`)
  1330. })
  1331. test('defineEmits w/ type (union)', () => {
  1332. const type = `((e: 'foo' | 'bar') => void) | ((e: 'baz', id: number) => void)`
  1333. expect(() =>
  1334. compile(`
  1335. <script setup lang="ts">
  1336. const emit = defineEmits<${type}>()
  1337. </script>
  1338. `)
  1339. ).toThrow()
  1340. })
  1341. test('defineEmits w/ type (type literal w/ call signatures)', () => {
  1342. const type = `{(e: 'foo' | 'bar'): void; (e: 'baz', id: number): void;}`
  1343. const { content } = compile(`
  1344. <script setup lang="ts">
  1345. const emit = defineEmits<${type}>()
  1346. </script>
  1347. `)
  1348. assertCode(content)
  1349. expect(content).toMatch(`emit: (${type}),`)
  1350. expect(content).toMatch(`emits: ["foo", "bar", "baz"]`)
  1351. })
  1352. test('defineEmits w/ type (interface)', () => {
  1353. const { content } = compile(`
  1354. <script setup lang="ts">
  1355. interface Emits { (e: 'foo' | 'bar'): void }
  1356. const emit = defineEmits<Emits>()
  1357. </script>
  1358. `)
  1359. assertCode(content)
  1360. expect(content).toMatch(`emit: ({ (e: 'foo' | 'bar'): void }),`)
  1361. expect(content).toMatch(`emits: ["foo", "bar"]`)
  1362. })
  1363. test('defineEmits w/ type (exported interface)', () => {
  1364. const { content } = compile(`
  1365. <script setup lang="ts">
  1366. export interface Emits { (e: 'foo' | 'bar'): void }
  1367. const emit = defineEmits<Emits>()
  1368. </script>
  1369. `)
  1370. assertCode(content)
  1371. expect(content).toMatch(`emit: ({ (e: 'foo' | 'bar'): void }),`)
  1372. expect(content).toMatch(`emits: ["foo", "bar"]`)
  1373. })
  1374. test('defineEmits w/ type from normal script', () => {
  1375. const { content } = compile(`
  1376. <script lang="ts">
  1377. export interface Emits { (e: 'foo' | 'bar'): void }
  1378. </script>
  1379. <script setup lang="ts">
  1380. const emit = defineEmits<Emits>()
  1381. </script>
  1382. `)
  1383. assertCode(content)
  1384. expect(content).toMatch(`emit: ({ (e: 'foo' | 'bar'): void }),`)
  1385. expect(content).toMatch(`emits: ["foo", "bar"]`)
  1386. })
  1387. test('defineEmits w/ type (type alias)', () => {
  1388. const { content } = compile(`
  1389. <script setup lang="ts">
  1390. type Emits = { (e: 'foo' | 'bar'): void }
  1391. const emit = defineEmits<Emits>()
  1392. </script>
  1393. `)
  1394. assertCode(content)
  1395. expect(content).toMatch(`emit: ({ (e: 'foo' | 'bar'): void }),`)
  1396. expect(content).toMatch(`emits: ["foo", "bar"]`)
  1397. })
  1398. test('defineEmits w/ type (exported type alias)', () => {
  1399. const { content } = compile(`
  1400. <script setup lang="ts">
  1401. export type Emits = { (e: 'foo' | 'bar'): void }
  1402. const emit = defineEmits<Emits>()
  1403. </script>
  1404. `)
  1405. assertCode(content)
  1406. expect(content).toMatch(`emit: ({ (e: 'foo' | 'bar'): void }),`)
  1407. expect(content).toMatch(`emits: ["foo", "bar"]`)
  1408. })
  1409. test('defineEmits w/ type (referenced function type)', () => {
  1410. const { content } = compile(`
  1411. <script setup lang="ts">
  1412. type Emits = (e: 'foo' | 'bar') => void
  1413. const emit = defineEmits<Emits>()
  1414. </script>
  1415. `)
  1416. assertCode(content)
  1417. expect(content).toMatch(`emit: ((e: 'foo' | 'bar') => void),`)
  1418. expect(content).toMatch(`emits: ["foo", "bar"]`)
  1419. })
  1420. test('defineEmits w/ type (referenced exported function type)', () => {
  1421. const { content } = compile(`
  1422. <script setup lang="ts">
  1423. export type Emits = (e: 'foo' | 'bar') => void
  1424. const emit = defineEmits<Emits>()
  1425. </script>
  1426. `)
  1427. assertCode(content)
  1428. expect(content).toMatch(`emit: ((e: 'foo' | 'bar') => void),`)
  1429. expect(content).toMatch(`emits: ["foo", "bar"]`)
  1430. })
  1431. // #5393
  1432. test('defineEmits w/ type (interface ts type)', () => {
  1433. const { content } = compile(`
  1434. <script setup lang="ts">
  1435. interface Emits { (e: 'foo'): void }
  1436. const emit: Emits = defineEmits(['foo'])
  1437. </script>
  1438. `)
  1439. assertCode(content)
  1440. expect(content).toMatch(`setup(__props, { expose: __expose, emit }) {`)
  1441. expect(content).toMatch(`emits: ['foo']`)
  1442. })
  1443. test('runtime Enum', () => {
  1444. const { content, bindings } = compile(
  1445. `<script setup lang="ts">
  1446. enum Foo { A = 123 }
  1447. </script>`
  1448. )
  1449. assertCode(content)
  1450. expect(bindings).toStrictEqual({
  1451. Foo: BindingTypes.LITERAL_CONST
  1452. })
  1453. })
  1454. test('runtime Enum in normal script', () => {
  1455. const { content, bindings } = compile(
  1456. `<script lang="ts">
  1457. export enum D { D = "D" }
  1458. const enum C { C = "C" }
  1459. enum B { B = "B" }
  1460. </script>
  1461. <script setup lang="ts">
  1462. enum Foo { A = 123 }
  1463. </script>`
  1464. )
  1465. assertCode(content)
  1466. expect(bindings).toStrictEqual({
  1467. D: BindingTypes.LITERAL_CONST,
  1468. C: BindingTypes.LITERAL_CONST,
  1469. B: BindingTypes.LITERAL_CONST,
  1470. Foo: BindingTypes.LITERAL_CONST
  1471. })
  1472. })
  1473. test('const Enum', () => {
  1474. const { content, bindings } = compile(
  1475. `<script setup lang="ts">
  1476. const enum Foo { A = 123 }
  1477. </script>`,
  1478. { hoistStatic: true }
  1479. )
  1480. assertCode(content)
  1481. expect(bindings).toStrictEqual({
  1482. Foo: BindingTypes.LITERAL_CONST
  1483. })
  1484. })
  1485. test('runtime inference for Enum in defineProps', () => {
  1486. expect(
  1487. compile(
  1488. `<script setup lang="ts">
  1489. const enum Foo { A = 123 }
  1490. defineProps<{
  1491. foo: Foo
  1492. }>()
  1493. </script>`,
  1494. { hoistStatic: true }
  1495. ).content
  1496. ).toMatch(`foo: { type: Number`)
  1497. expect(
  1498. compile(
  1499. `<script setup lang="ts">
  1500. const enum Foo { A = '123' }
  1501. defineProps<{
  1502. foo: Foo
  1503. }>()
  1504. </script>`,
  1505. { hoistStatic: true }
  1506. ).content
  1507. ).toMatch(`foo: { type: String`)
  1508. expect(
  1509. compile(
  1510. `<script setup lang="ts">
  1511. const enum Foo { A = '123', B = 123 }
  1512. defineProps<{
  1513. foo: Foo
  1514. }>()
  1515. </script>`,
  1516. { hoistStatic: true }
  1517. ).content
  1518. ).toMatch(`foo: { type: [String, Number]`)
  1519. expect(
  1520. compile(
  1521. `<script setup lang="ts">
  1522. const enum Foo { A, B }
  1523. defineProps<{
  1524. foo: Foo
  1525. }>()
  1526. </script>`,
  1527. { hoistStatic: true }
  1528. ).content
  1529. ).toMatch(`foo: { type: Number`)
  1530. })
  1531. test('import type', () => {
  1532. const { content } = compile(
  1533. `<script setup lang="ts">
  1534. import type { Foo } from './main.ts'
  1535. import { type Bar, Baz } from './main.ts'
  1536. </script>`
  1537. )
  1538. expect(content).toMatch(`return { get Baz() { return Baz } }`)
  1539. assertCode(content)
  1540. })
  1541. })
  1542. describe('async/await detection', () => {
  1543. function assertAwaitDetection(code: string, shouldAsync = true) {
  1544. const { content } = compile(`<script setup>${code}</script>`, {
  1545. reactivityTransform: true
  1546. })
  1547. if (shouldAsync) {
  1548. expect(content).toMatch(`let __temp, __restore`)
  1549. }
  1550. expect(content).toMatch(`${shouldAsync ? `async ` : ``}setup(`)
  1551. assertCode(content)
  1552. return content
  1553. }
  1554. test('expression statement', () => {
  1555. assertAwaitDetection(`await foo`)
  1556. })
  1557. test('variable', () => {
  1558. assertAwaitDetection(`const a = 1 + (await foo)`)
  1559. })
  1560. test('ref', () => {
  1561. assertAwaitDetection(`let a = $ref(1 + (await foo))`)
  1562. })
  1563. // #4448
  1564. test('nested await', () => {
  1565. assertAwaitDetection(`await (await foo)`)
  1566. assertAwaitDetection(`await ((await foo))`)
  1567. assertAwaitDetection(`await (await (await foo))`)
  1568. })
  1569. // should prepend semicolon
  1570. test('nested leading await in expression statement', () => {
  1571. const code = assertAwaitDetection(`foo()\nawait 1 + await 2`)
  1572. expect(code).toMatch(`foo()\n;(`)
  1573. })
  1574. // #4596 should NOT prepend semicolon
  1575. test('single line conditions', () => {
  1576. const code = assertAwaitDetection(`if (false) await foo()`)
  1577. expect(code).not.toMatch(`if (false) ;(`)
  1578. })
  1579. test('nested statements', () => {
  1580. assertAwaitDetection(`if (ok) { await foo } else { await bar }`)
  1581. })
  1582. test('multiple `if` nested statements', () => {
  1583. assertAwaitDetection(`if (ok) {
  1584. let a = 'foo'
  1585. await 0 + await 1
  1586. await 2
  1587. } else if (a) {
  1588. await 10
  1589. if (b) {
  1590. await 0 + await 1
  1591. } else {
  1592. let a = 'foo'
  1593. await 2
  1594. }
  1595. if (b) {
  1596. await 3
  1597. await 4
  1598. }
  1599. } else {
  1600. await 5
  1601. }`)
  1602. })
  1603. test('multiple `if while` nested statements', () => {
  1604. assertAwaitDetection(`if (ok) {
  1605. while (d) {
  1606. await 5
  1607. }
  1608. while (d) {
  1609. await 5
  1610. await 6
  1611. if (c) {
  1612. let f = 10
  1613. 10 + await 7
  1614. } else {
  1615. await 8
  1616. await 9
  1617. }
  1618. }
  1619. }`)
  1620. })
  1621. test('multiple `if for` nested statements', () => {
  1622. assertAwaitDetection(`if (ok) {
  1623. for (let a of [1,2,3]) {
  1624. await a
  1625. }
  1626. for (let a of [1,2,3]) {
  1627. await a
  1628. await a
  1629. }
  1630. }`)
  1631. })
  1632. test('should ignore await inside functions', () => {
  1633. // function declaration
  1634. assertAwaitDetection(`async function foo() { await bar }`, false)
  1635. // function expression
  1636. assertAwaitDetection(`const foo = async () => { await bar }`, false)
  1637. // object method
  1638. assertAwaitDetection(`const obj = { async method() { await bar }}`, false)
  1639. // class method
  1640. assertAwaitDetection(
  1641. `const cls = class Foo { async method() { await bar }}`,
  1642. false
  1643. )
  1644. })
  1645. })
  1646. describe('errors', () => {
  1647. test('<script> and <script setup> must have same lang', () => {
  1648. expect(() =>
  1649. compile(`<script>foo()</script><script setup lang="ts">bar()</script>`)
  1650. ).toThrow(`<script> and <script setup> must have the same language type`)
  1651. })
  1652. const moduleErrorMsg = `cannot contain ES module exports`
  1653. test('non-type named exports', () => {
  1654. expect(() =>
  1655. compile(`<script setup>
  1656. export const a = 1
  1657. </script>`)
  1658. ).toThrow(moduleErrorMsg)
  1659. expect(() =>
  1660. compile(`<script setup>
  1661. export * from './foo'
  1662. </script>`)
  1663. ).toThrow(moduleErrorMsg)
  1664. expect(() =>
  1665. compile(`<script setup>
  1666. const bar = 1
  1667. export { bar as default }
  1668. </script>`)
  1669. ).toThrow(moduleErrorMsg)
  1670. })
  1671. test('defineProps/Emit() w/ both type and non-type args', () => {
  1672. expect(() => {
  1673. compile(`<script setup lang="ts">
  1674. defineProps<{}>({})
  1675. </script>`)
  1676. }).toThrow(`cannot accept both type and non-type arguments`)
  1677. expect(() => {
  1678. compile(`<script setup lang="ts">
  1679. defineEmits<{}>({})
  1680. </script>`)
  1681. }).toThrow(`cannot accept both type and non-type arguments`)
  1682. })
  1683. test('defineProps/Emit() referencing local var', () => {
  1684. expect(() =>
  1685. compile(`<script setup>
  1686. let bar = 1
  1687. defineProps({
  1688. foo: {
  1689. default: () => bar
  1690. }
  1691. })
  1692. </script>`)
  1693. ).toThrow(`cannot reference locally declared variables`)
  1694. expect(() =>
  1695. compile(`<script setup>
  1696. let bar = 'hello'
  1697. defineEmits([bar])
  1698. </script>`)
  1699. ).toThrow(`cannot reference locally declared variables`)
  1700. // #4644
  1701. expect(() =>
  1702. compile(`
  1703. <script>const bar = 1</script>
  1704. <script setup>
  1705. defineProps({
  1706. foo: {
  1707. default: () => bar
  1708. }
  1709. })
  1710. </script>`)
  1711. ).not.toThrow(`cannot reference locally declared variables`)
  1712. })
  1713. test('should allow defineProps/Emit() referencing scope var', () => {
  1714. assertCode(
  1715. compile(`<script setup>
  1716. const bar = 1
  1717. defineProps({
  1718. foo: {
  1719. default: bar => bar + 1
  1720. }
  1721. })
  1722. defineEmits({
  1723. foo: bar => bar > 1
  1724. })
  1725. </script>`).content
  1726. )
  1727. })
  1728. test('should allow defineProps/Emit() referencing imported binding', () => {
  1729. assertCode(
  1730. compile(`<script setup>
  1731. import { bar } from './bar'
  1732. defineProps({
  1733. foo: {
  1734. default: () => bar
  1735. }
  1736. })
  1737. defineEmits({
  1738. foo: () => bar > 1
  1739. })
  1740. </script>`).content
  1741. )
  1742. })
  1743. })
  1744. })
  1745. describe('SFC analyze <script> bindings', () => {
  1746. it('can parse decorators syntax in typescript block', () => {
  1747. const { scriptAst } = compile(`
  1748. <script lang="ts">
  1749. import { Options, Vue } from 'vue-class-component';
  1750. @Options({
  1751. components: {
  1752. HelloWorld,
  1753. },
  1754. props: ['foo', 'bar']
  1755. })
  1756. export default class Home extends Vue {}
  1757. </script>
  1758. `)
  1759. expect(scriptAst).toBeDefined()
  1760. })
  1761. it('recognizes props array declaration', () => {
  1762. const { bindings } = compile(`
  1763. <script>
  1764. export default {
  1765. props: ['foo', 'bar']
  1766. }
  1767. </script>
  1768. `)
  1769. expect(bindings).toStrictEqual({
  1770. foo: BindingTypes.PROPS,
  1771. bar: BindingTypes.PROPS
  1772. })
  1773. expect(bindings!.__isScriptSetup).toBe(false)
  1774. })
  1775. it('recognizes props object declaration', () => {
  1776. const { bindings } = compile(`
  1777. <script>
  1778. export default {
  1779. props: {
  1780. foo: String,
  1781. bar: {
  1782. type: String,
  1783. },
  1784. baz: null,
  1785. qux: [String, Number]
  1786. }
  1787. }
  1788. </script>
  1789. `)
  1790. expect(bindings).toStrictEqual({
  1791. foo: BindingTypes.PROPS,
  1792. bar: BindingTypes.PROPS,
  1793. baz: BindingTypes.PROPS,
  1794. qux: BindingTypes.PROPS
  1795. })
  1796. expect(bindings!.__isScriptSetup).toBe(false)
  1797. })
  1798. it('recognizes setup return', () => {
  1799. const { bindings } = compile(`
  1800. <script>
  1801. const bar = 2
  1802. export default {
  1803. setup() {
  1804. return {
  1805. foo: 1,
  1806. bar
  1807. }
  1808. }
  1809. }
  1810. </script>
  1811. `)
  1812. expect(bindings).toStrictEqual({
  1813. foo: BindingTypes.SETUP_MAYBE_REF,
  1814. bar: BindingTypes.SETUP_MAYBE_REF
  1815. })
  1816. expect(bindings!.__isScriptSetup).toBe(false)
  1817. })
  1818. it('recognizes exported vars', () => {
  1819. const { bindings } = compile(`
  1820. <script>
  1821. export const foo = 2
  1822. </script>
  1823. <script setup>
  1824. console.log(foo)
  1825. </script>
  1826. `)
  1827. expect(bindings).toStrictEqual({
  1828. foo: BindingTypes.LITERAL_CONST
  1829. })
  1830. })
  1831. it('recognizes async setup return', () => {
  1832. const { bindings } = compile(`
  1833. <script>
  1834. const bar = 2
  1835. export default {
  1836. async setup() {
  1837. return {
  1838. foo: 1,
  1839. bar
  1840. }
  1841. }
  1842. }
  1843. </script>
  1844. `)
  1845. expect(bindings).toStrictEqual({
  1846. foo: BindingTypes.SETUP_MAYBE_REF,
  1847. bar: BindingTypes.SETUP_MAYBE_REF
  1848. })
  1849. expect(bindings!.__isScriptSetup).toBe(false)
  1850. })
  1851. it('recognizes data return', () => {
  1852. const { bindings } = compile(`
  1853. <script>
  1854. const bar = 2
  1855. export default {
  1856. data() {
  1857. return {
  1858. foo: null,
  1859. bar
  1860. }
  1861. }
  1862. }
  1863. </script>
  1864. `)
  1865. expect(bindings).toStrictEqual({
  1866. foo: BindingTypes.DATA,
  1867. bar: BindingTypes.DATA
  1868. })
  1869. })
  1870. it('recognizes methods', () => {
  1871. const { bindings } = compile(`
  1872. <script>
  1873. export default {
  1874. methods: {
  1875. foo() {}
  1876. }
  1877. }
  1878. </script>
  1879. `)
  1880. expect(bindings).toStrictEqual({ foo: BindingTypes.OPTIONS })
  1881. })
  1882. it('recognizes computeds', () => {
  1883. const { bindings } = compile(`
  1884. <script>
  1885. export default {
  1886. computed: {
  1887. foo() {},
  1888. bar: {
  1889. get() {},
  1890. set() {},
  1891. }
  1892. }
  1893. }
  1894. </script>
  1895. `)
  1896. expect(bindings).toStrictEqual({
  1897. foo: BindingTypes.OPTIONS,
  1898. bar: BindingTypes.OPTIONS
  1899. })
  1900. })
  1901. it('recognizes injections array declaration', () => {
  1902. const { bindings } = compile(`
  1903. <script>
  1904. export default {
  1905. inject: ['foo', 'bar']
  1906. }
  1907. </script>
  1908. `)
  1909. expect(bindings).toStrictEqual({
  1910. foo: BindingTypes.OPTIONS,
  1911. bar: BindingTypes.OPTIONS
  1912. })
  1913. })
  1914. it('recognizes injections object declaration', () => {
  1915. const { bindings } = compile(`
  1916. <script>
  1917. export default {
  1918. inject: {
  1919. foo: {},
  1920. bar: {},
  1921. }
  1922. }
  1923. </script>
  1924. `)
  1925. expect(bindings).toStrictEqual({
  1926. foo: BindingTypes.OPTIONS,
  1927. bar: BindingTypes.OPTIONS
  1928. })
  1929. })
  1930. it('works for mixed bindings', () => {
  1931. const { bindings } = compile(`
  1932. <script>
  1933. export default {
  1934. inject: ['foo'],
  1935. props: {
  1936. bar: String,
  1937. },
  1938. setup() {
  1939. return {
  1940. baz: null,
  1941. }
  1942. },
  1943. data() {
  1944. return {
  1945. qux: null
  1946. }
  1947. },
  1948. methods: {
  1949. quux() {}
  1950. },
  1951. computed: {
  1952. quuz() {}
  1953. }
  1954. }
  1955. </script>
  1956. `)
  1957. expect(bindings).toStrictEqual({
  1958. foo: BindingTypes.OPTIONS,
  1959. bar: BindingTypes.PROPS,
  1960. baz: BindingTypes.SETUP_MAYBE_REF,
  1961. qux: BindingTypes.DATA,
  1962. quux: BindingTypes.OPTIONS,
  1963. quuz: BindingTypes.OPTIONS
  1964. })
  1965. })
  1966. it('works for script setup', () => {
  1967. const { bindings } = compile(`
  1968. <script setup>
  1969. import { ref as r } from 'vue'
  1970. defineProps({
  1971. foo: String
  1972. })
  1973. const a = r(1)
  1974. let b = 2
  1975. const c = 3
  1976. const { d } = someFoo()
  1977. let { e } = someBar()
  1978. </script>
  1979. `)
  1980. expect(bindings).toStrictEqual({
  1981. r: BindingTypes.SETUP_CONST,
  1982. a: BindingTypes.SETUP_REF,
  1983. b: BindingTypes.SETUP_LET,
  1984. c: BindingTypes.LITERAL_CONST,
  1985. d: BindingTypes.SETUP_MAYBE_REF,
  1986. e: BindingTypes.SETUP_LET,
  1987. foo: BindingTypes.PROPS
  1988. })
  1989. })
  1990. describe('auto name inference', () => {
  1991. test('basic', () => {
  1992. const { content } = compile(
  1993. `<script setup>const a = 1</script>
  1994. <template>{{ a }}</template>`,
  1995. undefined,
  1996. {
  1997. filename: 'FooBar.vue'
  1998. }
  1999. )
  2000. expect(content).toMatch(`export default {
  2001. __name: 'FooBar'`)
  2002. assertCode(content)
  2003. })
  2004. test('do not overwrite manual name (object)', () => {
  2005. const { content } = compile(
  2006. `<script>
  2007. export default {
  2008. name: 'Baz'
  2009. }
  2010. </script>
  2011. <script setup>const a = 1</script>
  2012. <template>{{ a }}</template>`,
  2013. undefined,
  2014. {
  2015. filename: 'FooBar.vue'
  2016. }
  2017. )
  2018. expect(content).not.toMatch(`name: 'FooBar'`)
  2019. expect(content).toMatch(`name: 'Baz'`)
  2020. assertCode(content)
  2021. })
  2022. test('do not overwrite manual name (call)', () => {
  2023. const { content } = compile(
  2024. `<script>
  2025. import { defineComponent } from 'vue'
  2026. export default defineComponent({
  2027. name: 'Baz'
  2028. })
  2029. </script>
  2030. <script setup>const a = 1</script>
  2031. <template>{{ a }}</template>`,
  2032. undefined,
  2033. {
  2034. filename: 'FooBar.vue'
  2035. }
  2036. )
  2037. expect(content).not.toMatch(`name: 'FooBar'`)
  2038. expect(content).toMatch(`name: 'Baz'`)
  2039. assertCode(content)
  2040. })
  2041. })
  2042. })