File tree 3 files changed +74
-3
lines changed
3 files changed +74
-3
lines changed Original file line number Diff line number Diff line change @@ -108,6 +108,38 @@ return { props, bar }
108
108
} "
109
109
`;
110
110
111
+ exports[`SFC compile <script setup > defineProps/defineEmits in multi-variable decalration (full removal) 1`] = `
112
+ "export default {
113
+ props : [' item' ],
114
+ emits : [' a' ],
115
+ setup (__props , { expose , emit }) {
116
+ expose()
117
+
118
+ const props = __props
119
+
120
+
121
+ return { props , emit }
122
+ }
123
+
124
+ } "
125
+ `;
126
+
127
+ exports[`SFC compile <script setup > defineProps/defineEmits in multi-variable decalration 1`] = `
128
+ "export default {
129
+ props : [' item' ],
130
+ emits : [' a' ],
131
+ setup (__props , { expose , emit }) {
132
+ expose()
133
+
134
+ const props = __props
135
+ const a = 1;
136
+
137
+ return { props , a , emit }
138
+ }
139
+
140
+ }"
141
+ `;
142
+
111
143
exports[`SFC compile <script setup > errors should allow defineProps/Emit() referencing imported binding 1`] = `
112
144
"import { bar } from './bar'
113
145
Original file line number Diff line number Diff line change @@ -97,6 +97,32 @@ const myEmit = defineEmits(['foo', 'bar'])
97
97
emits: ['foo', 'bar'],` )
98
98
} )
99
99
100
+ test ( 'defineProps/defineEmits in multi-variable decalration' , ( ) => {
101
+ const { content } = compile ( `
102
+ <script setup>
103
+ const props = defineProps(['item']),
104
+ a = 1,
105
+ emit = defineEmits(['a']);
106
+ </script>
107
+ ` )
108
+ assertCode ( content )
109
+ expect ( content ) . toMatch ( `const a = 1;` ) // test correct removal
110
+ expect ( content ) . toMatch ( `props: ['item'],` )
111
+ expect ( content ) . toMatch ( `emits: ['a'],` )
112
+ } )
113
+
114
+ test ( 'defineProps/defineEmits in multi-variable decalration (full removal)' , ( ) => {
115
+ const { content } = compile ( `
116
+ <script setup>
117
+ const props = defineProps(['item']),
118
+ emit = defineEmits(['a']);
119
+ </script>
120
+ ` )
121
+ assertCode ( content )
122
+ expect ( content ) . toMatch ( `props: ['item'],` )
123
+ expect ( content ) . toMatch ( `emits: ['a'],` )
124
+ } )
125
+
100
126
test ( 'defineExpose()' , ( ) => {
101
127
const { content } = compile ( `
102
128
<script setup>
Original file line number Diff line number Diff line change @@ -820,7 +820,10 @@ export function compileScript(
820
820
}
821
821
822
822
if ( node . type === 'VariableDeclaration' && ! node . declare ) {
823
- for ( const decl of node . declarations ) {
823
+ const total = node . declarations . length
824
+ let left = total
825
+ for ( let i = 0 ; i < total ; i ++ ) {
826
+ const decl = node . declarations [ i ]
824
827
if ( decl . init ) {
825
828
const isDefineProps =
826
829
processDefineProps ( decl . init ) || processWithDefaults ( decl . init )
@@ -838,10 +841,20 @@ export function compileScript(
838
841
)
839
842
}
840
843
if ( isDefineProps || isDefineEmits )
841
- if ( node . declarations . length === 1 ) {
844
+ if ( left === 1 ) {
842
845
s . remove ( node . start ! + startOffset , node . end ! + startOffset )
843
846
} else {
844
- s . remove ( decl . start ! + startOffset , decl . end ! + startOffset )
847
+ let start = decl . start ! + startOffset
848
+ let end = decl . end ! + startOffset
849
+ if ( i < total - 1 ) {
850
+ // not the last one, locate the start of the next
851
+ end = node . declarations [ i + 1 ] . start ! + startOffset
852
+ } else {
853
+ // last one, locate the end of the prev
854
+ start = node . declarations [ i - 1 ] . end ! + startOffset
855
+ }
856
+ s . remove ( start , end )
857
+ left --
845
858
}
846
859
}
847
860
}
You can’t perform that action at this time.
0 commit comments