Skip to content

Commit d57b3c4

Browse files
brad4dcopybara-github
authored andcommitted
Use LinkedHash(Map|Set) in more places for deterministic behavior
It's important that the compiler always iterate over maps and sets in a deterministic order. PiperOrigin-RevId: 563512674
1 parent 49501b2 commit d57b3c4

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
import com.google.javascript.rhino.JSDocInfo;
2727
import com.google.javascript.rhino.Node;
2828
import java.util.ArrayList;
29-
import java.util.HashMap;
30-
import java.util.HashSet;
3129
import java.util.LinkedHashMap;
3230
import java.util.LinkedHashSet;
3331
import java.util.List;
@@ -107,7 +105,7 @@ public final class ConcretizeStaticInheritanceForInlining implements CompilerPas
107105
static final DiagnosticType DUPLICATE_CLASS =
108106
DiagnosticType.error("DUPLICATE_CLASS", "Multiple classes cannot share the same name: {0}");
109107

110-
private final Set<String> duplicateClassNames = new HashSet<>();
108+
private final Set<String> duplicateClassNames = new LinkedHashSet<>();
111109

112110
// Property names that may cause issues if they are concretized.
113111
private static final ImmutableSet<String> BANNED_PROP_NAMES =
@@ -117,7 +115,7 @@ private static class JavascriptClass {
117115
// All static members to the class including get set properties.
118116
private final Set<Node> staticMembers = new LinkedHashSet<>();
119117
// Keep updated the set of static member names to avoid O(n^2) searches.
120-
private final Set<String> staticMemberNames = new HashSet<>();
118+
private final Set<String> staticMemberNames = new LinkedHashSet<>();
121119
// Collect all the static field accesses to the class.
122120
private final Set<Node> staticFieldAccess = new LinkedHashSet<>();
123121
// Collect all get set properties as defined by Object.defineProperties(...)
@@ -203,7 +201,9 @@ private void copyDeclarations(
203201
}
204202

205203
private void copyStaticMembers(
206-
JavascriptClass superClass, JavascriptClass subClass, Node inheritsCall,
204+
JavascriptClass superClass,
205+
JavascriptClass subClass,
206+
Node inheritsCall,
207207
FindStaticMembers findStaticMembers) {
208208
for (Node staticMember : superClass.staticMembers) {
209209
checkState(staticMember.isAssign(), staticMember);
@@ -276,7 +276,7 @@ private class FindStaticMembers extends AbstractPostOrderCallback {
276276
final List<Node> inheritsCalls = new ArrayList<>();
277277
// Store the order we find class definitions and static fields. Copied statics must occur
278278
// after both the namespace and the copied property are defined.
279-
final Map<Node, Integer> nodeOrder = new HashMap<>();
279+
final Map<Node, Integer> nodeOrder = new LinkedHashMap<>();
280280

281281
@Override
282282
public void visit(NodeTraversal t, Node n, Node parent) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
import java.util.ArrayDeque;
4747
import java.util.ArrayList;
4848
import java.util.Deque;
49-
import java.util.HashSet;
5049
import java.util.LinkedHashMap;
5150
import java.util.LinkedHashSet;
5251
import java.util.List;
@@ -1299,7 +1298,7 @@ class CollapseProperties implements CompilerPass {
12991298
/** Maps names (e.g. "a.b.c") to nodes in the global namespace tree */
13001299
private Map<String, Name> nameMap;
13011300

1302-
private final HashSet<String> dynamicallyImportedModules = new HashSet<>();
1301+
private final LinkedHashSet<String> dynamicallyImportedModules = new LinkedHashSet<>();
13031302

13041303
@Override
13051304
public void process(Node externs, Node root) {
@@ -1365,7 +1364,8 @@ private boolean canEliminate(Name name) {
13651364
*/
13661365
private ImmutableSet<Name> checkNamespaces() {
13671366
ImmutableSet.Builder<Name> escaped = ImmutableSet.builder();
1368-
HashSet<String> dynamicallyImportedModuleRefs = new HashSet<>(dynamicallyImportedModules);
1367+
LinkedHashSet<String> dynamicallyImportedModuleRefs =
1368+
new LinkedHashSet<>(dynamicallyImportedModules);
13691369
if (!dynamicallyImportedModules.isEmpty()) {
13701370
// When the output chunk type is ES_MODULES, properties of the module namespace
13711371
// must not be collapsed as they are referenced off the namespace object. The namespace

0 commit comments

Comments
 (0)