compileScript.spec.ts 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189
  1. import { BindingTypes } from '@vue/compiler-dom'
  2. import { compileSFCScript as compile, assertCode } from './utils'
  3. describe('SFC compile <script setup>', () => {
  4. test('should expose top level declarations', () => {
  5. const { content } = compile(`
  6. <script setup>
  7. import { x } from './x'
  8. let a = 1
  9. const b = 2
  10. function c() {}
  11. class d {}
  12. </script>
  13. `)
  14. assertCode(content)
  15. expect(content).toMatch('return { a, b, c, d, x }')
  16. })
  17. test('defineProps()', () => {
  18. const { content, bindings } = compile(`
  19. <script setup>
  20. import { defineProps } from 'vue'
  21. const props = defineProps({
  22. foo: String
  23. })
  24. const bar = 1
  25. </script>
  26. `)
  27. // should generate working code
  28. assertCode(content)
  29. // should anayze bindings
  30. expect(bindings).toStrictEqual({
  31. foo: BindingTypes.PROPS,
  32. bar: BindingTypes.SETUP_CONST,
  33. props: BindingTypes.SETUP_CONST
  34. })
  35. // should remove defineOptions import and call
  36. expect(content).not.toMatch('defineProps')
  37. // should generate correct setup signature
  38. expect(content).toMatch(`setup(__props) {`)
  39. // should assign user identifier to it
  40. expect(content).toMatch(`const props = __props`)
  41. // should include context options in default export
  42. expect(content).toMatch(`export default {
  43. expose: [],
  44. props: {
  45. foo: String
  46. },`)
  47. })
  48. test('defineEmit()', () => {
  49. const { content, bindings } = compile(`
  50. <script setup>
  51. import { defineEmit } from 'vue'
  52. const myEmit = defineEmit(['foo', 'bar'])
  53. </script>
  54. `)
  55. assertCode(content)
  56. expect(bindings).toStrictEqual({
  57. myEmit: BindingTypes.SETUP_CONST
  58. })
  59. // should remove defineOptions import and call
  60. expect(content).not.toMatch('defineEmit')
  61. // should generate correct setup signature
  62. expect(content).toMatch(`setup(__props, { emit: myEmit }) {`)
  63. // should include context options in default export
  64. expect(content).toMatch(`export default {
  65. expose: [],
  66. emits: ['foo', 'bar'],`)
  67. })
  68. test('<template inherit-attrs="false">', () => {
  69. const { content } = compile(`
  70. <script>
  71. export default {}
  72. </script>
  73. <template inherit-attrs="false">
  74. {{ a }}
  75. </template>
  76. `)
  77. assertCode(content)
  78. const { content: content2 } = compile(`
  79. <script setup>
  80. const a = 1
  81. </script>
  82. <template inherit-attrs="false">
  83. {{ a }}
  84. </template>
  85. `)
  86. assertCode(content2)
  87. })
  88. describe('<script> and <script setup> co-usage', () => {
  89. test('script first', () => {
  90. const { content } = compile(`
  91. <script>
  92. export const n = 1
  93. </script>
  94. <script setup>
  95. import { x } from './x'
  96. x()
  97. </script>
  98. `)
  99. assertCode(content)
  100. })
  101. test('script setup first', () => {
  102. const { content } = compile(`
  103. <script setup>
  104. import { x } from './x'
  105. x()
  106. </script>
  107. <script>
  108. export const n = 1
  109. </script>
  110. `)
  111. assertCode(content)
  112. })
  113. })
  114. describe('imports', () => {
  115. test('should hoist and expose imports', () => {
  116. assertCode(
  117. compile(`<script setup>
  118. import { ref } from 'vue'
  119. import 'foo/css'
  120. </script>`).content
  121. )
  122. })
  123. test('should extract comment for import or type declarations', () => {
  124. assertCode(
  125. compile(`
  126. <script setup>
  127. import a from 'a' // comment
  128. import b from 'b'
  129. </script>
  130. `).content
  131. )
  132. })
  133. // #2740
  134. test('should allow defineProps/Emit at the start of imports', () => {
  135. assertCode(
  136. compile(`<script setup>
  137. import { defineProps, defineEmit, ref } from 'vue'
  138. defineProps(['foo'])
  139. defineEmit(['bar'])
  140. const r = ref(0)
  141. </script>`).content
  142. )
  143. })
  144. test('dedupe between user & helper', () => {
  145. const { content } = compile(`
  146. <script setup>
  147. import { ref } from 'vue'
  148. ref: foo = 1
  149. </script>
  150. `)
  151. assertCode(content)
  152. expect(content).toMatch(`import { ref } from 'vue'`)
  153. })
  154. test('import dedupe between <script> and <script setup>', () => {
  155. const { content } = compile(`
  156. <script>
  157. import { x } from './x'
  158. </script>
  159. <script setup>
  160. import { x } from './x'
  161. x()
  162. </script>
  163. `)
  164. assertCode(content)
  165. expect(content.indexOf(`import { x }`)).toEqual(
  166. content.lastIndexOf(`import { x }`)
  167. )
  168. })
  169. })
  170. describe('inlineTemplate mode', () => {
  171. test('should work', () => {
  172. const { content } = compile(
  173. `
  174. <script setup>
  175. import { ref } from 'vue'
  176. const count = ref(0)
  177. </script>
  178. <template>
  179. <div>{{ count }}</div>
  180. <div>static</div>
  181. </template>
  182. `,
  183. { inlineTemplate: true }
  184. )
  185. // check snapshot and make sure helper imports and
  186. // hoists are placed correctly.
  187. assertCode(content)
  188. })
  189. test('referencing scope components and directives', () => {
  190. const { content } = compile(
  191. `
  192. <script setup>
  193. import ChildComp from './Child.vue'
  194. import SomeOtherComp from './Other.vue'
  195. import myDir from './my-dir'
  196. </script>
  197. <template>
  198. <div v-my-dir></div>
  199. <ChildComp/>
  200. <some-other-comp/>
  201. </template>
  202. `,
  203. { inlineTemplate: true }
  204. )
  205. expect(content).toMatch('[_unref(myDir)]')
  206. expect(content).toMatch('_createVNode(ChildComp)')
  207. // kebab-case component support
  208. expect(content).toMatch('_createVNode(SomeOtherComp)')
  209. assertCode(content)
  210. })
  211. test('avoid unref() when necessary', () => {
  212. // function, const, component import
  213. const { content } = compile(
  214. `<script setup>
  215. import { ref } from 'vue'
  216. import Foo, { bar } from './Foo.vue'
  217. import other from './util'
  218. const count = ref(0)
  219. const constant = {}
  220. const maybe = foo()
  221. let lett = 1
  222. function fn() {}
  223. </script>
  224. <template>
  225. <Foo>{{ bar }}</Foo>
  226. <div @click="fn">{{ count }} {{ constant }} {{ maybe }} {{ lett }} {{ other }}</div>
  227. </template>
  228. `,
  229. { inlineTemplate: true }
  230. )
  231. // no need to unref vue component import
  232. expect(content).toMatch(`createVNode(Foo,`)
  233. // #2699 should unref named imports from .vue
  234. expect(content).toMatch(`unref(bar)`)
  235. // should unref other imports
  236. expect(content).toMatch(`unref(other)`)
  237. // no need to unref constant literals
  238. expect(content).not.toMatch(`unref(constant)`)
  239. // should directly use .value for known refs
  240. expect(content).toMatch(`count.value`)
  241. // should unref() on const bindings that may be refs
  242. expect(content).toMatch(`unref(maybe)`)
  243. // should unref() on let bindings
  244. expect(content).toMatch(`unref(lett)`)
  245. // no need to unref function declarations
  246. expect(content).toMatch(`{ onClick: fn }`)
  247. // no need to mark constant fns in patch flag
  248. expect(content).not.toMatch(`PROPS`)
  249. assertCode(content)
  250. })
  251. test('v-model codegen', () => {
  252. const { content } = compile(
  253. `<script setup>
  254. import { ref } from 'vue'
  255. const count = ref(0)
  256. const maybe = foo()
  257. let lett = 1
  258. </script>
  259. <template>
  260. <input v-model="count">
  261. <input v-model="maybe">
  262. <input v-model="lett">
  263. </template>
  264. `,
  265. { inlineTemplate: true }
  266. )
  267. // known const ref: set value
  268. expect(content).toMatch(`count.value = $event`)
  269. // const but maybe ref: also assign .value directly since non-ref
  270. // won't work
  271. expect(content).toMatch(`maybe.value = $event`)
  272. // let: handle both cases
  273. expect(content).toMatch(
  274. `_isRef(lett) ? lett.value = $event : lett = $event`
  275. )
  276. assertCode(content)
  277. })
  278. test('template assignment expression codegen', () => {
  279. const { content } = compile(
  280. `<script setup>
  281. import { ref } from 'vue'
  282. const count = ref(0)
  283. const maybe = foo()
  284. let lett = 1
  285. </script>
  286. <template>
  287. <div @click="count = 1"/>
  288. <div @click="maybe = count"/>
  289. <div @click="lett = count"/>
  290. </template>
  291. `,
  292. { inlineTemplate: true }
  293. )
  294. // known const ref: set value
  295. expect(content).toMatch(`count.value = 1`)
  296. // const but maybe ref: only assign after check
  297. expect(content).toMatch(`maybe.value = count.value`)
  298. // let: handle both cases
  299. expect(content).toMatch(
  300. `_isRef(lett) ? lett.value = count.value : lett = count.value`
  301. )
  302. assertCode(content)
  303. })
  304. test('template update expression codegen', () => {
  305. const { content } = compile(
  306. `<script setup>
  307. import { ref } from 'vue'
  308. const count = ref(0)
  309. const maybe = foo()
  310. let lett = 1
  311. </script>
  312. <template>
  313. <div @click="count++"/>
  314. <div @click="--count"/>
  315. <div @click="maybe++"/>
  316. <div @click="--maybe"/>
  317. <div @click="lett++"/>
  318. <div @click="--lett"/>
  319. </template>
  320. `,
  321. { inlineTemplate: true }
  322. )
  323. // known const ref: set value
  324. expect(content).toMatch(`count.value++`)
  325. expect(content).toMatch(`--count.value`)
  326. // const but maybe ref (non-ref case ignored)
  327. expect(content).toMatch(`maybe.value++`)
  328. expect(content).toMatch(`--maybe.value`)
  329. // let: handle both cases
  330. expect(content).toMatch(`_isRef(lett) ? lett.value++ : lett++`)
  331. expect(content).toMatch(`_isRef(lett) ? --lett.value : --lett`)
  332. assertCode(content)
  333. })
  334. test('template destructure assignment codegen', () => {
  335. const { content } = compile(
  336. `<script setup>
  337. import { ref } from 'vue'
  338. const val = {}
  339. const count = ref(0)
  340. const maybe = foo()
  341. let lett = 1
  342. </script>
  343. <template>
  344. <div @click="({ count } = val)"/>
  345. <div @click="[maybe] = val"/>
  346. <div @click="({ lett } = val)"/>
  347. </template>
  348. `,
  349. { inlineTemplate: true }
  350. )
  351. // known const ref: set value
  352. expect(content).toMatch(`({ count: count.value } = val)`)
  353. // const but maybe ref (non-ref case ignored)
  354. expect(content).toMatch(`[maybe.value] = val`)
  355. // let: assumes non-ref
  356. expect(content).toMatch(`{ lett: lett } = val`)
  357. assertCode(content)
  358. })
  359. test('ssr codegen', () => {
  360. const { content } = compile(
  361. `
  362. <script setup>
  363. import { ref } from 'vue'
  364. const count = ref(0)
  365. </script>
  366. <template>
  367. <div>{{ count }}</div>
  368. <div>static</div>
  369. </template>
  370. <style>
  371. div { color: v-bind(count) }
  372. </style>
  373. `,
  374. {
  375. inlineTemplate: true,
  376. templateOptions: {
  377. ssr: true
  378. }
  379. }
  380. )
  381. expect(content).toMatch(`\n __ssrInlineRender: true,\n`)
  382. expect(content).toMatch(`return (_ctx, _push`)
  383. expect(content).toMatch(`ssrInterpolate`)
  384. assertCode(content)
  385. })
  386. })
  387. describe('with TypeScript', () => {
  388. test('hoist type declarations', () => {
  389. const { content } = compile(`
  390. <script setup lang="ts">
  391. export interface Foo {}
  392. type Bar = {}
  393. </script>`)
  394. assertCode(content)
  395. })
  396. test('defineProps/Emit w/ runtime options', () => {
  397. const { content } = compile(`
  398. <script setup lang="ts">
  399. import { defineProps, defineEmit } from 'vue'
  400. const props = defineProps({ foo: String })
  401. const emit = defineEmit(['a', 'b'])
  402. </script>
  403. `)
  404. assertCode(content)
  405. expect(content).toMatch(`export default _defineComponent({
  406. expose: [],
  407. props: { foo: String },
  408. emits: ['a', 'b'],
  409. setup(__props, { emit }) {`)
  410. })
  411. test('defineProps w/ type', () => {
  412. const { content, bindings } = compile(`
  413. <script setup lang="ts">
  414. import { defineProps } from 'vue'
  415. interface Test {}
  416. type Alias = number[]
  417. defineProps<{
  418. string: string
  419. number: number
  420. boolean: boolean
  421. object: object
  422. objectLiteral: { a: number }
  423. fn: (n: number) => void
  424. functionRef: Function
  425. objectRef: Object
  426. array: string[]
  427. arrayRef: Array<any>
  428. tuple: [number, number]
  429. set: Set<string>
  430. literal: 'foo'
  431. optional?: any
  432. recordRef: Record<string, null>
  433. interface: Test
  434. alias: Alias
  435. union: string | number
  436. literalUnion: 'foo' | 'bar'
  437. literalUnionMixed: 'foo' | 1 | boolean
  438. intersection: Test & {}
  439. }>()
  440. </script>`)
  441. assertCode(content)
  442. expect(content).toMatch(`string: { type: String, required: true }`)
  443. expect(content).toMatch(`number: { type: Number, required: true }`)
  444. expect(content).toMatch(`boolean: { type: Boolean, required: true }`)
  445. expect(content).toMatch(`object: { type: Object, required: true }`)
  446. expect(content).toMatch(`objectLiteral: { type: Object, required: true }`)
  447. expect(content).toMatch(`fn: { type: Function, required: true }`)
  448. expect(content).toMatch(`functionRef: { type: Function, required: true }`)
  449. expect(content).toMatch(`objectRef: { type: Object, required: true }`)
  450. expect(content).toMatch(`array: { type: Array, required: true }`)
  451. expect(content).toMatch(`arrayRef: { type: Array, required: true }`)
  452. expect(content).toMatch(`tuple: { type: Array, required: true }`)
  453. expect(content).toMatch(`set: { type: Set, required: true }`)
  454. expect(content).toMatch(`literal: { type: String, required: true }`)
  455. expect(content).toMatch(`optional: { type: null, required: false }`)
  456. expect(content).toMatch(`recordRef: { type: Object, required: true }`)
  457. expect(content).toMatch(`interface: { type: Object, required: true }`)
  458. expect(content).toMatch(`alias: { type: Array, required: true }`)
  459. expect(content).toMatch(
  460. `union: { type: [String, Number], required: true }`
  461. )
  462. expect(content).toMatch(
  463. `literalUnion: { type: [String, String], required: true }`
  464. )
  465. expect(content).toMatch(
  466. `literalUnionMixed: { type: [String, Number, Boolean], required: true }`
  467. )
  468. expect(content).toMatch(`intersection: { type: Object, required: true }`)
  469. expect(bindings).toStrictEqual({
  470. string: BindingTypes.PROPS,
  471. number: BindingTypes.PROPS,
  472. boolean: BindingTypes.PROPS,
  473. object: BindingTypes.PROPS,
  474. objectLiteral: BindingTypes.PROPS,
  475. fn: BindingTypes.PROPS,
  476. functionRef: BindingTypes.PROPS,
  477. objectRef: BindingTypes.PROPS,
  478. array: BindingTypes.PROPS,
  479. arrayRef: BindingTypes.PROPS,
  480. tuple: BindingTypes.PROPS,
  481. set: BindingTypes.PROPS,
  482. literal: BindingTypes.PROPS,
  483. optional: BindingTypes.PROPS,
  484. recordRef: BindingTypes.PROPS,
  485. interface: BindingTypes.PROPS,
  486. alias: BindingTypes.PROPS,
  487. union: BindingTypes.PROPS,
  488. literalUnion: BindingTypes.PROPS,
  489. literalUnionMixed: BindingTypes.PROPS,
  490. intersection: BindingTypes.PROPS
  491. })
  492. })
  493. test('defineEmit w/ type', () => {
  494. const { content } = compile(`
  495. <script setup lang="ts">
  496. import { defineEmit } from 'vue'
  497. const emit = defineEmit<(e: 'foo' | 'bar') => void>()
  498. </script>
  499. `)
  500. assertCode(content)
  501. expect(content).toMatch(`emit: ((e: 'foo' | 'bar') => void),`)
  502. expect(content).toMatch(`emits: ["foo", "bar"] as unknown as undefined`)
  503. })
  504. test('defineEmit w/ type (union)', () => {
  505. const type = `((e: 'foo' | 'bar') => void) | ((e: 'baz', id: number) => void)`
  506. const { content } = compile(`
  507. <script setup lang="ts">
  508. import { defineEmit } from 'vue'
  509. const emit = defineEmit<${type}>()
  510. </script>
  511. `)
  512. assertCode(content)
  513. expect(content).toMatch(`emit: (${type}),`)
  514. expect(content).toMatch(
  515. `emits: ["foo", "bar", "baz"] as unknown as undefined`
  516. )
  517. })
  518. })
  519. describe('async/await detection', () => {
  520. function assertAwaitDetection(code: string, shouldAsync = true) {
  521. const { content } = compile(`<script setup>${code}</script>`)
  522. expect(content).toMatch(`${shouldAsync ? `async ` : ``}setup(`)
  523. }
  524. test('expression statement', () => {
  525. assertAwaitDetection(`await foo`)
  526. })
  527. test('variable', () => {
  528. assertAwaitDetection(`const a = 1 + (await foo)`)
  529. })
  530. test('ref', () => {
  531. assertAwaitDetection(`ref: a = 1 + (await foo)`)
  532. })
  533. test('nested statements', () => {
  534. assertAwaitDetection(`if (ok) { await foo } else { await bar }`)
  535. })
  536. test('should ignore await inside functions', () => {
  537. // function declaration
  538. assertAwaitDetection(`async function foo() { await bar }`, false)
  539. // function expression
  540. assertAwaitDetection(`const foo = async () => { await bar }`, false)
  541. // object method
  542. assertAwaitDetection(`const obj = { async method() { await bar }}`, false)
  543. // class method
  544. assertAwaitDetection(
  545. `const cls = class Foo { async method() { await bar }}`,
  546. false
  547. )
  548. })
  549. })
  550. describe('ref: syntax sugar', () => {
  551. test('convert ref declarations', () => {
  552. const { content, bindings } = compile(`<script setup>
  553. ref: foo
  554. ref: a = 1
  555. ref: b = {
  556. count: 0
  557. }
  558. let c = () => {}
  559. let d
  560. </script>`)
  561. expect(content).toMatch(`import { ref as _ref } from 'vue'`)
  562. expect(content).not.toMatch(`ref: foo`)
  563. expect(content).not.toMatch(`ref: a`)
  564. expect(content).not.toMatch(`ref: b`)
  565. expect(content).toMatch(`const foo = _ref()`)
  566. expect(content).toMatch(`const a = _ref(1)`)
  567. expect(content).toMatch(`
  568. const b = _ref({
  569. count: 0
  570. })
  571. `)
  572. // normal declarations left untouched
  573. expect(content).toMatch(`let c = () => {}`)
  574. expect(content).toMatch(`let d`)
  575. assertCode(content)
  576. expect(bindings).toStrictEqual({
  577. foo: BindingTypes.SETUP_REF,
  578. a: BindingTypes.SETUP_REF,
  579. b: BindingTypes.SETUP_REF,
  580. c: BindingTypes.SETUP_LET,
  581. d: BindingTypes.SETUP_LET
  582. })
  583. })
  584. test('multi ref declarations', () => {
  585. const { content, bindings } = compile(`<script setup>
  586. ref: a = 1, b = 2, c = {
  587. count: 0
  588. }
  589. </script>`)
  590. expect(content).toMatch(`
  591. const a = _ref(1), b = _ref(2), c = _ref({
  592. count: 0
  593. })
  594. `)
  595. expect(content).toMatch(`return { a, b, c }`)
  596. assertCode(content)
  597. expect(bindings).toStrictEqual({
  598. a: BindingTypes.SETUP_REF,
  599. b: BindingTypes.SETUP_REF,
  600. c: BindingTypes.SETUP_REF
  601. })
  602. })
  603. test('should not convert non ref labels', () => {
  604. const { content } = compile(`<script setup>
  605. foo: a = 1, b = 2, c = {
  606. count: 0
  607. }
  608. </script>`)
  609. expect(content).toMatch(`foo: a = 1, b = 2`)
  610. assertCode(content)
  611. })
  612. test('accessing ref binding', () => {
  613. const { content } = compile(`<script setup>
  614. ref: a = 1
  615. console.log(a)
  616. function get() {
  617. return a + 1
  618. }
  619. </script>`)
  620. expect(content).toMatch(`console.log(a.value)`)
  621. expect(content).toMatch(`return a.value + 1`)
  622. assertCode(content)
  623. })
  624. test('cases that should not append .value', () => {
  625. const { content } = compile(`<script setup>
  626. ref: a = 1
  627. console.log(b.a)
  628. function get(a) {
  629. return a + 1
  630. }
  631. </script>`)
  632. expect(content).not.toMatch(`a.value`)
  633. })
  634. test('mutating ref binding', () => {
  635. const { content } = compile(`<script setup>
  636. ref: a = 1
  637. ref: b = { count: 0 }
  638. function inc() {
  639. a++
  640. a = a + 1
  641. b.count++
  642. b.count = b.count + 1
  643. ;({ a } = { a: 2 })
  644. ;[a] = [1]
  645. }
  646. </script>`)
  647. expect(content).toMatch(`a.value++`)
  648. expect(content).toMatch(`a.value = a.value + 1`)
  649. expect(content).toMatch(`b.value.count++`)
  650. expect(content).toMatch(`b.value.count = b.value.count + 1`)
  651. expect(content).toMatch(`;({ a: a.value } = { a: 2 })`)
  652. expect(content).toMatch(`;[a.value] = [1]`)
  653. assertCode(content)
  654. })
  655. test('using ref binding in property shorthand', () => {
  656. const { content } = compile(`<script setup>
  657. ref: a = 1
  658. const b = { a }
  659. function test() {
  660. const { a } = b
  661. }
  662. </script>`)
  663. expect(content).toMatch(`const b = { a: a.value }`)
  664. // should not convert destructure
  665. expect(content).toMatch(`const { a } = b`)
  666. assertCode(content)
  667. })
  668. test('should not rewrite scope variable', () => {
  669. const { content } = compile(`
  670. <script setup>
  671. ref: a = 1
  672. ref: b = 1
  673. ref: d = 1
  674. const e = 1
  675. function test() {
  676. const a = 2
  677. console.log(a)
  678. console.log(b)
  679. let c = { c: 3 }
  680. console.log(c)
  681. let $d
  682. console.log($d)
  683. console.log(d)
  684. console.log(e)
  685. }
  686. </script>`)
  687. expect(content).toMatch('console.log(a)')
  688. expect(content).toMatch('console.log(b.value)')
  689. expect(content).toMatch('console.log(c)')
  690. expect(content).toMatch('console.log($d)')
  691. expect(content).toMatch('console.log(d.value)')
  692. expect(content).toMatch('console.log(e)')
  693. assertCode(content)
  694. })
  695. test('object destructure', () => {
  696. const { content, bindings } = compile(`<script setup>
  697. ref: n = 1, ({ a, b: c, d = 1, e: f = 2, ...g } = useFoo())
  698. console.log(n, a, c, d, f, g)
  699. </script>`)
  700. expect(content).toMatch(
  701. `const n = _ref(1), { a: __a, b: __c, d: __d = 1, e: __f = 2, ...__g } = useFoo()`
  702. )
  703. expect(content).toMatch(`\nconst a = _ref(__a);`)
  704. expect(content).not.toMatch(`\nconst b = _ref(__b);`)
  705. expect(content).toMatch(`\nconst c = _ref(__c);`)
  706. expect(content).toMatch(`\nconst d = _ref(__d);`)
  707. expect(content).not.toMatch(`\nconst e = _ref(__e);`)
  708. expect(content).toMatch(`\nconst f = _ref(__f);`)
  709. expect(content).toMatch(`\nconst g = _ref(__g);`)
  710. expect(content).toMatch(
  711. `console.log(n.value, a.value, c.value, d.value, f.value, g.value)`
  712. )
  713. expect(content).toMatch(`return { n, a, c, d, f, g }`)
  714. expect(bindings).toStrictEqual({
  715. n: BindingTypes.SETUP_REF,
  716. a: BindingTypes.SETUP_REF,
  717. c: BindingTypes.SETUP_REF,
  718. d: BindingTypes.SETUP_REF,
  719. f: BindingTypes.SETUP_REF,
  720. g: BindingTypes.SETUP_REF
  721. })
  722. assertCode(content)
  723. })
  724. test('array destructure', () => {
  725. const { content, bindings } = compile(`<script setup>
  726. ref: n = 1, [a, b = 1, ...c] = useFoo()
  727. console.log(n, a, b, c)
  728. </script>`)
  729. expect(content).toMatch(
  730. `const n = _ref(1), [__a, __b = 1, ...__c] = useFoo()`
  731. )
  732. expect(content).toMatch(`\nconst a = _ref(__a);`)
  733. expect(content).toMatch(`\nconst b = _ref(__b);`)
  734. expect(content).toMatch(`\nconst c = _ref(__c);`)
  735. expect(content).toMatch(`console.log(n.value, a.value, b.value, c.value)`)
  736. expect(content).toMatch(`return { n, a, b, c }`)
  737. expect(bindings).toStrictEqual({
  738. n: BindingTypes.SETUP_REF,
  739. a: BindingTypes.SETUP_REF,
  740. b: BindingTypes.SETUP_REF,
  741. c: BindingTypes.SETUP_REF
  742. })
  743. assertCode(content)
  744. })
  745. test('nested destructure', () => {
  746. const { content, bindings } = compile(`<script setup>
  747. ref: [{ a: { b }}] = useFoo()
  748. ref: ({ c: [d, e] } = useBar())
  749. console.log(b, d, e)
  750. </script>`)
  751. expect(content).toMatch(`const [{ a: { b: __b }}] = useFoo()`)
  752. expect(content).toMatch(`const { c: [__d, __e] } = useBar()`)
  753. expect(content).not.toMatch(`\nconst a = _ref(__a);`)
  754. expect(content).not.toMatch(`\nconst c = _ref(__c);`)
  755. expect(content).toMatch(`\nconst b = _ref(__b);`)
  756. expect(content).toMatch(`\nconst d = _ref(__d);`)
  757. expect(content).toMatch(`\nconst e = _ref(__e);`)
  758. expect(content).toMatch(`return { b, d, e }`)
  759. expect(bindings).toStrictEqual({
  760. b: BindingTypes.SETUP_REF,
  761. d: BindingTypes.SETUP_REF,
  762. e: BindingTypes.SETUP_REF
  763. })
  764. assertCode(content)
  765. })
  766. })
  767. describe('errors', () => {
  768. test('<script> and <script setup> must have same lang', () => {
  769. expect(() =>
  770. compile(`<script>foo()</script><script setup lang="ts">bar()</script>`)
  771. ).toThrow(`<script> and <script setup> must have the same language type`)
  772. })
  773. const moduleErrorMsg = `cannot contain ES module exports`
  774. test('non-type named exports', () => {
  775. expect(() =>
  776. compile(`<script setup>
  777. export const a = 1
  778. </script>`)
  779. ).toThrow(moduleErrorMsg)
  780. expect(() =>
  781. compile(`<script setup>
  782. export * from './foo'
  783. </script>`)
  784. ).toThrow(moduleErrorMsg)
  785. expect(() =>
  786. compile(`<script setup>
  787. const bar = 1
  788. export { bar as default }
  789. </script>`)
  790. ).toThrow(moduleErrorMsg)
  791. })
  792. test('ref: non-assignment expressions', () => {
  793. expect(() =>
  794. compile(`<script setup>
  795. ref: a = 1, foo()
  796. </script>`)
  797. ).toThrow(`ref: statements can only contain assignment expressions`)
  798. })
  799. test('defineProps/Emit() w/ both type and non-type args', () => {
  800. expect(() => {
  801. compile(`<script setup lang="ts">
  802. import { defineProps } from 'vue'
  803. defineProps<{}>({})
  804. </script>`)
  805. }).toThrow(`cannot accept both type and non-type arguments`)
  806. expect(() => {
  807. compile(`<script setup lang="ts">
  808. import { defineEmit } from 'vue'
  809. defineEmit<{}>({})
  810. </script>`)
  811. }).toThrow(`cannot accept both type and non-type arguments`)
  812. })
  813. test('defineProps/Emit() referencing local var', () => {
  814. expect(() =>
  815. compile(`<script setup>
  816. import { defineProps } from 'vue'
  817. const bar = 1
  818. defineProps({
  819. foo: {
  820. default: () => bar
  821. }
  822. })
  823. </script>`)
  824. ).toThrow(`cannot reference locally declared variables`)
  825. expect(() =>
  826. compile(`<script setup>
  827. import { defineEmit } from 'vue'
  828. const bar = 'hello'
  829. defineEmit([bar])
  830. </script>`)
  831. ).toThrow(`cannot reference locally declared variables`)
  832. })
  833. test('defineProps/Emit() referencing ref declarations', () => {
  834. expect(() =>
  835. compile(`<script setup>
  836. import { defineProps } from 'vue'
  837. ref: bar = 1
  838. defineProps({
  839. bar
  840. })
  841. </script>`)
  842. ).toThrow(`cannot reference locally declared variables`)
  843. expect(() =>
  844. compile(`<script setup>
  845. import { defineEmit } from 'vue'
  846. ref: bar = 1
  847. defineEmit({
  848. bar
  849. })
  850. </script>`)
  851. ).toThrow(`cannot reference locally declared variables`)
  852. })
  853. test('should allow defineProps/Emit() referencing scope var', () => {
  854. assertCode(
  855. compile(`<script setup>
  856. import { defineProps, defineEmit } from 'vue'
  857. const bar = 1
  858. defineProps({
  859. foo: {
  860. default: bar => bar + 1
  861. }
  862. })
  863. defineEmit({
  864. foo: bar => bar > 1
  865. })
  866. </script>`).content
  867. )
  868. })
  869. test('should allow defineProps/Emit() referencing imported binding', () => {
  870. assertCode(
  871. compile(`<script setup>
  872. import { defineProps, defineEmit } from 'vue'
  873. import { bar } from './bar'
  874. defineProps({
  875. foo: {
  876. default: () => bar
  877. }
  878. })
  879. defineEmit({
  880. foo: () => bar > 1
  881. })
  882. </script>`).content
  883. )
  884. })
  885. })
  886. })
  887. describe('SFC analyze <script> bindings', () => {
  888. it('can parse decorators syntax in typescript block', () => {
  889. const { scriptAst } = compile(`
  890. <script lang="ts">
  891. import { Options, Vue } from 'vue-class-component';
  892. @Options({
  893. components: {
  894. HelloWorld,
  895. },
  896. props: ['foo', 'bar']
  897. })
  898. export default class Home extends Vue {}
  899. </script>
  900. `)
  901. expect(scriptAst).toBeDefined()
  902. })
  903. it('recognizes props array declaration', () => {
  904. const { bindings } = compile(`
  905. <script>
  906. export default {
  907. props: ['foo', 'bar']
  908. }
  909. </script>
  910. `)
  911. expect(bindings).toStrictEqual({
  912. foo: BindingTypes.PROPS,
  913. bar: BindingTypes.PROPS
  914. })
  915. })
  916. it('recognizes props object declaration', () => {
  917. const { bindings } = compile(`
  918. <script>
  919. export default {
  920. props: {
  921. foo: String,
  922. bar: {
  923. type: String,
  924. },
  925. baz: null,
  926. qux: [String, Number]
  927. }
  928. }
  929. </script>
  930. `)
  931. expect(bindings).toStrictEqual({
  932. foo: BindingTypes.PROPS,
  933. bar: BindingTypes.PROPS,
  934. baz: BindingTypes.PROPS,
  935. qux: BindingTypes.PROPS
  936. })
  937. })
  938. it('recognizes setup return', () => {
  939. const { bindings } = compile(`
  940. <script>
  941. const bar = 2
  942. export default {
  943. setup() {
  944. return {
  945. foo: 1,
  946. bar
  947. }
  948. }
  949. }
  950. </script>
  951. `)
  952. expect(bindings).toStrictEqual({
  953. foo: BindingTypes.SETUP_MAYBE_REF,
  954. bar: BindingTypes.SETUP_MAYBE_REF
  955. })
  956. })
  957. it('recognizes async setup return', () => {
  958. const { bindings } = compile(`
  959. <script>
  960. const bar = 2
  961. export default {
  962. async setup() {
  963. return {
  964. foo: 1,
  965. bar
  966. }
  967. }
  968. }
  969. </script>
  970. `)
  971. expect(bindings).toStrictEqual({
  972. foo: BindingTypes.SETUP_MAYBE_REF,
  973. bar: BindingTypes.SETUP_MAYBE_REF
  974. })
  975. })
  976. it('recognizes data return', () => {
  977. const { bindings } = compile(`
  978. <script>
  979. const bar = 2
  980. export default {
  981. data() {
  982. return {
  983. foo: null,
  984. bar
  985. }
  986. }
  987. }
  988. </script>
  989. `)
  990. expect(bindings).toStrictEqual({
  991. foo: BindingTypes.DATA,
  992. bar: BindingTypes.DATA
  993. })
  994. })
  995. it('recognizes methods', () => {
  996. const { bindings } = compile(`
  997. <script>
  998. export default {
  999. methods: {
  1000. foo() {}
  1001. }
  1002. }
  1003. </script>
  1004. `)
  1005. expect(bindings).toStrictEqual({ foo: BindingTypes.OPTIONS })
  1006. })
  1007. it('recognizes computeds', () => {
  1008. const { bindings } = compile(`
  1009. <script>
  1010. export default {
  1011. computed: {
  1012. foo() {},
  1013. bar: {
  1014. get() {},
  1015. set() {},
  1016. }
  1017. }
  1018. }
  1019. </script>
  1020. `)
  1021. expect(bindings).toStrictEqual({
  1022. foo: BindingTypes.OPTIONS,
  1023. bar: BindingTypes.OPTIONS
  1024. })
  1025. })
  1026. it('recognizes injections array declaration', () => {
  1027. const { bindings } = compile(`
  1028. <script>
  1029. export default {
  1030. inject: ['foo', 'bar']
  1031. }
  1032. </script>
  1033. `)
  1034. expect(bindings).toStrictEqual({
  1035. foo: BindingTypes.OPTIONS,
  1036. bar: BindingTypes.OPTIONS
  1037. })
  1038. })
  1039. it('recognizes injections object declaration', () => {
  1040. const { bindings } = compile(`
  1041. <script>
  1042. export default {
  1043. inject: {
  1044. foo: {},
  1045. bar: {},
  1046. }
  1047. }
  1048. </script>
  1049. `)
  1050. expect(bindings).toStrictEqual({
  1051. foo: BindingTypes.OPTIONS,
  1052. bar: BindingTypes.OPTIONS
  1053. })
  1054. })
  1055. it('works for mixed bindings', () => {
  1056. const { bindings } = compile(`
  1057. <script>
  1058. export default {
  1059. inject: ['foo'],
  1060. props: {
  1061. bar: String,
  1062. },
  1063. setup() {
  1064. return {
  1065. baz: null,
  1066. }
  1067. },
  1068. data() {
  1069. return {
  1070. qux: null
  1071. }
  1072. },
  1073. methods: {
  1074. quux() {}
  1075. },
  1076. computed: {
  1077. quuz() {}
  1078. }
  1079. }
  1080. </script>
  1081. `)
  1082. expect(bindings).toStrictEqual({
  1083. foo: BindingTypes.OPTIONS,
  1084. bar: BindingTypes.PROPS,
  1085. baz: BindingTypes.SETUP_MAYBE_REF,
  1086. qux: BindingTypes.DATA,
  1087. quux: BindingTypes.OPTIONS,
  1088. quuz: BindingTypes.OPTIONS
  1089. })
  1090. })
  1091. it('works for script setup', () => {
  1092. const { bindings } = compile(`
  1093. <script setup>
  1094. import { defineProps, ref as r } from 'vue'
  1095. defineProps({
  1096. foo: String
  1097. })
  1098. const a = r(1)
  1099. let b = 2
  1100. const c = 3
  1101. const { d } = someFoo()
  1102. let { e } = someBar()
  1103. </script>
  1104. `)
  1105. expect(bindings).toStrictEqual({
  1106. r: BindingTypes.SETUP_CONST,
  1107. a: BindingTypes.SETUP_REF,
  1108. b: BindingTypes.SETUP_LET,
  1109. c: BindingTypes.SETUP_CONST,
  1110. d: BindingTypes.SETUP_MAYBE_REF,
  1111. e: BindingTypes.SETUP_LET,
  1112. foo: BindingTypes.PROPS
  1113. })
  1114. })
  1115. })