@@ -112,6 +112,7 @@ export default function rewriteLiveReferences(
112112 programPath ,
113113 // NOTE(logan): The 'Array.from' calls are to make this code with in loose mode.
114114 new Set ( [ ...Array . from ( imported . keys ( ) ) , ...Array . from ( exported . keys ( ) ) ] ) ,
115+ false ,
115116 ) ;
116117
117118 // Rewrite reads/writes from imports and exports to have the correct behavior.
@@ -290,6 +291,81 @@ const rewriteReferencesVisitor: Visitor<RewriteReferencesVisitorState> = {
290291 }
291292 } ,
292293
294+ UpdateExpression ( path ) {
295+ const {
296+ scope,
297+ seen,
298+ imported,
299+ exported,
300+ requeueInParent,
301+ buildImportReference,
302+ } = this ;
303+
304+ if ( seen . has ( path . node ) ) return ;
305+
306+ seen . add ( path . node ) ;
307+
308+ const arg = path . get ( "argument" ) ;
309+
310+ // No change needed
311+ if ( arg . isMemberExpression ( ) ) return ;
312+
313+ const update = path . node ;
314+
315+ if ( arg . isIdentifier ( ) ) {
316+ const localName = arg . node . name ;
317+
318+ // redeclared in this scope
319+ if ( scope . getBinding ( localName ) !== path . scope . getBinding ( localName ) ) {
320+ return ;
321+ }
322+
323+ const exportedNames = exported . get ( localName ) ;
324+ const importData = imported . get ( localName ) ;
325+
326+ if ( exportedNames ?. length > 0 || importData ) {
327+ if ( importData ) {
328+ path . replaceWith (
329+ assignmentExpression (
330+ update . operator [ 0 ] + "=" ,
331+ buildImportReference ( importData , arg . node ) ,
332+ buildImportThrow ( localName ) ,
333+ ) ,
334+ ) ;
335+ } else if ( update . prefix ) {
336+ // ++foo
337+ // => exports.foo = ++foo
338+ path . replaceWith (
339+ buildBindingExportAssignmentExpression (
340+ this . metadata ,
341+ exportedNames ,
342+ cloneNode ( update ) ,
343+ ) ,
344+ ) ;
345+ } else {
346+ // foo++
347+ // => (ref = i++, exports.i = i, ref)
348+ const ref = scope . generateDeclaredUidIdentifier ( localName ) ;
349+
350+ path . replaceWith (
351+ sequenceExpression ( [
352+ assignmentExpression ( "=" , cloneNode ( ref ) , cloneNode ( update ) ) ,
353+ buildBindingExportAssignmentExpression (
354+ this . metadata ,
355+ exportedNames ,
356+ identifier ( localName ) ,
357+ ) ,
358+ cloneNode ( ref ) ,
359+ ] ) ,
360+ ) ;
361+ }
362+ }
363+ }
364+
365+ requeueInParent ( path ) ;
366+ path . skip ( ) ;
367+ } ,
368+
293369 AssignmentExpression : {
294370 exit ( path ) {
295371 const {
0 commit comments