feat(experimental): standalone ref transform

This commit is contained in:
Evan You
2021-08-22 22:21:42 -04:00
parent 84b24a71f0
commit db8dc753c0
16 changed files with 1258 additions and 873 deletions

View File

@@ -0,0 +1,211 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`$ unwrapping 1`] = `
"
import { ref, shallowRef } from 'vue'
let foo = (ref())
let a = (ref(1))
let b = (shallowRef({
count: 0
}))
let c = () => {}
let d
"
`;
exports[`$$ 1`] = `
"import { ref as _ref } from 'vue'
let a = _ref(1)
const b = (a)
const c = ({ a })
callExternal((a))
"
`;
exports[`$computed declaration 1`] = `
"import { computed as _computed } from 'vue'
let a = _computed(() => 1)
"
`;
exports[`$ref & $shallowRef declarations 1`] = `
"import { ref as _ref, shallowRef as _shallowRef } from 'vue'
let foo = _ref()
let a = _ref(1)
let b = _shallowRef({
count: 0
})
let c = () => {}
let d
"
`;
exports[`accessing ref binding 1`] = `
"import { ref as _ref } from 'vue'
let a = _ref(1)
console.log(a.value)
function get() {
return a.value + 1
}
"
`;
exports[`array destructure 1`] = `
"import { ref as _ref } from 'vue'
let n = _ref(1), [__a, __b = 1, ...__c] = (useFoo())
const a = _ref(__a);
const b = _ref(__b);
const c = _ref(__c);
console.log(n.value, a.value, b.value, c.value)
"
`;
exports[`handle TS casting syntax 1`] = `
"import { ref as _ref } from 'vue'
let a = _ref(1)
console.log(a.value!)
console.log(a.value! + 1)
console.log(a.value as number)
console.log((a.value as number) + 1)
console.log(<number>a.value)
console.log(<number>a.value + 1)
console.log(a.value! + (a.value as number))
console.log(a.value! + <number>a.value)
console.log((a.value as number) + <number>a.value)
"
`;
exports[`mixing $ref & $computed declarations 1`] = `
"import { ref as _ref, computed as _computed } from 'vue'
let a = _ref(1), b = _computed(() => a.value + 1)
"
`;
exports[`multi $ref declarations 1`] = `
"import { ref as _ref } from 'vue'
let a = _ref(1), b = _ref(2), c = _ref({
count: 0
})
"
`;
exports[`mutating ref binding 1`] = `
"import { ref as _ref } from 'vue'
let a = _ref(1)
let b = _ref({ count: 0 })
function inc() {
a.value++
a.value = a.value + 1
b.value.count++
b.value.count = b.value.count + 1
;({ a: a.value } = { a: 2 })
;[a.value] = [1]
}
"
`;
exports[`nested destructure 1`] = `
"import { ref as _ref } from 'vue'
let [{ a: { b: __b }}] = (useFoo())
const b = _ref(__b);
let { c: [__d, __e] } = (useBar())
const d = _ref(__d);
const e = _ref(__e);
console.log(b.value, d.value, e.value)
"
`;
exports[`nested scopes 1`] = `
"import { ref as _ref } from 'vue'
let a = _ref(0)
let b = _ref(0)
let c = 0
a.value++ // outer a
b.value++ // outer b
c++ // outer c
function foo({ a }) {
a++ // inner a
b.value++ // inner b
let c = _ref(0)
c.value++ // inner c
let d = _ref(0)
const bar = (c) => {
c++ // nested c
d.value++ // nested d
}
if (true) {
let a = _ref(0)
a.value++ // if block a
}
return ({ a, b, c, d })
}
"
`;
exports[`object destructure 1`] = `
"import { ref as _ref } from 'vue'
let n = _ref(1), { a: __a, b: __c, d: __d = 1, e: __f = 2, ...__g } = (useFoo())
const a = _ref(__a);
const c = _ref(__c);
const d = _ref(__d);
const f = _ref(__f);
const g = _ref(__g);
let { foo: __foo } = (useSomthing(() => 1));
const foo = _ref(__foo);
console.log(n.value, a.value, c.value, d.value, f.value, g.value, foo.value)
"
`;
exports[`should not rewrite scope variable 1`] = `
"import { ref as _ref } from 'vue'
let a = _ref(1)
let b = _ref(1)
let d = _ref(1)
const e = 1
function test() {
const a = 2
console.log(a)
console.log(b.value)
let c = { c: 3 }
console.log(c)
console.log(d.value)
console.log(e)
}
"
`;
exports[`should not rewrite type identifiers 1`] = `
"import { ref as _ref } from 'vue'
const props = defineProps<{msg: string; ids?: string[]}>()
let ids = _ref([])"
`;
exports[`using ref binding in property shorthand 1`] = `
"import { ref as _ref } from 'vue'
let a = _ref(1)
const b = { a: a.value }
function test() {
const { a } = b
}
"
`;