compileScript.spec.ts 59 KB

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