]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-sfc): properly remove comma of multiple macros in the same declaration...
author三咲智子 Kevin Deng <sxzz@sxzz.moe>
Tue, 28 Mar 2023 08:23:39 +0000 (16:23 +0800)
committerGitHub <noreply@github.com>
Tue, 28 Mar 2023 08:23:39 +0000 (16:23 +0800)
closes #7422
reverts #6778

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

index c8bdb1573e947ac58af59b9031bd0ce90cc92ed4..99f339591591a852c861095d1e4f4852521f78b1 100644 (file)
@@ -754,6 +754,24 @@ return { a, props, emit }
 }"
 `;
 
+exports[`SFC compile <script setup> > defineProps/defineEmits in multi-variable declaration fix #7422 1`] = `
+"export default {
+  props: ['item'],
+  emits: ['foo'],
+  setup(__props, { expose, emit: emits }) {
+  expose();
+
+const props = __props;
+
+    const a = 0,
+          b = 0;
+    
+return { props, emits, a, b }
+}
+
+}"
+`;
+
 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 b8f80eb8e7a6ed2f57df0de86c4334b4303320b6..1c716fc55dbac343f00c870aa0af5cfb230dd503 100644 (file)
@@ -171,6 +171,23 @@ const myEmit = defineEmits(['foo', 'bar'])
     expect(content).toMatch(`emits: ['a'],`)
   })
 
+  // #7422
+  test('defineProps/defineEmits in multi-variable declaration fix #7422', () => {
+    const { content } = compile(`
+    <script setup>
+    const props = defineProps(['item']),
+          emits = defineEmits(['foo']),
+          a = 0,
+          b = 0;
+    </script>
+  `)
+    assertCode(content)
+    expect(content).toMatch(`props: ['item'],`)
+    expect(content).toMatch(`emits: ['foo'],`)
+    expect(content).toMatch(`const a = 0,`)
+    expect(content).toMatch(`b = 0;`)
+  })
+
   test('defineProps/defineEmits in multi-variable declaration (full removal)', () => {
     const { content } = compile(`
     <script setup>
index 74d4e96349e9847f025e850725e454136eb52e45..f05926df922a556322571684029aec582a904fd2 100644 (file)
@@ -1258,6 +1258,8 @@ export function compileScript(
     if (node.type === 'VariableDeclaration' && !node.declare) {
       const total = node.declarations.length
       let left = total
+      let lastNonRemoved: number | undefined
+
       for (let i = 0; i < total; i++) {
         const decl = node.declarations[i]
         const init = decl.init && unwrapTSNode(decl.init)
@@ -1280,16 +1282,20 @@ export function compileScript(
             } else {
               let start = decl.start! + startOffset
               let end = decl.end! + startOffset
-              if (i === 0) {
-                // first one, locate the start of the next
-                end = node.declarations[i + 1].start! + startOffset
+              if (i === total - 1) {
+                // last one, locate the end of the last one that is not removed
+                // if we arrive at this branch, there must have been a
+                // non-removed decl before us, so lastNonRemoved is non-null.
+                start = node.declarations[lastNonRemoved!].end! + startOffset
               } else {
-                // not first one, locate the end of the prev
-                start = node.declarations[i - 1].end! + startOffset
+                // not the last one, locate the start of the next
+                end = node.declarations[i + 1].start! + startOffset
               }
               s.remove(start, end)
               left--
             }
+          } else {
+            lastNonRemoved = i
           }
         }
       }