@@ -80,26 +80,59 @@ function assignmentHelper(visitor, path, childName) {
80
80
const assignedNames = utils . getNamesFromPattern ( child ) ;
81
81
const nameCount = assignedNames . length ;
82
82
83
- // Wrap assignments to exported identifiers with `module.runSetters`.
83
+ let scope = null ;
84
+ function inModuleScope ( name ) {
85
+ if ( scope === null ) {
86
+ scope = path . getScope ( ) ;
87
+ }
88
+
89
+ return ! scope || scope . find_owner ( name ) . parent === null ;
90
+ }
91
+
92
+ let modifiedExports = [ ] ;
93
+
94
+ // Identify which exports if any are modified by the assignment.
84
95
for ( let i = 0 ; i < nameCount ; ++ i ) {
85
- if ( visitor . exportedLocalNames [ assignedNames [ i ] ] === true ) {
86
- wrap ( visitor , path ) ;
87
- break ;
96
+ let name = assignedNames [ i ] ;
97
+ if (
98
+ visitor . exportedLocalNames [ name ] &&
99
+ inModuleScope ( name )
100
+ ) {
101
+ modifiedExports . push ( ...visitor . exportedLocalNames [ name ] ) ;
88
102
}
89
103
}
104
+
105
+ // Wrap assignments to exported identifiers with `module.runSetters`.
106
+ if ( modifiedExports . length > 0 ) {
107
+ wrap ( visitor , path , modifiedExports ) ;
108
+ }
90
109
}
91
110
92
- function wrap ( visitor , path ) {
111
+ function wrap ( visitor , path , names ) {
93
112
const value = path . getValue ( ) ;
94
113
95
114
if ( visitor . magicString !== null ) {
115
+ let end = ')' ;
116
+ if ( names ) {
117
+ end = `,[${ names . map ( n => `"${ n } "` ) . join ( ',' ) } ])` ;
118
+ }
119
+
96
120
visitor . magicString . prependRight (
97
121
value . start ,
98
122
visitor . moduleAlias + ".runSetters("
99
- ) . appendLeft ( value . end , ")" ) ;
123
+ ) . appendLeft ( value . end , end ) ;
100
124
}
101
125
102
126
if ( visitor . modifyAST ) {
127
+ let args = [ value ] ;
128
+ if ( names ) {
129
+ let array = {
130
+ type : "ArrayExpression" ,
131
+ elements : names . map ( n => ( { type : "StringLiteral" , value : n } ) )
132
+ } ;
133
+ args . push ( array ) ;
134
+ }
135
+
103
136
path . replace ( {
104
137
type : "CallExpression" ,
105
138
callee : {
@@ -113,7 +146,7 @@ function wrap(visitor, path) {
113
146
name : "runSetters"
114
147
}
115
148
} ,
116
- arguments : [ value ]
149
+ arguments : args
117
150
} ) ;
118
151
}
119
152
}
0 commit comments