Skip to content

Commit a95390a

Browse files
committed
fix: defineProps/defineEmits removal in multi-variable declaration fix vuejs#6757
1 parent 24f4c47 commit a95390a

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

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

+17
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,23 @@ return { props, a, emit }
720720
}"
721721
`;
722722

723+
exports[`SFC compile <script setup> defineProps/defineEmits in multi-variable declaration fix #6757 1`] = `
724+
"export default {
725+
props: ['item'],
726+
emits: ['a'],
727+
setup(__props, { expose, emit }) {
728+
expose();
729+
730+
const props = __props
731+
732+
const a = 1;
733+
734+
return { a, props, emit }
735+
}
736+
737+
}"
738+
`;
739+
723740
exports[`SFC compile <script setup> dev mode import usage check TS annotations 1`] = `
724741
"import { defineComponent as _defineComponent } from 'vue'
725742
import { Foo, Bar, Baz, Qux, Fred } from './x'

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

+15
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,21 @@ const myEmit = defineEmits(['foo', 'bar'])
141141
expect(content).toMatch(`emits: ['a'],`)
142142
})
143143

144+
// #6757
145+
test('defineProps/defineEmits in multi-variable declaration fix #6757 ', () => {
146+
const { content } = compile(`
147+
<script setup>
148+
const a = 1,
149+
props = defineProps(['item']),
150+
emit = defineEmits(['a']);
151+
</script>
152+
`)
153+
assertCode(content)
154+
expect(content).toMatch(`const a = 1;`) // test correct removal
155+
expect(content).toMatch(`props: ['item'],`)
156+
expect(content).toMatch(`emits: ['a'],`)
157+
})
158+
144159
test('defineProps/defineEmits in multi-variable declaration (full removal)', () => {
145160
const { content } = compile(`
146161
<script setup>

packages/compiler-sfc/src/compileScript.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1105,11 +1105,11 @@ export function compileScript(
11051105
} else {
11061106
let start = decl.start! + startOffset
11071107
let end = decl.end! + startOffset
1108-
if (i < total - 1) {
1109-
// not the last one, locate the start of the next
1108+
if (i === 0) {
1109+
// first one, locate the start of the next
11101110
end = node.declarations[i + 1].start! + startOffset
11111111
} else {
1112-
// last one, locate the end of the prev
1112+
// not first one, locate the end of the prev
11131113
start = node.declarations[i - 1].end! + startOffset
11141114
}
11151115
s.remove(start, end)

0 commit comments

Comments
 (0)