@@ -231,15 +231,15 @@ function transformForDeclareReactiveVar(
231
231
//
232
232
// To:
233
233
// $: let id = fn()
234
- // function fn () { return x + y; }
234
+ // function fn () { let tmp; return (tmp = x + y) ; }
235
235
//
236
236
//
237
237
// From:
238
238
// $: ({id} = foo);
239
239
//
240
240
// To:
241
241
// $: let {id} = fn()
242
- // function fn () { return foo; }
242
+ // function fn () { let tmp; return (tmp = foo) ; }
243
243
244
244
/**
245
245
* The opening paren tokens for
@@ -297,6 +297,7 @@ function transformForDeclareReactiveVar(
297
297
}
298
298
299
299
const functionId = ctx . generateUniqueId ( "reactiveVariableScopeFunction" ) ;
300
+ const tmpVarId = ctx . generateUniqueId ( "tmpVar" ) ;
300
301
for ( const token of openParens ) {
301
302
ctx . appendOriginal ( token . range [ 0 ] ) ;
302
303
ctx . skipOriginalOffset ( token . range [ 1 ] - token . range [ 0 ] ) ;
@@ -306,7 +307,7 @@ function transformForDeclareReactiveVar(
306
307
ctx . appendVirtualScript ( "let " ) ;
307
308
ctx . appendOriginal ( eq ? eq . range [ 1 ] : expression . right . range [ 0 ] ) ;
308
309
ctx . appendVirtualScript (
309
- `${ functionId } ();\nfunction ${ functionId } (){return (`
310
+ `${ functionId } ();\nfunction ${ functionId } (){let ${ tmpVarId } ; return (${ tmpVarId } = `
310
311
) ;
311
312
ctx . appendOriginal ( expression . right . range [ 1 ] ) ;
312
313
ctx . appendVirtualScript ( `)` ) ;
@@ -347,46 +348,61 @@ function transformForDeclareReactiveVar(
347
348
! fnDecl ||
348
349
fnDecl . type !== "FunctionDeclaration" ||
349
350
fnDecl . id . name !== functionId ||
350
- fnDecl . body . body . length !== 1 ||
351
- fnDecl . body . body [ 0 ] . type !== "ReturnStatement"
351
+ fnDecl . body . body . length !== 2 ||
352
+ fnDecl . body . body [ 0 ] . type !== "VariableDeclaration" ||
353
+ fnDecl . body . body [ 1 ] . type !== "ReturnStatement"
352
354
) {
353
355
return false ;
354
356
}
355
- const returnStatement = fnDecl . body . body [ 0 ] ;
356
- if ( returnStatement . argument ?. type !== expression . right . type ) {
357
+ const tmpVarDeclaration = fnDecl . body . body [ 0 ] ;
358
+ if (
359
+ tmpVarDeclaration . declarations . length !== 1 ||
360
+ tmpVarDeclaration . declarations [ 0 ] . type !== "VariableDeclarator"
361
+ ) {
357
362
return false ;
358
363
}
364
+ const tempVarDeclId = tmpVarDeclaration . declarations [ 0 ] . id ;
365
+ if (
366
+ tempVarDeclId . type !== "Identifier" ||
367
+ tempVarDeclId . name !== tmpVarId
368
+ ) {
369
+ return false ;
370
+ }
371
+ const returnStatement = fnDecl . body . body [ 1 ] ;
372
+ const assignment = returnStatement . argument ;
373
+ if (
374
+ assignment ?. type !== "AssignmentExpression" ||
375
+ assignment . left . type !== "Identifier" ||
376
+ assignment . right . type !== expression . right . type
377
+ ) {
378
+ return false ;
379
+ }
380
+ const tempLeft = assignment . left ;
359
381
// Remove function declaration
360
382
program . body . splice ( nextIndex , 1 ) ;
361
383
// Restore expression statement
362
- const newExpression : TSESTree . AssignmentExpression = {
363
- type : "AssignmentExpression" as TSESTree . AssignmentExpression [ "type" ] ,
364
- operator : "=" ,
365
- left : idDecl . id ,
366
- right : returnStatement . argument ,
367
- loc : {
368
- start : idDecl . id . loc . start ,
369
- end : expressionCloseParen
370
- ? expressionCloseParen . loc . end
371
- : returnStatement . argument . loc . end ,
372
- } ,
373
- range : [
374
- idDecl . id . range [ 0 ] ,
375
- expressionCloseParen
376
- ? expressionCloseParen . range [ 1 ]
377
- : returnStatement . argument . range [ 1 ] ,
378
- ] ,
384
+ assignment . left = idDecl . id ;
385
+ assignment . loc = {
386
+ start : idDecl . id . loc . start ,
387
+ end : expressionCloseParen
388
+ ? expressionCloseParen . loc . end
389
+ : assignment . right . loc . end ,
379
390
} ;
380
- idDecl . id . parent = newExpression ;
381
- returnStatement . argument . parent = newExpression ;
391
+ assignment . range = [
392
+ idDecl . id . range [ 0 ] ,
393
+ expressionCloseParen
394
+ ? expressionCloseParen . range [ 1 ]
395
+ : assignment . right . range [ 1 ] ,
396
+ ] ;
397
+ idDecl . id . parent = assignment ;
382
398
const newBody : TSESTree . ExpressionStatement = {
383
399
type : "ExpressionStatement" as TSESTree . ExpressionStatement [ "type" ] ,
384
- expression : newExpression ,
400
+ expression : assignment ,
385
401
loc : statement . body . loc ,
386
402
range : statement . body . range ,
387
403
parent : reactiveStatement ,
388
404
} ;
389
- newExpression . parent = newBody ;
405
+ assignment . parent = newBody ;
390
406
reactiveStatement . body = newBody ;
391
407
// Restore statement end location
392
408
reactiveStatement . range [ 1 ] = returnStatement . range [ 1 ] ;
@@ -401,12 +417,20 @@ function transformForDeclareReactiveVar(
401
417
) ;
402
418
403
419
const scopeManager = result . scopeManager as ScopeManager ;
420
+ removeAllScopeAndVariableAndReference ( tmpVarDeclaration , {
421
+ visitorKeys : result . visitorKeys ,
422
+ scopeManager,
423
+ } ) ;
404
424
removeFunctionScope ( fnDecl , scopeManager ) ;
425
+
405
426
const scope = getProgramScope ( scopeManager ) ;
406
427
for ( const reference of getAllReferences ( idDecl . id , scope ) ) {
407
- reference . writeExpr = newExpression . right as ESTree . Expression ;
428
+ reference . writeExpr = assignment . right as ESTree . Expression ;
408
429
}
409
430
431
+ removeIdentifierReference ( tempLeft , scope ) ;
432
+ removeIdentifierVariable ( tempVarDeclId , scope ) ;
433
+
410
434
removeIdentifierReference ( idDecl . init . callee , scope ) ;
411
435
removeIdentifierVariable ( idDecl . id , scope ) ;
412
436
return true ;
0 commit comments