Skip to content

Commit 336a3d7

Browse files
authored
fix(compiler-sfc): properly remove comma of multiple macros in the same declaration (#7423)
closes #7422 reverts #6778
1 parent 9f5e20c commit 336a3d7

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed

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

+18
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,24 @@ return { a, props, emit }
754754
}"
755755
`;
756756

757+
exports[`SFC compile <script setup> > defineProps/defineEmits in multi-variable declaration fix #7422 1`] = `
758+
"export default {
759+
props: ['item'],
760+
emits: ['foo'],
761+
setup(__props, { expose, emit: emits }) {
762+
expose();
763+
764+
const props = __props;
765+
766+
const a = 0,
767+
b = 0;
768+
769+
return { props, emits, a, b }
770+
}
771+
772+
}"
773+
`;
774+
757775
exports[`SFC compile <script setup> > dev mode import usage check > TS annotations 1`] = `
758776
"import { defineComponent as _defineComponent } from 'vue'
759777
import { Foo, Bar, Baz, Qux, Fred } from './x'

packages/compiler-sfc/__tests__/compileScript.spec.ts

+17
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,23 @@ const myEmit = defineEmits(['foo', 'bar'])
171171
expect(content).toMatch(`emits: ['a'],`)
172172
})
173173

174+
// #7422
175+
test('defineProps/defineEmits in multi-variable declaration fix #7422', () => {
176+
const { content } = compile(`
177+
<script setup>
178+
const props = defineProps(['item']),
179+
emits = defineEmits(['foo']),
180+
a = 0,
181+
b = 0;
182+
</script>
183+
`)
184+
assertCode(content)
185+
expect(content).toMatch(`props: ['item'],`)
186+
expect(content).toMatch(`emits: ['foo'],`)
187+
expect(content).toMatch(`const a = 0,`)
188+
expect(content).toMatch(`b = 0;`)
189+
})
190+
174191
test('defineProps/defineEmits in multi-variable declaration (full removal)', () => {
175192
const { content } = compile(`
176193
<script setup>

packages/compiler-sfc/src/compileScript.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -1258,6 +1258,8 @@ export function compileScript(
12581258
if (node.type === 'VariableDeclaration' && !node.declare) {
12591259
const total = node.declarations.length
12601260
let left = total
1261+
let lastNonRemoved: number | undefined
1262+
12611263
for (let i = 0; i < total; i++) {
12621264
const decl = node.declarations[i]
12631265
const init = decl.init && unwrapTSNode(decl.init)
@@ -1280,16 +1282,20 @@ export function compileScript(
12801282
} else {
12811283
let start = decl.start! + startOffset
12821284
let end = decl.end! + startOffset
1283-
if (i === 0) {
1284-
// first one, locate the start of the next
1285-
end = node.declarations[i + 1].start! + startOffset
1285+
if (i === total - 1) {
1286+
// last one, locate the end of the last one that is not removed
1287+
// if we arrive at this branch, there must have been a
1288+
// non-removed decl before us, so lastNonRemoved is non-null.
1289+
start = node.declarations[lastNonRemoved!].end! + startOffset
12861290
} else {
1287-
// not first one, locate the end of the prev
1288-
start = node.declarations[i - 1].end! + startOffset
1291+
// not the last one, locate the start of the next
1292+
end = node.declarations[i + 1].start! + startOffset
12891293
}
12901294
s.remove(start, end)
12911295
left--
12921296
}
1297+
} else {
1298+
lastNonRemoved = i
12931299
}
12941300
}
12951301
}

0 commit comments

Comments
 (0)