@@ -80,26 +80,59 @@ function assignmentHelper(visitor, path, childName) {
8080 const assignedNames = utils . getNamesFromPattern ( child ) ;
8181 const nameCount = assignedNames . length ;
8282
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.
8495 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 ] ) ;
88102 }
89103 }
104+
105+ // Wrap assignments to exported identifiers with `module.runSetters`.
106+ if ( modifiedExports . length > 0 ) {
107+ wrap ( visitor , path , modifiedExports ) ;
108+ }
90109}
91110
92- function wrap ( visitor , path ) {
111+ function wrap ( visitor , path , names ) {
93112 const value = path . getValue ( ) ;
94113
95114 if ( visitor . magicString !== null ) {
115+ let end = ')' ;
116+ if ( names ) {
117+ end = `,[${ names . map ( n => `"${ n } "` ) . join ( ',' ) } ])` ;
118+ }
119+
96120 visitor . magicString . prependRight (
97121 value . start ,
98122 visitor . moduleAlias + ".runSetters("
99- ) . appendLeft ( value . end , ")" ) ;
123+ ) . appendLeft ( value . end , end ) ;
100124 }
101125
102126 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+
103136 path . replace ( {
104137 type : "CallExpression" ,
105138 callee : {
@@ -113,7 +146,7 @@ function wrap(visitor, path) {
113146 name : "runSetters"
114147 }
115148 } ,
116- arguments : [ value ]
149+ arguments : args
117150 } ) ;
118151 }
119152}
0 commit comments