compileScript.spec.ts 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  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('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. test('ssr codegen', () => {
  285. const { content } = compile(
  286. `
  287. <script setup>
  288. import { ref } from 'vue'
  289. const count = ref(0)
  290. </script>
  291. <template>
  292. <div>{{ count }}</div>
  293. <div>static</div>
  294. </template>
  295. <style>
  296. div { color: v-bind(count) }
  297. </style>
  298. `,
  299. {
  300. inlineTemplate: true,
  301. templateOptions: {
  302. ssr: true
  303. }
  304. }
  305. )
  306. expect(content).toMatch(`\n __ssrInlineRender: true,\n`)
  307. expect(content).toMatch(`return (_ctx, _push`)
  308. expect(content).toMatch(`ssrInterpolate`)
  309. assertCode(content)
  310. })
  311. })
  312. describe('with TypeScript', () => {
  313. test('hoist type declarations', () => {
  314. const { content } = compile(`
  315. <script setup lang="ts">
  316. export interface Foo {}
  317. type Bar = {}
  318. </script>`)
  319. assertCode(content)
  320. })
  321. test('defineOptions w/ runtime options', () => {
  322. const { content } = compile(`
  323. <script setup lang="ts">
  324. import { defineOptions } from 'vue'
  325. const { props, emit } = defineOptions({
  326. props: { foo: String },
  327. emits: ['a', 'b']
  328. })
  329. </script>
  330. `)
  331. assertCode(content)
  332. expect(content).toMatch(`export default _defineComponent({
  333. expose: [],
  334. props: { foo: String },
  335. emits: ['a', 'b'],
  336. setup(__props, { props, emit }) {`)
  337. })
  338. test('defineOptions w/ type / extract props', () => {
  339. const { content, bindings } = compile(`
  340. <script setup lang="ts">
  341. import { defineOptions } from 'vue'
  342. interface Test {}
  343. type Alias = number[]
  344. defineOptions<{
  345. props: {
  346. string: string
  347. number: number
  348. boolean: boolean
  349. object: object
  350. objectLiteral: { a: number }
  351. fn: (n: number) => void
  352. functionRef: Function
  353. objectRef: Object
  354. array: string[]
  355. arrayRef: Array<any>
  356. tuple: [number, number]
  357. set: Set<string>
  358. literal: 'foo'
  359. optional?: any
  360. recordRef: Record<string, null>
  361. interface: Test
  362. alias: Alias
  363. union: string | number
  364. literalUnion: 'foo' | 'bar'
  365. literalUnionMixed: 'foo' | 1 | boolean
  366. intersection: Test & {}
  367. }
  368. }>()
  369. </script>`)
  370. assertCode(content)
  371. expect(content).toMatch(`string: { type: String, required: true }`)
  372. expect(content).toMatch(`number: { type: Number, required: true }`)
  373. expect(content).toMatch(`boolean: { type: Boolean, required: true }`)
  374. expect(content).toMatch(`object: { type: Object, required: true }`)
  375. expect(content).toMatch(`objectLiteral: { type: Object, required: true }`)
  376. expect(content).toMatch(`fn: { type: Function, required: true }`)
  377. expect(content).toMatch(`functionRef: { type: Function, required: true }`)
  378. expect(content).toMatch(`objectRef: { type: Object, required: true }`)
  379. expect(content).toMatch(`array: { type: Array, required: true }`)
  380. expect(content).toMatch(`arrayRef: { type: Array, required: true }`)
  381. expect(content).toMatch(`tuple: { type: Array, required: true }`)
  382. expect(content).toMatch(`set: { type: Set, required: true }`)
  383. expect(content).toMatch(`literal: { type: String, required: true }`)
  384. expect(content).toMatch(`optional: { type: null, required: false }`)
  385. expect(content).toMatch(`recordRef: { type: Object, required: true }`)
  386. expect(content).toMatch(`interface: { type: Object, required: true }`)
  387. expect(content).toMatch(`alias: { type: Array, required: true }`)
  388. expect(content).toMatch(
  389. `union: { type: [String, Number], required: true }`
  390. )
  391. expect(content).toMatch(
  392. `literalUnion: { type: [String, String], required: true }`
  393. )
  394. expect(content).toMatch(
  395. `literalUnionMixed: { type: [String, Number, Boolean], required: true }`
  396. )
  397. expect(content).toMatch(`intersection: { type: Object, required: true }`)
  398. expect(bindings).toStrictEqual({
  399. string: BindingTypes.PROPS,
  400. number: BindingTypes.PROPS,
  401. boolean: BindingTypes.PROPS,
  402. object: BindingTypes.PROPS,
  403. objectLiteral: BindingTypes.PROPS,
  404. fn: BindingTypes.PROPS,
  405. functionRef: BindingTypes.PROPS,
  406. objectRef: BindingTypes.PROPS,
  407. array: BindingTypes.PROPS,
  408. arrayRef: BindingTypes.PROPS,
  409. tuple: BindingTypes.PROPS,
  410. set: BindingTypes.PROPS,
  411. literal: BindingTypes.PROPS,
  412. optional: BindingTypes.PROPS,
  413. recordRef: BindingTypes.PROPS,
  414. interface: BindingTypes.PROPS,
  415. alias: BindingTypes.PROPS,
  416. union: BindingTypes.PROPS,
  417. literalUnion: BindingTypes.PROPS,
  418. literalUnionMixed: BindingTypes.PROPS,
  419. intersection: BindingTypes.PROPS
  420. })
  421. })
  422. test('defineOptions w/ type / extract emits', () => {
  423. const { content } = compile(`
  424. <script setup lang="ts">
  425. import { defineOptions } from 'vue'
  426. const { emit } = defineOptions<{
  427. emit: (e: 'foo' | 'bar') => void
  428. }>()
  429. </script>
  430. `)
  431. assertCode(content)
  432. expect(content).toMatch(`props: {},\n emit: (e: 'foo' | 'bar') => void,`)
  433. expect(content).toMatch(`emits: ["foo", "bar"] as unknown as undefined`)
  434. })
  435. test('defineOptions w/ type / extract emits (union)', () => {
  436. const { content } = compile(`
  437. <script setup lang="ts">
  438. import { defineOptions } from 'vue'
  439. const { emit } = defineOptions<{
  440. emit: ((e: 'foo' | 'bar') => void) | ((e: 'baz', id: number) => void)
  441. }>()
  442. </script>
  443. `)
  444. assertCode(content)
  445. expect(content).toMatch(
  446. `props: {},\n emit: ((e: 'foo' | 'bar') => void) | ((e: 'baz', id: number) => void),`
  447. )
  448. expect(content).toMatch(
  449. `emits: ["foo", "bar", "baz"] as unknown as undefined`
  450. )
  451. })
  452. })
  453. describe('async/await detection', () => {
  454. function assertAwaitDetection(code: string, shouldAsync = true) {
  455. const { content } = compile(`<script setup>${code}</script>`)
  456. expect(content).toMatch(`${shouldAsync ? `async ` : ``}setup(`)
  457. }
  458. test('expression statement', () => {
  459. assertAwaitDetection(`await foo`)
  460. })
  461. test('variable', () => {
  462. assertAwaitDetection(`const a = 1 + (await foo)`)
  463. })
  464. test('ref', () => {
  465. assertAwaitDetection(`ref: a = 1 + (await foo)`)
  466. })
  467. test('nested statements', () => {
  468. assertAwaitDetection(`if (ok) { await foo } else { await bar }`)
  469. })
  470. test('should ignore await inside functions', () => {
  471. // function declaration
  472. assertAwaitDetection(`async function foo() { await bar }`, false)
  473. // function expression
  474. assertAwaitDetection(`const foo = async () => { await bar }`, false)
  475. // object method
  476. assertAwaitDetection(`const obj = { async method() { await bar }}`, false)
  477. // class method
  478. assertAwaitDetection(
  479. `const cls = class Foo { async method() { await bar }}`,
  480. false
  481. )
  482. })
  483. })
  484. describe('ref: syntax sugar', () => {
  485. test('convert ref declarations', () => {
  486. const { content, bindings } = compile(`<script setup>
  487. ref: foo
  488. ref: a = 1
  489. ref: b = {
  490. count: 0
  491. }
  492. let c = () => {}
  493. let d
  494. </script>`)
  495. expect(content).toMatch(`import { ref as _ref } from 'vue'`)
  496. expect(content).not.toMatch(`ref: foo`)
  497. expect(content).not.toMatch(`ref: a`)
  498. expect(content).not.toMatch(`ref: b`)
  499. expect(content).toMatch(`const foo = _ref()`)
  500. expect(content).toMatch(`const a = _ref(1)`)
  501. expect(content).toMatch(`
  502. const b = _ref({
  503. count: 0
  504. })
  505. `)
  506. // normal declarations left untouched
  507. expect(content).toMatch(`let c = () => {}`)
  508. expect(content).toMatch(`let d`)
  509. assertCode(content)
  510. expect(bindings).toStrictEqual({
  511. foo: BindingTypes.SETUP_REF,
  512. a: BindingTypes.SETUP_REF,
  513. b: BindingTypes.SETUP_REF,
  514. c: BindingTypes.SETUP_LET,
  515. d: BindingTypes.SETUP_LET
  516. })
  517. })
  518. test('multi ref declarations', () => {
  519. const { content, bindings } = compile(`<script setup>
  520. ref: a = 1, b = 2, c = {
  521. count: 0
  522. }
  523. </script>`)
  524. expect(content).toMatch(`
  525. const a = _ref(1), b = _ref(2), c = _ref({
  526. count: 0
  527. })
  528. `)
  529. expect(content).toMatch(`return { a, b, c }`)
  530. assertCode(content)
  531. expect(bindings).toStrictEqual({
  532. a: BindingTypes.SETUP_REF,
  533. b: BindingTypes.SETUP_REF,
  534. c: BindingTypes.SETUP_REF
  535. })
  536. })
  537. test('should not convert non ref labels', () => {
  538. const { content } = compile(`<script setup>
  539. foo: a = 1, b = 2, c = {
  540. count: 0
  541. }
  542. </script>`)
  543. expect(content).toMatch(`foo: a = 1, b = 2`)
  544. assertCode(content)
  545. })
  546. test('accessing ref binding', () => {
  547. const { content } = compile(`<script setup>
  548. ref: a = 1
  549. console.log(a)
  550. function get() {
  551. return a + 1
  552. }
  553. </script>`)
  554. expect(content).toMatch(`console.log(a.value)`)
  555. expect(content).toMatch(`return a.value + 1`)
  556. assertCode(content)
  557. })
  558. test('cases that should not append .value', () => {
  559. const { content } = compile(`<script setup>
  560. ref: a = 1
  561. console.log(b.a)
  562. function get(a) {
  563. return a + 1
  564. }
  565. </script>`)
  566. expect(content).not.toMatch(`a.value`)
  567. })
  568. test('mutating ref binding', () => {
  569. const { content } = compile(`<script setup>
  570. ref: a = 1
  571. ref: b = { count: 0 }
  572. function inc() {
  573. a++
  574. a = a + 1
  575. b.count++
  576. b.count = b.count + 1
  577. ;({ a } = { a: 2 })
  578. ;[a] = [1]
  579. }
  580. </script>`)
  581. expect(content).toMatch(`a.value++`)
  582. expect(content).toMatch(`a.value = a.value + 1`)
  583. expect(content).toMatch(`b.value.count++`)
  584. expect(content).toMatch(`b.value.count = b.value.count + 1`)
  585. expect(content).toMatch(`;({ a: a.value } = { a: 2 })`)
  586. expect(content).toMatch(`;[a.value] = [1]`)
  587. assertCode(content)
  588. })
  589. test('using ref binding in property shorthand', () => {
  590. const { content } = compile(`<script setup>
  591. ref: a = 1
  592. const b = { a }
  593. function test() {
  594. const { a } = b
  595. }
  596. </script>`)
  597. expect(content).toMatch(`const b = { a: a.value }`)
  598. // should not convert destructure
  599. expect(content).toMatch(`const { a } = b`)
  600. assertCode(content)
  601. })
  602. test('object destructure', () => {
  603. const { content, bindings } = compile(`<script setup>
  604. ref: n = 1, ({ a, b: c, d = 1, e: f = 2, ...g } = useFoo())
  605. console.log(n, a, c, d, f, g)
  606. </script>`)
  607. expect(content).toMatch(
  608. `const n = _ref(1), { a: __a, b: __c, d: __d = 1, e: __f = 2, ...__g } = useFoo()`
  609. )
  610. expect(content).toMatch(`\nconst a = _ref(__a);`)
  611. expect(content).not.toMatch(`\nconst b = _ref(__b);`)
  612. expect(content).toMatch(`\nconst c = _ref(__c);`)
  613. expect(content).toMatch(`\nconst d = _ref(__d);`)
  614. expect(content).not.toMatch(`\nconst e = _ref(__e);`)
  615. expect(content).toMatch(`\nconst f = _ref(__f);`)
  616. expect(content).toMatch(`\nconst g = _ref(__g);`)
  617. expect(content).toMatch(
  618. `console.log(n.value, a.value, c.value, d.value, f.value, g.value)`
  619. )
  620. expect(content).toMatch(`return { n, a, c, d, f, g }`)
  621. expect(bindings).toStrictEqual({
  622. n: BindingTypes.SETUP_REF,
  623. a: BindingTypes.SETUP_REF,
  624. c: BindingTypes.SETUP_REF,
  625. d: BindingTypes.SETUP_REF,
  626. f: BindingTypes.SETUP_REF,
  627. g: BindingTypes.SETUP_REF
  628. })
  629. assertCode(content)
  630. })
  631. test('array destructure', () => {
  632. const { content, bindings } = compile(`<script setup>
  633. ref: n = 1, [a, b = 1, ...c] = useFoo()
  634. console.log(n, a, b, c)
  635. </script>`)
  636. expect(content).toMatch(
  637. `const n = _ref(1), [__a, __b = 1, ...__c] = useFoo()`
  638. )
  639. expect(content).toMatch(`\nconst a = _ref(__a);`)
  640. expect(content).toMatch(`\nconst b = _ref(__b);`)
  641. expect(content).toMatch(`\nconst c = _ref(__c);`)
  642. expect(content).toMatch(`console.log(n.value, a.value, b.value, c.value)`)
  643. expect(content).toMatch(`return { n, a, b, c }`)
  644. expect(bindings).toStrictEqual({
  645. n: BindingTypes.SETUP_REF,
  646. a: BindingTypes.SETUP_REF,
  647. b: BindingTypes.SETUP_REF,
  648. c: BindingTypes.SETUP_REF
  649. })
  650. assertCode(content)
  651. })
  652. test('nested destructure', () => {
  653. const { content, bindings } = compile(`<script setup>
  654. ref: [{ a: { b }}] = useFoo()
  655. ref: ({ c: [d, e] } = useBar())
  656. console.log(b, d, e)
  657. </script>`)
  658. expect(content).toMatch(`const [{ a: { b: __b }}] = useFoo()`)
  659. expect(content).toMatch(`const { c: [__d, __e] } = useBar()`)
  660. expect(content).not.toMatch(`\nconst a = _ref(__a);`)
  661. expect(content).not.toMatch(`\nconst c = _ref(__c);`)
  662. expect(content).toMatch(`\nconst b = _ref(__b);`)
  663. expect(content).toMatch(`\nconst d = _ref(__d);`)
  664. expect(content).toMatch(`\nconst e = _ref(__e);`)
  665. expect(content).toMatch(`return { b, d, e }`)
  666. expect(bindings).toStrictEqual({
  667. b: BindingTypes.SETUP_REF,
  668. d: BindingTypes.SETUP_REF,
  669. e: BindingTypes.SETUP_REF
  670. })
  671. assertCode(content)
  672. })
  673. })
  674. describe('errors', () => {
  675. test('<script> and <script setup> must have same lang', () => {
  676. expect(() =>
  677. compile(`<script>foo()</script><script setup lang="ts">bar()</script>`)
  678. ).toThrow(`<script> and <script setup> must have the same language type`)
  679. })
  680. const moduleErrorMsg = `cannot contain ES module exports`
  681. test('non-type named exports', () => {
  682. expect(() =>
  683. compile(`<script setup>
  684. export const a = 1
  685. </script>`)
  686. ).toThrow(moduleErrorMsg)
  687. expect(() =>
  688. compile(`<script setup>
  689. export * from './foo'
  690. </script>`)
  691. ).toThrow(moduleErrorMsg)
  692. expect(() =>
  693. compile(`<script setup>
  694. const bar = 1
  695. export { bar as default }
  696. </script>`)
  697. ).toThrow(moduleErrorMsg)
  698. })
  699. test('ref: non-assignment expressions', () => {
  700. expect(() =>
  701. compile(`<script setup>
  702. ref: a = 1, foo()
  703. </script>`)
  704. ).toThrow(`ref: statements can only contain assignment expressions`)
  705. })
  706. test('defineOptions() w/ both type and non-type args', () => {
  707. expect(() => {
  708. compile(`<script setup lang="ts">
  709. import { defineOptions } from 'vue'
  710. defineOptions<{}>({})
  711. </script>`)
  712. }).toThrow(`cannot accept both type and non-type arguments`)
  713. })
  714. test('defineOptions() referencing local var', () => {
  715. expect(() =>
  716. compile(`<script setup>
  717. import { defineOptions } from 'vue'
  718. const bar = 1
  719. defineOptions({
  720. props: {
  721. foo: {
  722. default: () => bar
  723. }
  724. }
  725. })
  726. </script>`)
  727. ).toThrow(`cannot reference locally declared variables`)
  728. })
  729. test('defineOptions() referencing ref declarations', () => {
  730. expect(() =>
  731. compile(`<script setup>
  732. import { defineOptions } from 'vue'
  733. ref: bar = 1
  734. defineOptions({
  735. props: { bar }
  736. })
  737. </script>`)
  738. ).toThrow(`cannot reference locally declared variables`)
  739. })
  740. test('should allow defineOptions() referencing scope var', () => {
  741. assertCode(
  742. compile(`<script setup>
  743. import { defineOptions } from 'vue'
  744. const bar = 1
  745. defineOptions({
  746. props: {
  747. foo: {
  748. default: bar => bar + 1
  749. }
  750. }
  751. })
  752. </script>`).content
  753. )
  754. })
  755. test('should allow defineOptions() referencing imported binding', () => {
  756. assertCode(
  757. compile(`<script setup>
  758. import { defineOptions } from 'vue'
  759. import { bar } from './bar'
  760. defineOptions({
  761. props: {
  762. foo: {
  763. default: () => bar
  764. }
  765. }
  766. })
  767. </script>`).content
  768. )
  769. })
  770. })
  771. })
  772. describe('SFC analyze <script> bindings', () => {
  773. it('can parse decorators syntax in typescript block', () => {
  774. const { scriptAst } = compile(`
  775. <script lang="ts">
  776. import { Options, Vue } from 'vue-class-component';
  777. @Options({
  778. components: {
  779. HelloWorld,
  780. },
  781. props: ['foo', 'bar']
  782. })
  783. export default class Home extends Vue {}
  784. </script>
  785. `)
  786. expect(scriptAst).toBeDefined()
  787. })
  788. it('recognizes props array declaration', () => {
  789. const { bindings } = compile(`
  790. <script>
  791. export default {
  792. props: ['foo', 'bar']
  793. }
  794. </script>
  795. `)
  796. expect(bindings).toStrictEqual({
  797. foo: BindingTypes.PROPS,
  798. bar: BindingTypes.PROPS
  799. })
  800. })
  801. it('recognizes props object declaration', () => {
  802. const { bindings } = compile(`
  803. <script>
  804. export default {
  805. props: {
  806. foo: String,
  807. bar: {
  808. type: String,
  809. },
  810. baz: null,
  811. qux: [String, Number]
  812. }
  813. }
  814. </script>
  815. `)
  816. expect(bindings).toStrictEqual({
  817. foo: BindingTypes.PROPS,
  818. bar: BindingTypes.PROPS,
  819. baz: BindingTypes.PROPS,
  820. qux: BindingTypes.PROPS
  821. })
  822. })
  823. it('recognizes setup return', () => {
  824. const { bindings } = compile(`
  825. <script>
  826. const bar = 2
  827. export default {
  828. setup() {
  829. return {
  830. foo: 1,
  831. bar
  832. }
  833. }
  834. }
  835. </script>
  836. `)
  837. expect(bindings).toStrictEqual({
  838. foo: BindingTypes.SETUP_MAYBE_REF,
  839. bar: BindingTypes.SETUP_MAYBE_REF
  840. })
  841. })
  842. it('recognizes async setup return', () => {
  843. const { bindings } = compile(`
  844. <script>
  845. const bar = 2
  846. export default {
  847. async setup() {
  848. return {
  849. foo: 1,
  850. bar
  851. }
  852. }
  853. }
  854. </script>
  855. `)
  856. expect(bindings).toStrictEqual({
  857. foo: BindingTypes.SETUP_MAYBE_REF,
  858. bar: BindingTypes.SETUP_MAYBE_REF
  859. })
  860. })
  861. it('recognizes data return', () => {
  862. const { bindings } = compile(`
  863. <script>
  864. const bar = 2
  865. export default {
  866. data() {
  867. return {
  868. foo: null,
  869. bar
  870. }
  871. }
  872. }
  873. </script>
  874. `)
  875. expect(bindings).toStrictEqual({
  876. foo: BindingTypes.DATA,
  877. bar: BindingTypes.DATA
  878. })
  879. })
  880. it('recognizes methods', () => {
  881. const { bindings } = compile(`
  882. <script>
  883. export default {
  884. methods: {
  885. foo() {}
  886. }
  887. }
  888. </script>
  889. `)
  890. expect(bindings).toStrictEqual({ foo: BindingTypes.OPTIONS })
  891. })
  892. it('recognizes computeds', () => {
  893. const { bindings } = compile(`
  894. <script>
  895. export default {
  896. computed: {
  897. foo() {},
  898. bar: {
  899. get() {},
  900. set() {},
  901. }
  902. }
  903. }
  904. </script>
  905. `)
  906. expect(bindings).toStrictEqual({
  907. foo: BindingTypes.OPTIONS,
  908. bar: BindingTypes.OPTIONS
  909. })
  910. })
  911. it('recognizes injections array declaration', () => {
  912. const { bindings } = compile(`
  913. <script>
  914. export default {
  915. inject: ['foo', 'bar']
  916. }
  917. </script>
  918. `)
  919. expect(bindings).toStrictEqual({
  920. foo: BindingTypes.OPTIONS,
  921. bar: BindingTypes.OPTIONS
  922. })
  923. })
  924. it('recognizes injections object declaration', () => {
  925. const { bindings } = compile(`
  926. <script>
  927. export default {
  928. inject: {
  929. foo: {},
  930. bar: {},
  931. }
  932. }
  933. </script>
  934. `)
  935. expect(bindings).toStrictEqual({
  936. foo: BindingTypes.OPTIONS,
  937. bar: BindingTypes.OPTIONS
  938. })
  939. })
  940. it('works for mixed bindings', () => {
  941. const { bindings } = compile(`
  942. <script>
  943. export default {
  944. inject: ['foo'],
  945. props: {
  946. bar: String,
  947. },
  948. setup() {
  949. return {
  950. baz: null,
  951. }
  952. },
  953. data() {
  954. return {
  955. qux: null
  956. }
  957. },
  958. methods: {
  959. quux() {}
  960. },
  961. computed: {
  962. quuz() {}
  963. }
  964. }
  965. </script>
  966. `)
  967. expect(bindings).toStrictEqual({
  968. foo: BindingTypes.OPTIONS,
  969. bar: BindingTypes.PROPS,
  970. baz: BindingTypes.SETUP_MAYBE_REF,
  971. qux: BindingTypes.DATA,
  972. quux: BindingTypes.OPTIONS,
  973. quuz: BindingTypes.OPTIONS
  974. })
  975. })
  976. it('works for script setup', () => {
  977. const { bindings } = compile(`
  978. <script setup>
  979. import { defineOptions, ref as r } from 'vue'
  980. defineOptions({
  981. props: {
  982. foo: String,
  983. }
  984. })
  985. const a = r(1)
  986. let b = 2
  987. const c = 3
  988. const { d } = someFoo()
  989. let { e } = someBar()
  990. </script>
  991. `)
  992. expect(bindings).toStrictEqual({
  993. r: BindingTypes.SETUP_CONST,
  994. a: BindingTypes.SETUP_REF,
  995. b: BindingTypes.SETUP_LET,
  996. c: BindingTypes.SETUP_CONST,
  997. d: BindingTypes.SETUP_MAYBE_REF,
  998. e: BindingTypes.SETUP_LET,
  999. foo: BindingTypes.PROPS
  1000. })
  1001. })
  1002. })