@@ -155,11 +155,7 @@ export function compileScript(
155
155
helperImports . add ( 'ref' )
156
156
const { left, right } = exp
157
157
if ( left . type === 'Identifier' ) {
158
- if ( left . name [ 0 ] === '$' ) {
159
- error ( `ref variable identifiers cannot start with $.` , left )
160
- }
161
- refBindings [ left . name ] = setupBindings [ left . name ] = true
162
- refIdentifiers . add ( left )
158
+ registerRefBinding ( left )
163
159
s . prependRight ( right . start ! + startOffset , `ref(` )
164
160
s . appendLeft ( right . end ! + startOffset , ')' )
165
161
} else if ( left . type === 'ObjectPattern' ) {
@@ -186,11 +182,22 @@ export function compileScript(
186
182
// possible multiple declarations
187
183
// ref: x = 1, y = 2
188
184
exp . expressions . forEach ( e => processRefExpression ( e , statement ) )
185
+ } else if ( exp . type === 'Identifier' ) {
186
+ registerRefBinding ( exp )
187
+ s . appendLeft ( exp . end ! + startOffset , ` = ref()` )
189
188
} else {
190
189
error ( `ref: statements can only contain assignment expressions.` , exp )
191
190
}
192
191
}
193
192
193
+ function registerRefBinding ( id : Identifier ) {
194
+ if ( id . name [ 0 ] === '$' ) {
195
+ error ( `ref variable identifiers cannot start with $.` , id )
196
+ }
197
+ refBindings [ id . name ] = setupBindings [ id . name ] = true
198
+ refIdentifiers . add ( id )
199
+ }
200
+
194
201
function processRefObjectPattern (
195
202
pattern : ObjectPattern ,
196
203
statement : LabeledStatement
@@ -227,9 +234,7 @@ export function compileScript(
227
234
s . prependRight ( nameId . start ! + startOffset , `__` )
228
235
}
229
236
if ( nameId ) {
230
- // register binding
231
- refBindings [ nameId . name ] = setupBindings [ nameId . name ] = true
232
- refIdentifiers . add ( nameId )
237
+ registerRefBinding ( nameId )
233
238
// append binding declarations after the parent statement
234
239
s . appendLeft (
235
240
statement . end ! + startOffset ,
@@ -261,10 +266,9 @@ export function compileScript(
261
266
processRefArrayPattern ( e , statement )
262
267
}
263
268
if ( nameId ) {
269
+ registerRefBinding ( nameId )
270
+ // prefix original
264
271
s . prependRight ( nameId . start ! + startOffset , `__` )
265
- // register binding
266
- refBindings [ nameId . name ] = setupBindings [ nameId . name ] = true
267
- refIdentifiers . add ( nameId )
268
272
// append binding declarations after the parent statement
269
273
s . appendLeft (
270
274
statement . end ! + startOffset ,
0 commit comments