compileScriptRefSugar.spec.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. import { BindingTypes } from '@vue/compiler-core'
  2. import { compileSFCScript as compile, assertCode } from './utils'
  3. describe('<script setup> ref sugar', () => {
  4. function compileWithRefSugar(src: string) {
  5. return compile(src, { refSugar: true })
  6. }
  7. test('$ref & $shallowRef declarations', () => {
  8. const { content, bindings } = compileWithRefSugar(`<script setup>
  9. let foo = $ref()
  10. let a = $ref(1)
  11. let b = $shallowRef({
  12. count: 0
  13. })
  14. let c = () => {}
  15. let d
  16. </script>`)
  17. expect(content).toMatch(
  18. `import { ref as _ref, shallowRef as _shallowRef } from 'vue'`
  19. )
  20. expect(content).not.toMatch(`$ref()`)
  21. expect(content).not.toMatch(`$ref(1)`)
  22. expect(content).not.toMatch(`$shallowRef({`)
  23. expect(content).toMatch(`let foo = _ref()`)
  24. expect(content).toMatch(`let a = _ref(1)`)
  25. expect(content).toMatch(`
  26. let b = _shallowRef({
  27. count: 0
  28. })
  29. `)
  30. // normal declarations left untouched
  31. expect(content).toMatch(`let c = () => {}`)
  32. expect(content).toMatch(`let d`)
  33. assertCode(content)
  34. expect(bindings).toStrictEqual({
  35. foo: BindingTypes.SETUP_REF,
  36. a: BindingTypes.SETUP_REF,
  37. b: BindingTypes.SETUP_REF,
  38. c: BindingTypes.SETUP_LET,
  39. d: BindingTypes.SETUP_LET
  40. })
  41. })
  42. test('multi $ref declarations', () => {
  43. const { content, bindings } = compileWithRefSugar(`<script setup>
  44. let a = $ref(1), b = $ref(2), c = $ref({
  45. count: 0
  46. })
  47. </script>`)
  48. expect(content).toMatch(`
  49. let a = _ref(1), b = _ref(2), c = _ref({
  50. count: 0
  51. })
  52. `)
  53. expect(content).toMatch(`return { a, b, c }`)
  54. assertCode(content)
  55. expect(bindings).toStrictEqual({
  56. a: BindingTypes.SETUP_REF,
  57. b: BindingTypes.SETUP_REF,
  58. c: BindingTypes.SETUP_REF
  59. })
  60. })
  61. test('$computed declaration', () => {
  62. const { content, bindings } = compileWithRefSugar(`<script setup>
  63. const a = $computed(() => 1)
  64. </script>`)
  65. expect(content).toMatch(`
  66. const a = _computed(() => 1)
  67. `)
  68. expect(content).toMatch(`return { a }`)
  69. assertCode(content)
  70. expect(bindings).toStrictEqual({
  71. a: BindingTypes.SETUP_REF
  72. })
  73. })
  74. test('mixing $ref & $computed declarations', () => {
  75. const { content, bindings } = compileWithRefSugar(`<script setup>
  76. let a = $ref(1), b = $computed(() => a + 1)
  77. </script>`)
  78. expect(content).toMatch(`
  79. let a = _ref(1), b = _computed(() => a.value + 1)
  80. `)
  81. expect(content).toMatch(`return { a, b }`)
  82. assertCode(content)
  83. expect(bindings).toStrictEqual({
  84. a: BindingTypes.SETUP_REF,
  85. b: BindingTypes.SETUP_REF
  86. })
  87. })
  88. test('accessing ref binding', () => {
  89. const { content } = compileWithRefSugar(`<script setup>
  90. let a = $ref(1)
  91. console.log(a)
  92. function get() {
  93. return a + 1
  94. }
  95. </script>`)
  96. expect(content).toMatch(`console.log(a.value)`)
  97. expect(content).toMatch(`return a.value + 1`)
  98. assertCode(content)
  99. })
  100. test('cases that should not append .value', () => {
  101. const { content } = compileWithRefSugar(`<script setup>
  102. let a = $ref(1)
  103. console.log(b.a)
  104. function get(a) {
  105. return a + 1
  106. }
  107. </script>`)
  108. expect(content).not.toMatch(`a.value`)
  109. })
  110. test('mutating ref binding', () => {
  111. const { content } = compileWithRefSugar(`<script setup>
  112. let a = $ref(1)
  113. let b = $ref({ count: 0 })
  114. function inc() {
  115. a++
  116. a = a + 1
  117. b.count++
  118. b.count = b.count + 1
  119. ;({ a } = { a: 2 })
  120. ;[a] = [1]
  121. }
  122. </script>`)
  123. expect(content).toMatch(`a.value++`)
  124. expect(content).toMatch(`a.value = a.value + 1`)
  125. expect(content).toMatch(`b.value.count++`)
  126. expect(content).toMatch(`b.value.count = b.value.count + 1`)
  127. expect(content).toMatch(`;({ a: a.value } = { a: 2 })`)
  128. expect(content).toMatch(`;[a.value] = [1]`)
  129. assertCode(content)
  130. })
  131. test('using ref binding in property shorthand', () => {
  132. const { content } = compileWithRefSugar(`<script setup>
  133. let a = $ref(1)
  134. const b = { a }
  135. function test() {
  136. const { a } = b
  137. }
  138. </script>`)
  139. expect(content).toMatch(`const b = { a: a.value }`)
  140. // should not convert destructure
  141. expect(content).toMatch(`const { a } = b`)
  142. assertCode(content)
  143. })
  144. test('should not rewrite scope variable', () => {
  145. const { content } = compileWithRefSugar(`
  146. <script setup>
  147. let a = $ref(1)
  148. let b = $ref(1)
  149. let d = $ref(1)
  150. const e = 1
  151. function test() {
  152. const a = 2
  153. console.log(a)
  154. console.log(b)
  155. let c = { c: 3 }
  156. console.log(c)
  157. console.log(d)
  158. console.log(e)
  159. }
  160. </script>`)
  161. expect(content).toMatch('console.log(a)')
  162. expect(content).toMatch('console.log(b.value)')
  163. expect(content).toMatch('console.log(c)')
  164. expect(content).toMatch('console.log(d.value)')
  165. expect(content).toMatch('console.log(e)')
  166. assertCode(content)
  167. })
  168. test('object destructure', () => {
  169. const { content, bindings } = compileWithRefSugar(`<script setup>
  170. let n = $ref(1), { a, b: c, d = 1, e: f = 2, ...g } = $fromRefs(useFoo())
  171. let { foo } = $fromRefs(useSomthing(() => 1));
  172. console.log(n, a, c, d, f, g, foo)
  173. </script>`)
  174. expect(content).toMatch(
  175. `let n = _ref(1), { a: __a, b: __c, d: __d = 1, e: __f = 2, ...__g } = (useFoo())`
  176. )
  177. expect(content).toMatch(`let { foo: __foo } = (useSomthing(() => 1))`)
  178. expect(content).toMatch(`\nconst a = _shallowRef(__a);`)
  179. expect(content).not.toMatch(`\nconst b = _shallowRef(__b);`)
  180. expect(content).toMatch(`\nconst c = _shallowRef(__c);`)
  181. expect(content).toMatch(`\nconst d = _shallowRef(__d);`)
  182. expect(content).not.toMatch(`\nconst e = _shallowRef(__e);`)
  183. expect(content).toMatch(`\nconst f = _shallowRef(__f);`)
  184. expect(content).toMatch(`\nconst g = _shallowRef(__g);`)
  185. expect(content).toMatch(`\nconst foo = _shallowRef(__foo);`)
  186. expect(content).toMatch(
  187. `console.log(n.value, a.value, c.value, d.value, f.value, g.value, foo.value)`
  188. )
  189. expect(content).toMatch(`return { n, a, c, d, f, g, foo }`)
  190. expect(bindings).toStrictEqual({
  191. n: BindingTypes.SETUP_REF,
  192. a: BindingTypes.SETUP_REF,
  193. c: BindingTypes.SETUP_REF,
  194. d: BindingTypes.SETUP_REF,
  195. f: BindingTypes.SETUP_REF,
  196. g: BindingTypes.SETUP_REF,
  197. foo: BindingTypes.SETUP_REF
  198. })
  199. assertCode(content)
  200. })
  201. test('array destructure', () => {
  202. const { content, bindings } = compileWithRefSugar(`<script setup>
  203. let n = $ref(1), [a, b = 1, ...c] = $fromRefs(useFoo())
  204. console.log(n, a, b, c)
  205. </script>`)
  206. expect(content).toMatch(
  207. `let n = _ref(1), [__a, __b = 1, ...__c] = (useFoo())`
  208. )
  209. expect(content).toMatch(`\nconst a = _shallowRef(__a);`)
  210. expect(content).toMatch(`\nconst b = _shallowRef(__b);`)
  211. expect(content).toMatch(`\nconst c = _shallowRef(__c);`)
  212. expect(content).toMatch(`console.log(n.value, a.value, b.value, c.value)`)
  213. expect(content).toMatch(`return { n, a, b, c }`)
  214. expect(bindings).toStrictEqual({
  215. n: BindingTypes.SETUP_REF,
  216. a: BindingTypes.SETUP_REF,
  217. b: BindingTypes.SETUP_REF,
  218. c: BindingTypes.SETUP_REF
  219. })
  220. assertCode(content)
  221. })
  222. test('nested destructure', () => {
  223. const { content, bindings } = compileWithRefSugar(`<script setup>
  224. let [{ a: { b }}] = $fromRefs(useFoo())
  225. let { c: [d, e] } = $fromRefs(useBar())
  226. console.log(b, d, e)
  227. </script>`)
  228. expect(content).toMatch(`let [{ a: { b: __b }}] = (useFoo())`)
  229. expect(content).toMatch(`let { c: [__d, __e] } = (useBar())`)
  230. expect(content).not.toMatch(`\nconst a = _shallowRef(__a);`)
  231. expect(content).not.toMatch(`\nconst c = _shallowRef(__c);`)
  232. expect(content).toMatch(`\nconst b = _shallowRef(__b);`)
  233. expect(content).toMatch(`\nconst d = _shallowRef(__d);`)
  234. expect(content).toMatch(`\nconst e = _shallowRef(__e);`)
  235. expect(content).toMatch(`return { b, d, e }`)
  236. expect(bindings).toStrictEqual({
  237. b: BindingTypes.SETUP_REF,
  238. d: BindingTypes.SETUP_REF,
  239. e: BindingTypes.SETUP_REF
  240. })
  241. assertCode(content)
  242. })
  243. test('$raw', () => {
  244. const { content } = compileWithRefSugar(`<script setup>
  245. let a = $ref(1)
  246. const b = $raw(a)
  247. const c = $raw({ a })
  248. callExternal($raw(a))
  249. </script>`)
  250. expect(content).toMatch(`const b = (a)`)
  251. expect(content).toMatch(`const c = ({ a })`)
  252. expect(content).toMatch(`callExternal((a))`)
  253. assertCode(content)
  254. })
  255. //#4062
  256. test('should not rewrite type identifiers', () => {
  257. const { content } = compile(
  258. `
  259. <script setup lang="ts">
  260. const props = defineProps<{msg: string; ids?: string[]}>()
  261. let ids = $ref([])
  262. </script>`,
  263. {
  264. refSugar: true
  265. }
  266. )
  267. assertCode(content)
  268. expect(content).not.toMatch('.value')
  269. })
  270. // #4254
  271. test('handle TS casting syntax', () => {
  272. const { content } = compile(
  273. `
  274. <script setup lang="ts">
  275. let a = $ref(1)
  276. console.log(a!)
  277. console.log(a! + 1)
  278. console.log(a as number)
  279. console.log((a as number) + 1)
  280. console.log(<number>a)
  281. console.log(<number>a + 1)
  282. console.log(a! + (a as number))
  283. console.log(a! + <number>a)
  284. console.log((a as number) + <number>a)
  285. </script>`,
  286. {
  287. refSugar: true
  288. }
  289. )
  290. assertCode(content)
  291. expect(content).toMatch('console.log(a.value!)')
  292. expect(content).toMatch('console.log(a.value as number)')
  293. expect(content).toMatch('console.log(<number>a.value)')
  294. })
  295. describe('errors', () => {
  296. test('non-let $ref declaration', () => {
  297. expect(() =>
  298. compile(
  299. `<script setup>
  300. const a = $ref(1)
  301. </script>`,
  302. { refSugar: true }
  303. )
  304. ).toThrow(`$ref() bindings can only be declared with let`)
  305. })
  306. test('$ref w/ destructure', () => {
  307. expect(() =>
  308. compile(
  309. `<script setup>
  310. let { a } = $ref(1)
  311. </script>`,
  312. { refSugar: true }
  313. )
  314. ).toThrow(`$ref() bindings cannot be used with destructuring`)
  315. })
  316. test('$computed w/ destructure', () => {
  317. expect(() =>
  318. compile(
  319. `<script setup>
  320. const { a } = $computed(() => 1)
  321. </script>`,
  322. { refSugar: true }
  323. )
  324. ).toThrow(`$computed() bindings cannot be used with destructuring`)
  325. })
  326. test('defineProps/Emit() referencing ref declarations', () => {
  327. expect(() =>
  328. compile(
  329. `<script setup>
  330. let bar = $ref(1)
  331. defineProps({
  332. bar
  333. })
  334. </script>`,
  335. { refSugar: true }
  336. )
  337. ).toThrow(`cannot reference locally declared variables`)
  338. expect(() =>
  339. compile(
  340. `<script setup>
  341. let bar = $ref(1)
  342. defineEmits({
  343. bar
  344. })
  345. </script>`,
  346. { refSugar: true }
  347. )
  348. ).toThrow(`cannot reference locally declared variables`)
  349. })
  350. test('warn usage in non-init positions', () => {
  351. expect(() =>
  352. compile(
  353. `<script setup>
  354. let bar = $ref(1)
  355. bar = $ref(2)
  356. </script>`,
  357. { refSugar: true }
  358. )
  359. ).toThrow(`$ref can only be used directly as a variable initializer`)
  360. expect(() =>
  361. compile(
  362. `<script setup>
  363. let bar = { foo: $computed(1) }
  364. </script>`,
  365. { refSugar: true }
  366. )
  367. ).toThrow(`$computed can only be used directly as a variable initializer`)
  368. })
  369. })
  370. })