compileScript.spec.ts 32 KB

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