compileScript.spec.ts 38 KB

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