]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-sfc): properly analyze destructured bindings with dynamic keys
authorEvan You <yyx990803@gmail.com>
Thu, 9 Sep 2021 16:28:59 +0000 (12:28 -0400)
committerEvan You <yyx990803@gmail.com>
Thu, 9 Sep 2021 16:28:59 +0000 (12:28 -0400)
fix #4540

packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap
packages/compiler-sfc/__tests__/compileScript.spec.ts
packages/compiler-sfc/src/compileScript.ts

index 54591d402bd59e594cd0de62aa4c65b5ff4c8ad1..29bfc58cddb1c56c4cc0fd36ea28998e18e96ca5 100644 (file)
@@ -96,6 +96,19 @@ export default /*#__PURE__*/ Object.assign(__default__, {
 })"
 `;
 
+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'],
index 854357b639775bbe368fef2b60e489dfd141634d..d3a1282f4c549557906acd0a84e10eb17e1c3778 100644 (file)
@@ -36,6 +36,23 @@ describe('SFC compile <script setup>', () => {
     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>
index 523efd724d037afef0ba0e9eb92e81187c7e22bd..609244232ef2ed65126017f9c7b1b8fb4e7c0001 100644 (file)
@@ -1347,19 +1347,16 @@ function walkObjectPattern(
 ) {
   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