})"
`;
+exports[`SFC compile <script setup> binding analysis for destructur 1`] = `
+"export default {
+ setup(__props, { expose }) {
+ expose()
+
+ const { foo, b: bar, ['x' + 'y']: baz, x: { y, zz: { z }}} = {}
+
+return { foo, bar, baz, y, z }
+}
+
+}"
+`;
+
exports[`SFC compile <script setup> defineEmits() 1`] = `
"export default {
emits: ['foo', 'bar'],
assertCode(content)
})
+ test('binding analysis for destructur', () => {
+ const { content, bindings } = compile(`
+ <script setup>
+ const { foo, b: bar, ['x' + 'y']: baz, x: { y, zz: { z }}} = {}
+ </script>
+ `)
+ expect(content).toMatch('return { foo, bar, baz, y, z }')
+ expect(bindings).toStrictEqual({
+ foo: BindingTypes.SETUP_MAYBE_REF,
+ bar: BindingTypes.SETUP_MAYBE_REF,
+ baz: BindingTypes.SETUP_MAYBE_REF,
+ y: BindingTypes.SETUP_MAYBE_REF,
+ z: BindingTypes.SETUP_MAYBE_REF
+ })
+ assertCode(content)
+ })
+
test('defineProps()', () => {
const { content, bindings } = compile(`
<script setup>
) {
for (const p of node.properties) {
if (p.type === 'ObjectProperty') {
- // key can only be Identifier in ObjectPattern
- if (p.key.type === 'Identifier') {
- if (p.key === p.value) {
- // const { x } = ...
- const type = isDefineCall
- ? BindingTypes.SETUP_CONST
- : isConst
- ? BindingTypes.SETUP_MAYBE_REF
- : BindingTypes.SETUP_LET
- registerBinding(bindings, p.key, type)
- } else {
- walkPattern(p.value, bindings, isConst, isDefineCall)
- }
+ if (p.key.type === 'Identifier' && p.key === p.value) {
+ // shorthand: const { x } = ...
+ const type = isDefineCall
+ ? BindingTypes.SETUP_CONST
+ : isConst
+ ? BindingTypes.SETUP_MAYBE_REF
+ : BindingTypes.SETUP_LET
+ registerBinding(bindings, p.key, type)
+ } else {
+ walkPattern(p.value, bindings, isConst, isDefineCall)
}
} else {
// ...rest