}"
`;
+exports[`SFC compile <script setup> defineProps/defineEmits in multi-variable decalration (full removal) 1`] = `
+"export default {
+ props: ['item'],
+ emits: ['a'],
+ setup(__props, { expose, emit }) {
+ expose()
+
+const props = __props
+
+
+return { props, emit }
+}
+
+}"
+`;
+
+exports[`SFC compile <script setup> defineProps/defineEmits in multi-variable decalration 1`] = `
+"export default {
+ props: ['item'],
+ emits: ['a'],
+ setup(__props, { expose, emit }) {
+ expose()
+
+const props = __props
+ const a = 1;
+
+return { props, a, emit }
+}
+
+}"
+`;
+
exports[`SFC compile <script setup> errors should allow defineProps/Emit() referencing imported binding 1`] = `
"import { bar } from './bar'
emits: ['foo', 'bar'],`)
})
+ test('defineProps/defineEmits in multi-variable decalration', () => {
+ const { content } = compile(`
+ <script setup>
+ const props = defineProps(['item']),
+ a = 1,
+ 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 decalration (full removal)', () => {
+ const { content } = compile(`
+ <script setup>
+ const props = defineProps(['item']),
+ emit = defineEmits(['a']);
+ </script>
+ `)
+ assertCode(content)
+ expect(content).toMatch(`props: ['item'],`)
+ expect(content).toMatch(`emits: ['a'],`)
+ })
+
test('defineExpose()', () => {
const { content } = compile(`
<script setup>
}
if (node.type === 'VariableDeclaration' && !node.declare) {
- for (const decl of node.declarations) {
+ const total = node.declarations.length
+ let left = total
+ for (let i = 0; i < total; i++) {
+ const decl = node.declarations[i]
if (decl.init) {
const isDefineProps =
processDefineProps(decl.init) || processWithDefaults(decl.init)
)
}
if (isDefineProps || isDefineEmits)
- if (node.declarations.length === 1) {
+ if (left === 1) {
s.remove(node.start! + startOffset, node.end! + startOffset)
} else {
- s.remove(decl.start! + startOffset, decl.end! + startOffset)
+ let start = decl.start! + startOffset
+ let end = decl.end! + startOffset
+ if (i < total - 1) {
+ // not the last one, locate the start of the next
+ end = node.declarations[i + 1].start! + startOffset
+ } else {
+ // last one, locate the end of the prev
+ start = node.declarations[i - 1].end! + startOffset
+ }
+ s.remove(start, end)
+ left--
}
}
}