compileScript.spec.ts 39 KB

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