compileScript.spec.ts 32 KB

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