compileScriptRefSugar.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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('convert ref declarations', () => {
  8. const { content, bindings } = compileWithRefSugar(`<script setup>
  9. ref: foo
  10. ref: a = 1
  11. ref: b = {
  12. count: 0
  13. }
  14. let c = () => {}
  15. let d
  16. </script>`)
  17. expect(content).toMatch(`import { ref as _ref } from 'vue'`)
  18. expect(content).not.toMatch(`ref: foo`)
  19. expect(content).not.toMatch(`ref: a`)
  20. expect(content).not.toMatch(`ref: b`)
  21. expect(content).toMatch(`const foo = _ref()`)
  22. expect(content).toMatch(`const a = _ref(1)`)
  23. expect(content).toMatch(`
  24. const b = _ref({
  25. count: 0
  26. })
  27. `)
  28. // normal declarations left untouched
  29. expect(content).toMatch(`let c = () => {}`)
  30. expect(content).toMatch(`let d`)
  31. assertCode(content)
  32. expect(bindings).toStrictEqual({
  33. foo: BindingTypes.SETUP_REF,
  34. a: BindingTypes.SETUP_REF,
  35. b: BindingTypes.SETUP_REF,
  36. c: BindingTypes.SETUP_LET,
  37. d: BindingTypes.SETUP_LET
  38. })
  39. })
  40. test('multi ref declarations', () => {
  41. const { content, bindings } = compileWithRefSugar(`<script setup>
  42. ref: a = 1, b = 2, c = {
  43. count: 0
  44. }
  45. </script>`)
  46. expect(content).toMatch(`
  47. const a = _ref(1), b = _ref(2), c = _ref({
  48. count: 0
  49. })
  50. `)
  51. expect(content).toMatch(`return { a, b, c }`)
  52. assertCode(content)
  53. expect(bindings).toStrictEqual({
  54. a: BindingTypes.SETUP_REF,
  55. b: BindingTypes.SETUP_REF,
  56. c: BindingTypes.SETUP_REF
  57. })
  58. })
  59. test('should not convert non ref labels', () => {
  60. const { content } = compileWithRefSugar(`<script setup>
  61. foo: a = 1, b = 2, c = {
  62. count: 0
  63. }
  64. </script>`)
  65. expect(content).toMatch(`foo: a = 1, b = 2`)
  66. assertCode(content)
  67. })
  68. test('accessing ref binding', () => {
  69. const { content } = compileWithRefSugar(`<script setup>
  70. ref: a = 1
  71. console.log(a)
  72. function get() {
  73. return a + 1
  74. }
  75. </script>`)
  76. expect(content).toMatch(`console.log(a.value)`)
  77. expect(content).toMatch(`return a.value + 1`)
  78. assertCode(content)
  79. })
  80. test('cases that should not append .value', () => {
  81. const { content } = compileWithRefSugar(`<script setup>
  82. ref: a = 1
  83. console.log(b.a)
  84. function get(a) {
  85. return a + 1
  86. }
  87. </script>`)
  88. expect(content).not.toMatch(`a.value`)
  89. })
  90. test('mutating ref binding', () => {
  91. const { content } = compileWithRefSugar(`<script setup>
  92. ref: a = 1
  93. ref: b = { count: 0 }
  94. function inc() {
  95. a++
  96. a = a + 1
  97. b.count++
  98. b.count = b.count + 1
  99. ;({ a } = { a: 2 })
  100. ;[a] = [1]
  101. }
  102. </script>`)
  103. expect(content).toMatch(`a.value++`)
  104. expect(content).toMatch(`a.value = a.value + 1`)
  105. expect(content).toMatch(`b.value.count++`)
  106. expect(content).toMatch(`b.value.count = b.value.count + 1`)
  107. expect(content).toMatch(`;({ a: a.value } = { a: 2 })`)
  108. expect(content).toMatch(`;[a.value] = [1]`)
  109. assertCode(content)
  110. })
  111. test('using ref binding in property shorthand', () => {
  112. const { content } = compileWithRefSugar(`<script setup>
  113. ref: a = 1
  114. const b = { a }
  115. function test() {
  116. const { a } = b
  117. }
  118. </script>`)
  119. expect(content).toMatch(`const b = { a: a.value }`)
  120. // should not convert destructure
  121. expect(content).toMatch(`const { a } = b`)
  122. assertCode(content)
  123. })
  124. test('should not rewrite scope variable', () => {
  125. const { content } = compileWithRefSugar(`
  126. <script setup>
  127. ref: a = 1
  128. ref: b = 1
  129. ref: d = 1
  130. const e = 1
  131. function test() {
  132. const a = 2
  133. console.log(a)
  134. console.log(b)
  135. let c = { c: 3 }
  136. console.log(c)
  137. let $d
  138. console.log($d)
  139. console.log(d)
  140. console.log(e)
  141. }
  142. </script>`)
  143. expect(content).toMatch('console.log(a)')
  144. expect(content).toMatch('console.log(b.value)')
  145. expect(content).toMatch('console.log(c)')
  146. expect(content).toMatch('console.log($d)')
  147. expect(content).toMatch('console.log(d.value)')
  148. expect(content).toMatch('console.log(e)')
  149. assertCode(content)
  150. })
  151. test('object destructure', () => {
  152. const { content, bindings } = compileWithRefSugar(`<script setup>
  153. ref: n = 1, ({ a, b: c, d = 1, e: f = 2, ...g } = useFoo())
  154. ref: ({ foo } = useSomthing(() => 1));
  155. console.log(n, a, c, d, f, g, foo)
  156. </script>`)
  157. expect(content).toMatch(
  158. `const n = _ref(1), { a: __a, b: __c, d: __d = 1, e: __f = 2, ...__g } = useFoo()`
  159. )
  160. expect(content).toMatch(`const { foo: __foo } = useSomthing(() => 1)`)
  161. expect(content).toMatch(`\nconst a = _ref(__a);`)
  162. expect(content).not.toMatch(`\nconst b = _ref(__b);`)
  163. expect(content).toMatch(`\nconst c = _ref(__c);`)
  164. expect(content).toMatch(`\nconst d = _ref(__d);`)
  165. expect(content).not.toMatch(`\nconst e = _ref(__e);`)
  166. expect(content).toMatch(`\nconst f = _ref(__f);`)
  167. expect(content).toMatch(`\nconst g = _ref(__g);`)
  168. expect(content).toMatch(`\nconst foo = _ref(__foo);`)
  169. expect(content).toMatch(
  170. `console.log(n.value, a.value, c.value, d.value, f.value, g.value, foo.value)`
  171. )
  172. expect(content).toMatch(`return { n, a, c, d, f, g, foo }`)
  173. expect(bindings).toStrictEqual({
  174. n: BindingTypes.SETUP_REF,
  175. a: BindingTypes.SETUP_REF,
  176. c: BindingTypes.SETUP_REF,
  177. d: BindingTypes.SETUP_REF,
  178. f: BindingTypes.SETUP_REF,
  179. g: BindingTypes.SETUP_REF,
  180. foo: BindingTypes.SETUP_REF
  181. })
  182. assertCode(content)
  183. })
  184. test('array destructure', () => {
  185. const { content, bindings } = compileWithRefSugar(`<script setup>
  186. ref: n = 1, [a, b = 1, ...c] = useFoo()
  187. console.log(n, a, b, c)
  188. </script>`)
  189. expect(content).toMatch(
  190. `const n = _ref(1), [__a, __b = 1, ...__c] = useFoo()`
  191. )
  192. expect(content).toMatch(`\nconst a = _ref(__a);`)
  193. expect(content).toMatch(`\nconst b = _ref(__b);`)
  194. expect(content).toMatch(`\nconst c = _ref(__c);`)
  195. expect(content).toMatch(`console.log(n.value, a.value, b.value, c.value)`)
  196. expect(content).toMatch(`return { n, a, b, c }`)
  197. expect(bindings).toStrictEqual({
  198. n: BindingTypes.SETUP_REF,
  199. a: BindingTypes.SETUP_REF,
  200. b: BindingTypes.SETUP_REF,
  201. c: BindingTypes.SETUP_REF
  202. })
  203. assertCode(content)
  204. })
  205. test('nested destructure', () => {
  206. const { content, bindings } = compileWithRefSugar(`<script setup>
  207. ref: [{ a: { b }}] = useFoo()
  208. ref: ({ c: [d, e] } = useBar())
  209. console.log(b, d, e)
  210. </script>`)
  211. expect(content).toMatch(`const [{ a: { b: __b }}] = useFoo()`)
  212. expect(content).toMatch(`const { c: [__d, __e] } = useBar()`)
  213. expect(content).not.toMatch(`\nconst a = _ref(__a);`)
  214. expect(content).not.toMatch(`\nconst c = _ref(__c);`)
  215. expect(content).toMatch(`\nconst b = _ref(__b);`)
  216. expect(content).toMatch(`\nconst d = _ref(__d);`)
  217. expect(content).toMatch(`\nconst e = _ref(__e);`)
  218. expect(content).toMatch(`return { b, d, e }`)
  219. expect(bindings).toStrictEqual({
  220. b: BindingTypes.SETUP_REF,
  221. d: BindingTypes.SETUP_REF,
  222. e: BindingTypes.SETUP_REF
  223. })
  224. assertCode(content)
  225. })
  226. describe('errors', () => {
  227. test('ref: non-assignment expressions', () => {
  228. expect(() =>
  229. compile(
  230. `<script setup>
  231. ref: a = 1, foo()
  232. </script>`,
  233. { refSugar: true }
  234. )
  235. ).toThrow(`ref: statements can only contain assignment expressions`)
  236. })
  237. test('defineProps/Emit() referencing ref declarations', () => {
  238. expect(() =>
  239. compile(
  240. `<script setup>
  241. ref: bar = 1
  242. defineProps({
  243. bar
  244. })
  245. </script>`,
  246. { refSugar: true }
  247. )
  248. ).toThrow(`cannot reference locally declared variables`)
  249. expect(() =>
  250. compile(
  251. `<script setup>
  252. ref: bar = 1
  253. defineEmits({
  254. bar
  255. })
  256. </script>`,
  257. { refSugar: true }
  258. )
  259. ).toThrow(`cannot reference locally declared variables`)
  260. })
  261. })
  262. })