From 077450ed181a8938164537f526585580e131cf8b Mon Sep 17 00:00:00 2001
From: webfansplz <308241863@qq.com>
Date: Wed, 9 Nov 2022 14:54:12 +0800
Subject: [PATCH] fix(compiler-sfc): fix macro usage in multi-variable
declaration
---
packages/compiler-sfc/src/compileScript.ts | 6 +++---
.../__snapshots__/compileScript.spec.ts.snap | 16 ++++++++++++++++
packages/compiler-sfc/test/compileScript.spec.ts | 15 +++++++++++++++
3 files changed, 34 insertions(+), 3 deletions(-)
diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts
index 548fc592423..8a0749690a1 100644
--- a/packages/compiler-sfc/src/compileScript.ts
+++ b/packages/compiler-sfc/src/compileScript.ts
@@ -907,11 +907,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)
diff --git a/packages/compiler-sfc/test/__snapshots__/compileScript.spec.ts.snap b/packages/compiler-sfc/test/__snapshots__/compileScript.spec.ts.snap
index bb52a66b907..6d9a1a2a85a 100644
--- a/packages/compiler-sfc/test/__snapshots__/compileScript.spec.ts.snap
+++ b/packages/compiler-sfc/test/__snapshots__/compileScript.spec.ts.snap
@@ -288,6 +288,22 @@ return { props, a, emit }
}"
`;
+exports[`SFC compile
+ `)
+ 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(`