]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-sfc): allow declaring variables after defineProps (#7461)
authorJulien Huang <63512348+huang-julien@users.noreply.github.com>
Mon, 9 Jan 2023 14:13:58 +0000 (15:13 +0100)
committerGitHub <noreply@github.com>
Mon, 9 Jan 2023 14:13:58 +0000 (15:13 +0100)
* fix(compiler-sfc): allow declaring variables after defineProps

* test(compiler-sfc): test defineProps in multiple variable declaration

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

index 7641a151ef5268c1287ec090881f93aa5b74aa07..77048203996ca4a42854ee0a56cf40fc18edad02 100644 (file)
@@ -136,6 +136,24 @@ return () => {}
 })"
 `;
 
+exports[`sfc props transform multiple variable declarations 1`] = `
+"import { toDisplayString as _toDisplayString, openBlock as _openBlock, createElementBlock as _createElementBlock } from "vue"
+
+
+export default {
+  props: ['foo'],
+  setup(__props) {
+
+      const bar = 'fish', hello = 'world'
+      
+return (_ctx, _cache) => {
+  return (_openBlock(), _createElementBlock("div", null, _toDisplayString(__props.foo) + " " + _toDisplayString(hello) + " " + _toDisplayString(bar), 1 /* TEXT */))
+}
+}
+
+}"
+`;
+
 exports[`sfc props transform nested scope 1`] = `
 "export default {
   props: ['foo', 'bar'],
index 6fcc0e59068f37f5a4ffd91c85a46580d0a448a7..05c7989b8f1da6818eb59eb7f17d27324d5bb837 100644 (file)
@@ -28,6 +28,26 @@ describe('sfc props transform', () => {
     })
   })
 
+  test('multiple variable declarations', () => {
+    const { content, bindings } = compile(`
+      <script setup>
+      const bar = 'fish', { foo } = defineProps(['foo']), hello = 'world'
+      </script>
+      <template><div>{{ foo }} {{ hello }} {{ bar }}</div></template>
+    `)
+    expect(content).not.toMatch(`const { foo } =`)
+    expect(content).toMatch(`const bar = 'fish', hello = 'world'`)
+    expect(content).toMatch(`_toDisplayString(hello)`)
+    expect(content).toMatch(`_toDisplayString(bar)`)
+    expect(content).toMatch(`_toDisplayString(__props.foo)`)
+    assertCode(content)
+    expect(bindings).toStrictEqual({
+      foo: BindingTypes.PROPS,
+      bar: BindingTypes.SETUP_CONST,
+      hello: BindingTypes.SETUP_CONST
+    })
+  })
+
   test('nested scope', () => {
     const { content, bindings } = compile(`
       <script setup>
index 997d7c11b9fe6a97188d7b82d836f4ef2f8d9ca8..74cbb5396ae1be891e1071e53450464df76f67ec 100644 (file)
@@ -1758,8 +1758,7 @@ function walkDeclaration(
         registerBinding(bindings, id, bindingType)
       } else {
         if (isCallOf(init, DEFINE_PROPS)) {
-          // skip walking props destructure
-          return
+          continue
         }
         if (id.type === 'ObjectPattern') {
           walkObjectPattern(id, bindings, isConst, isDefineCall)