Skip to content

Commit f38ba47

Browse files
brad4dcopybara-github
authored andcommitted
Reformatting of MakeDeclaredNamesUnique
Reformat this code to match our current autoformatting before making real changes to it. Also eliminate a boolean constructor parameter that was always `true`. PiperOrigin-RevId: 551075827
1 parent 58f0fd4 commit f38ba47

File tree

1 file changed

+31
-52
lines changed

1 file changed

+31
-52
lines changed

src/com/google/javascript/jscomp/MakeDeclaredNamesUnique.java

Lines changed: 31 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
import org.jspecify.nullness.Nullable;
4040

4141
/**
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.
4444
*/
4545
class MakeDeclaredNamesUnique extends NodeTraversal.AbstractScopedCallback {
4646

@@ -109,7 +109,7 @@ static Builder builder() {
109109
}
110110

111111
static CompilerPass getContextualRenameInverter(AbstractCompiler compiler) {
112-
return new ContextualRenameInverter(compiler, true);
112+
return new ContextualRenameInverter(compiler);
113113
}
114114

115115
@Override
@@ -210,21 +210,19 @@ private void findDeclaredNames(NodeTraversal t, Node n) {
210210
}
211211
}
212212

213-
/**
214-
* Declared names renaming policy interface.
215-
*/
213+
/** Declared names renaming policy interface. */
216214
interface Renamer {
217215

218216
/**
219217
* Called when a declared name is found in the local current scope.
218+
*
220219
* @param hoisted Whether this name should be declared in the nearest enclosing "hoist scope"
221220
* instead of the scope represented by this Renamer.
222221
*/
223222
void addDeclaredName(String name, boolean hoisted);
224223

225224
/**
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.
228226
*/
229227
String getReplacementName(String oldName);
230228

@@ -245,9 +243,7 @@ interface Renamer {
245243
Renamer getHoistRenamer();
246244
}
247245

248-
/**
249-
* Inverts the transformation by {@link ContextualRenamer}, when possible.
250-
*/
246+
/** Inverts the transformation by {@link ContextualRenamer}, when possible. */
251247
static class ContextualRenameInverter implements ScopedCallback, CompilerPass {
252248
private final AbstractCompiler compiler;
253249

@@ -261,12 +257,8 @@ static class ContextualRenameInverter implements ScopedCallback, CompilerPass {
261257
private final ListMultimap<String, Node> nameMap =
262258
MultimapBuilder.hashKeys().arrayListValues().build();
263259

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) {
268261
this.compiler = compiler;
269-
this.markChanges = markChanges;
270262
}
271263

272264
@Override
@@ -287,9 +279,7 @@ private static boolean containsSeparator(String name) {
287279
return name.contains(ContextualRenamer.UNIQUE_ID_SEPARATOR);
288280
}
289281

290-
/**
291-
* Prepare a set for the new scope.
292-
*/
282+
/** Prepare a set for the new scope. */
293283
@Override
294284
public void enterScope(NodeTraversal t) {
295285
if (t.inGlobalScope()) {
@@ -301,8 +291,8 @@ public void enterScope(NodeTraversal t) {
301291
}
302292

303293
/**
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.
306296
*/
307297
@Override
308298
public void exitScope(NodeTraversal t) {
@@ -327,9 +317,8 @@ public void exitScope(NodeTraversal t) {
327317
}
328318

329319
/**
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.
333322
*/
334323
void handleScopeVar(Var v) {
335324
String name = v.getName();
@@ -343,23 +332,19 @@ void handleScopeVar(Var v) {
343332
for (Node n : references) {
344333
checkState(n.isName() || n.isImportStar(), n);
345334
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);
354341
}
355342
}
356343
nameMap.removeAll(name);
357344
}
358345
}
359346

360-
/**
361-
* Find a name usable in the local scope.
362-
*/
347+
/** Find a name usable in the local scope. */
363348
private String findReplacementName(String name) {
364349
String original = getOriginalName(name);
365350
String newName = original;
@@ -374,7 +359,8 @@ private String findReplacementName(String name) {
374359
* @return Whether the name is valid to use in the local scope.
375360
*/
376361
private boolean isValidName(String name) {
377-
return TokenStream.isJSIdentifier(name) && !referencedNames.contains(name)
362+
return TokenStream.isJSIdentifier(name)
363+
&& !referencedNames.contains(name)
378364
&& !name.equals(ARGUMENTS);
379365
}
380366

@@ -508,9 +494,7 @@ public String getReplacementName(String oldName) {
508494
return declarations.get(oldName);
509495
}
510496

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. */
514498
private static String getUniqueName(String name, int id) {
515499
return name + UNIQUE_ID_SEPARATOR + id;
516500
}
@@ -534,13 +518,12 @@ public Renamer getHoistRenamer() {
534518
}
535519
}
536520

537-
538521
/**
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
542526
*
543-
* Used by the FunctionInjector
544527
* @see FunctionInjector
545528
*/
546529
static class InlineRenamer implements Renamer {
@@ -590,8 +573,7 @@ private String getUniqueName(String name) {
590573
}
591574

592575
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));
595577
}
596578

597579
if (convention.isExported(name)) {
@@ -629,18 +611,16 @@ public Renamer getHoistRenamer() {
629611
}
630612

631613
/**
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.
634616
*/
635617
static class BoilerplateRenamer extends ContextualRenamer {
636618
private final Supplier<String> uniqueIdSupplier;
637619
private final String idPrefix;
638620
private final CodingConvention convention;
639621

640622
BoilerplateRenamer(
641-
CodingConvention convention,
642-
Supplier<String> uniqueIdSupplier,
643-
String idPrefix) {
623+
CodingConvention convention, Supplier<String> uniqueIdSupplier, String idPrefix) {
644624
this.convention = convention;
645625
this.uniqueIdSupplier = uniqueIdSupplier;
646626
this.idPrefix = idPrefix;
@@ -690,5 +670,4 @@ public Renamer getHoistRenamer() {
690670
return delegate.getHoistRenamer();
691671
}
692672
}
693-
694673
}

0 commit comments

Comments
 (0)