@@ -244,8 +244,13 @@ export function processExpressions(
244244 expressions : SimpleExpressionNode [ ] ,
245245) : DeclarationResult {
246246 // analyze variables
247- const { seenVariable, variableToExpMap, expToVariableMap, seenIdentifier } =
248- analyzeExpressions ( expressions )
247+ const {
248+ seenVariable,
249+ variableToExpMap,
250+ expToVariableMap,
251+ seenIdentifier,
252+ updatedVariable,
253+ } = analyzeExpressions ( expressions )
249254
250255 // process repeated identifiers and member expressions
251256 // e.g., `foo[baz]` will be transformed into `foo_baz`
@@ -255,6 +260,7 @@ export function processExpressions(
255260 variableToExpMap ,
256261 expToVariableMap ,
257262 seenIdentifier ,
263+ updatedVariable ,
258264 )
259265
260266 // process duplicate expressions after identifier and member expression handling.
@@ -263,6 +269,8 @@ export function processExpressions(
263269 context ,
264270 expressions ,
265271 varDeclarations ,
272+ updatedVariable ,
273+ expToVariableMap ,
266274 )
267275
268276 return genDeclarations ( [ ...varDeclarations , ...expDeclarations ] , context )
@@ -273,11 +281,13 @@ function analyzeExpressions(expressions: SimpleExpressionNode[]) {
273281 const variableToExpMap = new Map < string , Set < SimpleExpressionNode > > ( )
274282 const expToVariableMap = new Map < SimpleExpressionNode , string [ ] > ( )
275283 const seenIdentifier = new Set < string > ( )
284+ const updatedVariable = new Set < string > ( )
276285
277286 const registerVariable = (
278287 name : string ,
279288 exp : SimpleExpressionNode ,
280289 isIdentifier : boolean ,
290+ parentStack : Node [ ] = [ ] ,
281291 ) => {
282292 if ( isIdentifier ) seenIdentifier . add ( name )
283293 seenVariable [ name ] = ( seenVariable [ name ] || 0 ) + 1
@@ -286,6 +296,13 @@ function analyzeExpressions(expressions: SimpleExpressionNode[]) {
286296 ( variableToExpMap . get ( name ) || new Set ( ) ) . add ( exp ) ,
287297 )
288298 expToVariableMap . set ( exp , ( expToVariableMap . get ( exp ) || [ ] ) . concat ( name ) )
299+ if (
300+ parentStack . some (
301+ p => p . type === 'UpdateExpression' || p . type === 'AssignmentExpression' ,
302+ )
303+ ) {
304+ updatedVariable . add ( name )
305+ }
289306 }
290307
291308 for ( const exp of expressions ) {
@@ -299,14 +316,20 @@ function analyzeExpressions(expressions: SimpleExpressionNode[]) {
299316 const memberExp = extractMemberExpression ( parent , name => {
300317 registerVariable ( name , exp , true )
301318 } )
302- registerVariable ( memberExp , exp , false )
319+ registerVariable ( memberExp , exp , false , parentStack )
303320 } else if ( ! parentStack . some ( isMemberExpression ) ) {
304- registerVariable ( currentNode . name , exp , true )
321+ registerVariable ( currentNode . name , exp , true , parentStack )
305322 }
306323 } )
307324 }
308325
309- return { seenVariable, seenIdentifier, variableToExpMap, expToVariableMap }
326+ return {
327+ seenVariable,
328+ seenIdentifier,
329+ variableToExpMap,
330+ expToVariableMap,
331+ updatedVariable,
332+ }
310333}
311334
312335function processRepeatedVariables (
@@ -315,9 +338,11 @@ function processRepeatedVariables(
315338 variableToExpMap : Map < string , Set < SimpleExpressionNode > > ,
316339 expToVariableMap : Map < SimpleExpressionNode , string [ ] > ,
317340 seenIdentifier : Set < string > ,
341+ updatedVariable : Set < string > ,
318342) : DeclarationValue [ ] {
319343 const declarations : DeclarationValue [ ] = [ ]
320344 for ( const [ name , exps ] of variableToExpMap ) {
345+ if ( updatedVariable . has ( name ) ) continue
321346 if ( seenVariable [ name ] > 1 && exps . size > 0 ) {
322347 const isIdentifier = seenIdentifier . has ( name )
323348 const varName = isIdentifier ? name : genVarName ( name )
@@ -409,12 +434,19 @@ function processRepeatedExpressions(
409434 context : CodegenContext ,
410435 expressions : SimpleExpressionNode [ ] ,
411436 varDeclarations : DeclarationValue [ ] ,
437+ updatedVariable : Set < string > ,
438+ expToVariableMap : Map < SimpleExpressionNode , string [ ] > ,
412439) : DeclarationValue [ ] {
413440 const declarations : DeclarationValue [ ] = [ ]
414441 const seenExp = expressions . reduce (
415442 ( acc , exp ) => {
443+ const variables = expToVariableMap . get ( exp )
416444 // only handle expressions that are not identifiers
417- if ( exp . ast && exp . ast . type !== 'Identifier' ) {
445+ if (
446+ exp . ast &&
447+ exp . ast . type !== 'Identifier' &&
448+ ! ( variables && variables . some ( v => updatedVariable . has ( v ) ) )
449+ ) {
418450 acc [ exp . content ] = ( acc [ exp . content ] || 0 ) + 1
419451 }
420452 return acc
0 commit comments