@@ -244,8 +244,13 @@ export function processExpressions(
244
244
expressions : SimpleExpressionNode [ ] ,
245
245
) : DeclarationResult {
246
246
// 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 )
249
254
250
255
// process repeated identifiers and member expressions
251
256
// e.g., `foo[baz]` will be transformed into `foo_baz`
@@ -255,6 +260,7 @@ export function processExpressions(
255
260
variableToExpMap ,
256
261
expToVariableMap ,
257
262
seenIdentifier ,
263
+ updatedVariable ,
258
264
)
259
265
260
266
// process duplicate expressions after identifier and member expression handling.
@@ -263,6 +269,8 @@ export function processExpressions(
263
269
context ,
264
270
expressions ,
265
271
varDeclarations ,
272
+ updatedVariable ,
273
+ expToVariableMap ,
266
274
)
267
275
268
276
return genDeclarations ( [ ...varDeclarations , ...expDeclarations ] , context )
@@ -273,11 +281,13 @@ function analyzeExpressions(expressions: SimpleExpressionNode[]) {
273
281
const variableToExpMap = new Map < string , Set < SimpleExpressionNode > > ( )
274
282
const expToVariableMap = new Map < SimpleExpressionNode , string [ ] > ( )
275
283
const seenIdentifier = new Set < string > ( )
284
+ const updatedVariable = new Set < string > ( )
276
285
277
286
const registerVariable = (
278
287
name : string ,
279
288
exp : SimpleExpressionNode ,
280
289
isIdentifier : boolean ,
290
+ parentStack : Node [ ] = [ ] ,
281
291
) => {
282
292
if ( isIdentifier ) seenIdentifier . add ( name )
283
293
seenVariable [ name ] = ( seenVariable [ name ] || 0 ) + 1
@@ -286,6 +296,13 @@ function analyzeExpressions(expressions: SimpleExpressionNode[]) {
286
296
( variableToExpMap . get ( name ) || new Set ( ) ) . add ( exp ) ,
287
297
)
288
298
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
+ }
289
306
}
290
307
291
308
for ( const exp of expressions ) {
@@ -299,14 +316,20 @@ function analyzeExpressions(expressions: SimpleExpressionNode[]) {
299
316
const memberExp = extractMemberExpression ( parent , name => {
300
317
registerVariable ( name , exp , true )
301
318
} )
302
- registerVariable ( memberExp , exp , false )
319
+ registerVariable ( memberExp , exp , false , parentStack )
303
320
} else if ( ! parentStack . some ( isMemberExpression ) ) {
304
- registerVariable ( currentNode . name , exp , true )
321
+ registerVariable ( currentNode . name , exp , true , parentStack )
305
322
}
306
323
} )
307
324
}
308
325
309
- return { seenVariable, seenIdentifier, variableToExpMap, expToVariableMap }
326
+ return {
327
+ seenVariable,
328
+ seenIdentifier,
329
+ variableToExpMap,
330
+ expToVariableMap,
331
+ updatedVariable,
332
+ }
310
333
}
311
334
312
335
function processRepeatedVariables (
@@ -315,9 +338,11 @@ function processRepeatedVariables(
315
338
variableToExpMap : Map < string , Set < SimpleExpressionNode > > ,
316
339
expToVariableMap : Map < SimpleExpressionNode , string [ ] > ,
317
340
seenIdentifier : Set < string > ,
341
+ updatedVariable : Set < string > ,
318
342
) : DeclarationValue [ ] {
319
343
const declarations : DeclarationValue [ ] = [ ]
320
344
for ( const [ name , exps ] of variableToExpMap ) {
345
+ if ( updatedVariable . has ( name ) ) continue
321
346
if ( seenVariable [ name ] > 1 && exps . size > 0 ) {
322
347
const isIdentifier = seenIdentifier . has ( name )
323
348
const varName = isIdentifier ? name : genVarName ( name )
@@ -409,12 +434,19 @@ function processRepeatedExpressions(
409
434
context : CodegenContext ,
410
435
expressions : SimpleExpressionNode [ ] ,
411
436
varDeclarations : DeclarationValue [ ] ,
437
+ updatedVariable : Set < string > ,
438
+ expToVariableMap : Map < SimpleExpressionNode , string [ ] > ,
412
439
) : DeclarationValue [ ] {
413
440
const declarations : DeclarationValue [ ] = [ ]
414
441
const seenExp = expressions . reduce (
415
442
( acc , exp ) => {
443
+ const variables = expToVariableMap . get ( exp )
416
444
// 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
+ ) {
418
450
acc [ exp . content ] = ( acc [ exp . content ] || 0 ) + 1
419
451
}
420
452
return acc
0 commit comments