@@ -37,7 +37,7 @@ function reactLabelSugar(_: any, options: PluginOptions): PluginItem {
37
37
return {
38
38
visitor : {
39
39
LabeledStatement : ( path , state : ReactRefsState ) => {
40
- const { node } = path ;
40
+ const { node, scope } = path ;
41
41
const { label, body } = node ;
42
42
if ( label . name !== refLabel ) return ;
43
43
@@ -57,7 +57,7 @@ function reactLabelSugar(_: any, options: PluginOptions): PluginItem {
57
57
58
58
const refItem : ReactRefItem = {
59
59
identify : left ,
60
- modifier : path . scope . generateUidIdentifier ( `set${ capitalize ( left . name ) } ` ) ,
60
+ modifier : scope . generateUidIdentifier ( `set${ capitalize ( left . name ) } ` ) ,
61
61
} ;
62
62
63
63
path . replaceWith (
@@ -68,14 +68,14 @@ function reactLabelSugar(_: any, options: PluginOptions): PluginItem {
68
68
) ,
69
69
] )
70
70
) ;
71
- if ( ! state . reactRefs ) state . reactRefs = [ ] ;
71
+ state . reactRefs ?? = [ ] ;
72
72
state . reactRefs . push ( refItem ) ;
73
73
} ,
74
74
AssignmentExpression : ( path , state : ReactRefsState ) => {
75
75
if ( ! state . reactRefs ) return ;
76
- const { node } = path ;
76
+ const { node, scope } = path ;
77
77
const ref = getTargetRef ( node . left , state . reactRefs ) ;
78
- if ( ! ref ) return ;
78
+ if ( ! ref || scope . bindings [ ref . identify . name ] ) return ;
79
79
if ( isMemberExpression ( node . left ) ) {
80
80
! ignoreMemberExpr && handleMemberExpression ( node . left , ref , path ) ;
81
81
} else {
@@ -88,21 +88,21 @@ function reactLabelSugar(_: any, options: PluginOptions): PluginItem {
88
88
} ,
89
89
UpdateExpression : ( path , state : ReactRefsState ) => {
90
90
if ( ! state . reactRefs ) return ;
91
- const { node } = path ;
91
+ const { node, scope } = path ;
92
92
const ref = getTargetRef ( node . argument , state . reactRefs ) ;
93
- if ( ! ref ) return ;
93
+ if ( ! ref || scope . bindings [ ref . identify . name ] ) return ;
94
94
if ( isMemberExpression ( node . argument ) ) {
95
95
! ignoreMemberExpr && handleMemberExpression ( node . argument , ref , path ) ;
96
- } else {
97
- path . replaceWith (
98
- callExpression ( ref . modifier , [
99
- arrowFunctionExpression (
100
- [ ref . identify ] ,
101
- binaryExpression ( node . operator === "++" ? "+" : "-" , ref . identify , numericLiteral ( 1 ) )
102
- ) ,
103
- ] )
104
- ) ;
96
+ return ;
105
97
}
98
+ path . replaceWith (
99
+ callExpression ( ref . modifier , [
100
+ arrowFunctionExpression (
101
+ [ ref . identify ] ,
102
+ binaryExpression ( node . operator === "++" ? "+" : "-" , ref . identify , numericLiteral ( 1 ) )
103
+ ) ,
104
+ ] )
105
+ ) ;
106
106
} ,
107
107
} ,
108
108
} ;
0 commit comments