compileScript.spec.ts 29 KB

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