feat(reactivity-transform): $$() escape for destructured prop bindings

This commit is contained in:
Evan You
2021-12-11 17:50:09 +08:00
parent 179fc05a84
commit 198ca14f19
4 changed files with 96 additions and 15 deletions

View File

@@ -1,5 +1,25 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`sfc props transform $$() escape 1`] = `
"import { toRef as _toRef } from 'vue'
export default {
props: ['foo'],
setup(__props) {
const __props_bar = _toRef(__props, 'bar')
const __props_foo = _toRef(__props, 'foo')
console.log((__props_foo))
console.log((__props_bar))
({ foo: __props_foo, baz: __props_bar })
return () => {}
}
}"
`;
exports[`sfc props transform aliasing 1`] = `
"import { toDisplayString as _toDisplayString } from \\"vue\\"

View File

@@ -36,7 +36,7 @@ describe('SFC compile <script setup>', () => {
assertCode(content)
})
test('binding analysis for destructur', () => {
test('binding analysis for destructure', () => {
const { content, bindings } = compile(`
<script setup>
const { foo, b: bar, ['x' + 'y']: baz, x: { y, zz: { z }}} = {}

View File

@@ -145,6 +145,23 @@ describe('sfc props transform', () => {
})
})
test('$$() escape', () => {
const { content } = compile(`
<script setup>
const { foo, bar: baz } = defineProps(['foo'])
console.log($$(foo))
console.log($$(baz))
$$({ foo, baz })
</script>
`)
expect(content).toMatch(`const __props_foo = _toRef(__props, 'foo')`)
expect(content).toMatch(`const __props_bar = _toRef(__props, 'bar')`)
expect(content).toMatch(`console.log((__props_foo))`)
expect(content).toMatch(`console.log((__props_bar))`)
expect(content).toMatch(`({ foo: __props_foo, baz: __props_bar })`)
assertCode(content)
})
describe('errors', () => {
test('should error on deep destructure', () => {
expect(() =>