@@ -18,7 +18,8 @@ import {
18
18
hasComplexExpression ,
19
19
findMethodName ,
20
20
isVarName ,
21
- setParentCondition
21
+ setParentCondition ,
22
+ isContainJSXElement
22
23
} from './utils'
23
24
import { difference } from 'lodash'
24
25
import {
@@ -391,11 +392,15 @@ export class RenderParser {
391
392
//
392
393
} else {
393
394
const ifStatement = parentPath . findParent ( p => p . isIfStatement ( ) )
394
- const blockStatement = parentPath . findParent ( p => p . isBlockStatement ( ) && p . parentPath === ifStatement ) as NodePath < t . BlockStatement >
395
+ const blockStatement = parentPath . findParent ( p => p . isBlockStatement ( ) && ( p . parentPath === ifStatement ) ) as NodePath < t . BlockStatement >
395
396
if ( blockStatement && blockStatement . isBlockStatement ( ) ) {
396
397
blockStatement . traverse ( {
397
398
VariableDeclarator : ( p ) => {
398
399
const { id, init } = p . node
400
+ const ifStem = p . parentPath . parentPath . parentPath
401
+ if ( ! ifStem . isIfStatement ( ) || isContainJSXElement ( p ) ) {
402
+ return
403
+ }
399
404
if ( t . isIdentifier ( id ) ) {
400
405
if ( id . name . startsWith ( 'loopArray' ) ) {
401
406
this . renderPath . node . body . body . unshift (
@@ -406,7 +411,7 @@ export class RenderParser {
406
411
)
407
412
} else {
408
413
const newId = this . renderScope . generateDeclaredUidIdentifier ( '$' + id . name )
409
- this . renderScope . rename ( id . name , newId . name )
414
+ blockStatement . scope . rename ( id . name , newId . name )
410
415
p . parentPath . replaceWith (
411
416
template ( 'ID = INIT;' ) ( { ID : newId , INIT : init } )
412
417
)
@@ -901,21 +906,26 @@ export class RenderParser {
901
906
replaceOriginal ( path , parent , name )
902
907
}
903
908
} )
909
+ const replacements = new Set ( )
904
910
component . traverse ( {
905
911
Identifier ( path ) {
906
912
const name = path . node . name
907
913
const parent = path . parent
914
+ if ( replacements . has ( parent ) ) {
915
+ return
916
+ }
908
917
if ( stateToBeAssign . has ( name ) && path . isReferencedIdentifier ( ) ) {
909
- path . replaceWith (
910
- t . memberExpression (
911
- t . identifier ( item . name ) ,
912
- path . node
913
- )
918
+ const replacement = t . memberExpression (
919
+ t . identifier ( item . name ) ,
920
+ path . node
914
921
)
922
+ path . replaceWith ( replacement )
923
+ replacements . add ( replacement )
915
924
hasOriginalRef = true
925
+ } else {
926
+ replaceOriginal ( path , parent , name )
916
927
}
917
928
918
- replaceOriginal ( path , parent , name )
919
929
} ,
920
930
MemberExpression ( path ) {
921
931
const id = findFirstIdentifierFromMemberExpression ( path . node )
0 commit comments