39
39
import org .jspecify .nullness .Nullable ;
40
40
41
41
/**
42
- * Find all Functions, VARs, and Exception names and make them
43
- * unique. Specifically, it will not modify object properties.
42
+ * Find all Functions, VARs, and Exception names and make them unique. Specifically, it will not
43
+ * modify object properties.
44
44
*/
45
45
class MakeDeclaredNamesUnique extends NodeTraversal .AbstractScopedCallback {
46
46
@@ -109,7 +109,7 @@ static Builder builder() {
109
109
}
110
110
111
111
static CompilerPass getContextualRenameInverter (AbstractCompiler compiler ) {
112
- return new ContextualRenameInverter (compiler , true );
112
+ return new ContextualRenameInverter (compiler );
113
113
}
114
114
115
115
@ Override
@@ -210,21 +210,19 @@ private void findDeclaredNames(NodeTraversal t, Node n) {
210
210
}
211
211
}
212
212
213
- /**
214
- * Declared names renaming policy interface.
215
- */
213
+ /** Declared names renaming policy interface. */
216
214
interface Renamer {
217
215
218
216
/**
219
217
* Called when a declared name is found in the local current scope.
218
+ *
220
219
* @param hoisted Whether this name should be declared in the nearest enclosing "hoist scope"
221
220
* instead of the scope represented by this Renamer.
222
221
*/
223
222
void addDeclaredName (String name , boolean hoisted );
224
223
225
224
/**
226
- * @return A replacement name, null if oldName is unknown or should not
227
- * be replaced.
225
+ * @return A replacement name, null if oldName is unknown or should not be replaced.
228
226
*/
229
227
String getReplacementName (String oldName );
230
228
@@ -245,9 +243,7 @@ interface Renamer {
245
243
Renamer getHoistRenamer ();
246
244
}
247
245
248
- /**
249
- * Inverts the transformation by {@link ContextualRenamer}, when possible.
250
- */
246
+ /** Inverts the transformation by {@link ContextualRenamer}, when possible. */
251
247
static class ContextualRenameInverter implements ScopedCallback , CompilerPass {
252
248
private final AbstractCompiler compiler ;
253
249
@@ -261,12 +257,8 @@ static class ContextualRenameInverter implements ScopedCallback, CompilerPass {
261
257
private final ListMultimap <String , Node > nameMap =
262
258
MultimapBuilder .hashKeys ().arrayListValues ().build ();
263
259
264
- // Whether to report changes to the compiler.
265
- private final boolean markChanges ;
266
-
267
- private ContextualRenameInverter (AbstractCompiler compiler , boolean markChanges ) {
260
+ private ContextualRenameInverter (AbstractCompiler compiler ) {
268
261
this .compiler = compiler ;
269
- this .markChanges = markChanges ;
270
262
}
271
263
272
264
@ Override
@@ -287,9 +279,7 @@ private static boolean containsSeparator(String name) {
287
279
return name .contains (ContextualRenamer .UNIQUE_ID_SEPARATOR );
288
280
}
289
281
290
- /**
291
- * Prepare a set for the new scope.
292
- */
282
+ /** Prepare a set for the new scope. */
293
283
@ Override
294
284
public void enterScope (NodeTraversal t ) {
295
285
if (t .inGlobalScope ()) {
@@ -301,8 +291,8 @@ public void enterScope(NodeTraversal t) {
301
291
}
302
292
303
293
/**
304
- * Rename vars for the current scope, and merge any referenced
305
- * names into the parent scope reference set.
294
+ * Rename vars for the current scope, and merge any referenced names into the parent scope
295
+ * reference set.
306
296
*/
307
297
@ Override
308
298
public void exitScope (NodeTraversal t ) {
@@ -327,9 +317,8 @@ public void exitScope(NodeTraversal t) {
327
317
}
328
318
329
319
/**
330
- * For the Var declared in the current scope determine if it is possible
331
- * to revert the name to its original form without conflicting with other
332
- * values.
320
+ * For the Var declared in the current scope determine if it is possible to revert the name to
321
+ * its original form without conflicting with other values.
333
322
*/
334
323
void handleScopeVar (Var v ) {
335
324
String name = v .getName ();
@@ -343,23 +332,19 @@ void handleScopeVar(Var v) {
343
332
for (Node n : references ) {
344
333
checkState (n .isName () || n .isImportStar (), n );
345
334
n .setString (newName );
346
- if (markChanges ) {
347
- compiler .reportChangeToEnclosingScope (n );
348
- Node parent = n .getParent ();
349
- // If we are renaming a function declaration, make sure the containing scope
350
- // has the opportunity to act on the change.
351
- if (parent .isFunction () && NodeUtil .isFunctionDeclaration (parent )) {
352
- compiler .reportChangeToEnclosingScope (parent );
353
- }
335
+ compiler .reportChangeToEnclosingScope (n );
336
+ Node parent = n .getParent ();
337
+ // If we are renaming a function declaration, make sure the containing scope
338
+ // has the opportunity to act on the change.
339
+ if (parent .isFunction () && NodeUtil .isFunctionDeclaration (parent )) {
340
+ compiler .reportChangeToEnclosingScope (parent );
354
341
}
355
342
}
356
343
nameMap .removeAll (name );
357
344
}
358
345
}
359
346
360
- /**
361
- * Find a name usable in the local scope.
362
- */
347
+ /** Find a name usable in the local scope. */
363
348
private String findReplacementName (String name ) {
364
349
String original = getOriginalName (name );
365
350
String newName = original ;
@@ -374,7 +359,8 @@ private String findReplacementName(String name) {
374
359
* @return Whether the name is valid to use in the local scope.
375
360
*/
376
361
private boolean isValidName (String name ) {
377
- return TokenStream .isJSIdentifier (name ) && !referencedNames .contains (name )
362
+ return TokenStream .isJSIdentifier (name )
363
+ && !referencedNames .contains (name )
378
364
&& !name .equals (ARGUMENTS );
379
365
}
380
366
@@ -508,9 +494,7 @@ public String getReplacementName(String oldName) {
508
494
return declarations .get (oldName );
509
495
}
510
496
511
- /**
512
- * Given a name and the associated id, create a new unique name.
513
- */
497
+ /** Given a name and the associated id, create a new unique name. */
514
498
private static String getUniqueName (String name , int id ) {
515
499
return name + UNIQUE_ID_SEPARATOR + id ;
516
500
}
@@ -534,13 +518,12 @@ public Renamer getHoistRenamer() {
534
518
}
535
519
}
536
520
537
-
538
521
/**
539
- * Rename every declared name to be unique. Typically this would be used
540
- * when injecting code to insure that names do not conflict with existing
541
- * names.
522
+ * Rename every declared name to be unique. Typically, this would be used when injecting code to
523
+ * ensure that names do not conflict with existing names.
524
+ *
525
+ * <p>Used by the FunctionInjector
542
526
*
543
- * Used by the FunctionInjector
544
527
* @see FunctionInjector
545
528
*/
546
529
static class InlineRenamer implements Renamer {
@@ -590,8 +573,7 @@ private String getUniqueName(String name) {
590
573
}
591
574
592
575
if (name .contains (ContextualRenamer .UNIQUE_ID_SEPARATOR )) {
593
- name = name .substring (
594
- 0 , name .lastIndexOf (ContextualRenamer .UNIQUE_ID_SEPARATOR ));
576
+ name = name .substring (0 , name .lastIndexOf (ContextualRenamer .UNIQUE_ID_SEPARATOR ));
595
577
}
596
578
597
579
if (convention .isExported (name )) {
@@ -629,18 +611,16 @@ public Renamer getHoistRenamer() {
629
611
}
630
612
631
613
/**
632
- * For injecting boilerplate libraries. Leaves global names alone
633
- * and renames local names like InlineRenamer.
614
+ * For injecting boilerplate libraries. Leaves global names alone and renames local names like
615
+ * InlineRenamer.
634
616
*/
635
617
static class BoilerplateRenamer extends ContextualRenamer {
636
618
private final Supplier <String > uniqueIdSupplier ;
637
619
private final String idPrefix ;
638
620
private final CodingConvention convention ;
639
621
640
622
BoilerplateRenamer (
641
- CodingConvention convention ,
642
- Supplier <String > uniqueIdSupplier ,
643
- String idPrefix ) {
623
+ CodingConvention convention , Supplier <String > uniqueIdSupplier , String idPrefix ) {
644
624
this .convention = convention ;
645
625
this .uniqueIdSupplier = uniqueIdSupplier ;
646
626
this .idPrefix = idPrefix ;
@@ -690,5 +670,4 @@ public Renamer getHoistRenamer() {
690
670
return delegate .getHoistRenamer ();
691
671
}
692
672
}
693
-
694
673
}
0 commit comments