]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-sfc): fix macro usage in multi-variable declaration (#6778)
author花果山大圣 <316783812@qq.com>
Tue, 8 Nov 2022 03:16:21 +0000 (11:16 +0800)
committerGitHub <noreply@github.com>
Tue, 8 Nov 2022 03:16:21 +0000 (22:16 -0500)
fix #6757

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

index 41c0d472369b58ee4496eca567d1fc1c5e15f665..9dc932034ba39e19c9b291e6f5f2b7a983cf3269 100644 (file)
@@ -720,6 +720,23 @@ return { props, a, emit }
 }"
 `;
 
+exports[`SFC compile <script setup> defineProps/defineEmits in multi-variable declaration fix #6757  1`] = `
+"export default {
+  props: ['item'],
+  emits: ['a'],
+  setup(__props, { expose, emit }) {
+  expose();
+
+const props = __props
+
+    const a = 1;
+    
+return { a, props, emit }
+}
+
+}"
+`;
+
 exports[`SFC compile <script setup> dev mode import usage check TS annotations 1`] = `
 "import { defineComponent as _defineComponent } from 'vue'
 import { Foo, Bar, Baz, Qux, Fred } from './x'
index 09ccb327afef68ff1c09646afb5aa4a295c2882d..08602d3384648c2f665cd6242c82698687f15bee 100644 (file)
@@ -141,6 +141,21 @@ const myEmit = defineEmits(['foo', 'bar'])
     expect(content).toMatch(`emits: ['a'],`)
   })
 
+  // #6757
+  test('defineProps/defineEmits in multi-variable declaration fix #6757 ', () => {
+    const { content } = compile(`
+    <script setup>
+    const a = 1,
+          props = defineProps(['item']),
+          emit = defineEmits(['a']);
+    </script>
+  `)
+    assertCode(content)
+    expect(content).toMatch(`const a = 1;`) // test correct removal
+    expect(content).toMatch(`props: ['item'],`)
+    expect(content).toMatch(`emits: ['a'],`)
+  })
+
   test('defineProps/defineEmits in multi-variable declaration (full removal)', () => {
     const { content } = compile(`
     <script setup>
index b1db1875ac379ee8179a229f7e21f6a577be95fb..09222396fc89cb7187cd313ee1e356fabdbb4b49 100644 (file)
@@ -1177,11 +1177,11 @@ export function compileScript(
             } else {
               let start = decl.start! + startOffset
               let end = decl.end! + startOffset
-              if (i < total - 1) {
-                // not the last one, locate the start of the next
+              if (i === 0) {
+                // first one, locate the start of the next
                 end = node.declarations[i + 1].start! + startOffset
               } else {
-                // last one, locate the end of the prev
+                // not first one, locate the end of the prev
                 start = node.declarations[i - 1].end! + startOffset
               }
               s.remove(start, end)