Skip to content

Commit d04cb02

Browse files
rishipalcopybara-github
authored andcommitted
Use consistent naming for JSChunk identifiers in JSCompiler.
JSCompiler sometimes uses "chunks" and sometimes uses "modules" for naming to represent JSChunk. This inconsistency can become confusing as modules can also mean other things (goog.module, ESModule). Most of the changes were generated by using the following replacements and fixing the breakages revealed thereby. ``` s/JSChunk module/JSChunk chunk s/JSChunk[] modules/JSChunk[] chunks s/Array<JSChunk> modules/Array<JSChunk> chunks s/setModules(/setChunks( s/getModules()/getChunks() s/JSChunkGraph moduleGraph/JSChunkGraph chunkGraph s/initModules/initChunks s/ModuleSources/ChunkSources ... ``` Other changes were generated manually. #codehealth PiperOrigin-RevId: 562921865
1 parent 454e6c8 commit d04cb02

40 files changed

+931
-961
lines changed

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

Lines changed: 88 additions & 89 deletions
Large diffs are not rendered by default.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ void afterPass(String passName) {}
9292
public abstract @Nullable Node getScriptNode(String filename);
9393

9494
/** Gets the module graph. */
95-
abstract @Nullable JSChunkGraph getModuleGraph();
95+
abstract @Nullable JSChunkGraph getChunkGraph();
9696

9797
/**
9898
* Gets the inputs in the order in which they are being processed. Only for use by {@code
@@ -245,7 +245,7 @@ void afterPass(String passName) {}
245245
* @param module A module. If null, will return the first SCRIPT node of all modules.
246246
* @return A SCRIPT node (never null).
247247
*/
248-
abstract Node getNodeForCodeInsertion(@Nullable JSChunk module);
248+
abstract Node getNodeForCodeInsertion(@Nullable JSChunk chunk);
249249

250250
abstract TypeValidator getTypeValidator();
251251

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

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class AliasStrings implements CompilerPass, NodeTraversal.Callback {
5555

5656
private final AbstractCompiler compiler;
5757

58-
private final JSChunkGraph moduleGraph;
58+
private final JSChunkGraph chunkGraph;
5959

6060
private final boolean outputStringUsage;
6161

@@ -69,10 +69,10 @@ class AliasStrings implements CompilerPass, NodeTraversal.Callback {
6969
private static final int ALIAS_LARGE_STRINGS_LENGTH = 100;
7070

7171
/**
72-
* Map from module to the node in that module that should parent any string variable declarations
73-
* that have to be moved into that module
72+
* Map from chunk to the node in that chunk that should parent any string variable declarations
73+
* that have to be moved into that chunk
7474
*/
75-
private final Map<JSChunk, Node> moduleVarParentMap = new HashMap<>();
75+
private final Map<JSChunk, Node> chunkVarParentMap = new HashMap<>();
7676

7777
/** package private. This value is AND-ed with the hash function to allow
7878
* unit tests to reduce the range of hash values to test collision cases */
@@ -82,18 +82,18 @@ class AliasStrings implements CompilerPass, NodeTraversal.Callback {
8282
* Creates an instance.
8383
*
8484
* @param compiler The compiler
85-
* @param moduleGraph The module graph, or null if there are no modules
85+
* @param chunkGraph The module graph, or null if there are no modules
8686
* @param outputStringUsage Outputs all strings and the number of times they were used in the
8787
* application to the server log.
8888
* @param aliasStringsMode The alias strings policy set to either ALL or LARGE
8989
*/
9090
AliasStrings(
9191
AbstractCompiler compiler,
92-
JSChunkGraph moduleGraph,
92+
JSChunkGraph chunkGraph,
9393
boolean outputStringUsage,
9494
AliasStringsMode aliasStringsMode) {
9595
this.compiler = compiler;
96-
this.moduleGraph = moduleGraph;
96+
this.chunkGraph = chunkGraph;
9797
this.outputStringUsage = outputStringUsage;
9898
this.aliasStringsMode = aliasStringsMode;
9999
}
@@ -154,26 +154,22 @@ public void visit(NodeTraversal t, Node n, Node parent) {
154154

155155
info.occurrences.add(occurrence);
156156

157-
// The current module.
158-
JSChunk module = t.getChunk();
157+
// The current chunk.
158+
JSChunk chunk = t.getChunk();
159159
if (info.occurrences.size() != 1) {
160160
// Check whether the current module depends on the module containing
161161
// the declaration.
162-
if (module != null
163-
&& info.moduleToContainDecl != null
164-
&& module != info.moduleToContainDecl) {
162+
if (chunk != null && info.chunkToContainDecl != null && chunk != info.chunkToContainDecl) {
165163
// We need to declare this string in the deepest module in the
166164
// module dependency graph that both of these modules depend on.
167-
module =
168-
moduleGraph.getDeepestCommonDependencyInclusive(module, info.moduleToContainDecl);
165+
chunk = chunkGraph.getDeepestCommonDependencyInclusive(chunk, info.chunkToContainDecl);
169166
} else {
170167
// use the previously saved insertion location.
171168
return;
172169
}
173170
}
174-
Node varParent =
175-
moduleVarParentMap.computeIfAbsent(module, compiler::getNodeForCodeInsertion);
176-
info.moduleToContainDecl = module;
171+
Node varParent = chunkVarParentMap.computeIfAbsent(chunk, compiler::getNodeForCodeInsertion);
172+
info.chunkToContainDecl = chunk;
177173
info.parentForNewVarDecl = varParent;
178174
info.siblingToInsertVarDeclBefore = varParent.getFirstChild();
179175
}
@@ -302,7 +298,7 @@ private final class StringInfo {
302298

303299
final ArrayList<Node> occurrences = new ArrayList<>();
304300

305-
JSChunk moduleToContainDecl;
301+
JSChunk chunkToContainDecl;
306302
Node parentForNewVarDecl;
307303
Node siblingToInsertVarDeclBefore;
308304

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

Lines changed: 47 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class AnalyzePrototypeProperties implements CompilerPass {
5353
private final boolean anchorUnusedVars;
5454
private final boolean rootScopeUsesAreGlobal;
5555

56-
private final JSChunkGraph moduleGraph;
56+
private final JSChunkGraph chunkGraph;
5757
private final @Nullable JSChunk firstModule;
5858

5959
// Properties that are implicitly used as part of the JS language.
@@ -100,7 +100,7 @@ class AnalyzePrototypeProperties implements CompilerPass {
100100
* Creates a new pass for analyzing prototype properties.
101101
*
102102
* @param compiler The compiler.
103-
* @param moduleGraph The graph for resolving module dependencies.
103+
* @param chunkGraph The graph for resolving module dependencies.
104104
* @param canModifyExterns If true, then we can move prototype properties that are declared in the
105105
* externs file.
106106
* @param anchorUnusedVars If true, then we must mark all vars as referenced, even if they are
@@ -110,18 +110,18 @@ class AnalyzePrototypeProperties implements CompilerPass {
110110
*/
111111
AnalyzePrototypeProperties(
112112
AbstractCompiler compiler,
113-
JSChunkGraph moduleGraph,
113+
JSChunkGraph chunkGraph,
114114
boolean canModifyExterns,
115115
boolean anchorUnusedVars,
116116
boolean rootScopeUsesAreGlobal) {
117117
this.compiler = compiler;
118-
this.moduleGraph = moduleGraph;
118+
this.chunkGraph = chunkGraph;
119119
this.canModifyExterns = canModifyExterns;
120120
this.anchorUnusedVars = anchorUnusedVars;
121121
this.rootScopeUsesAreGlobal = rootScopeUsesAreGlobal;
122122

123-
if (moduleGraph.getChunkCount() > 1) {
124-
firstModule = moduleGraph.getRootChunk();
123+
if (chunkGraph.getChunkCount() > 1) {
124+
firstModule = chunkGraph.getRootChunk();
125125
} else {
126126
firstModule = null;
127127
}
@@ -133,11 +133,11 @@ class AnalyzePrototypeProperties implements CompilerPass {
133133

134134
for (String property : IMPLICITLY_USED_PROPERTIES) {
135135
NameInfo nameInfo = getNameInfoForName(property, PROPERTY);
136-
if (moduleGraph == null) {
136+
if (chunkGraph == null) {
137137
symbolGraph.connect(externNode, null, nameInfo);
138138
} else {
139-
for (JSChunk module : moduleGraph.getAllChunks()) {
140-
symbolGraph.connect(externNode, module, nameInfo);
139+
for (JSChunk chunk : chunkGraph.getAllChunks()) {
140+
symbolGraph.connect(externNode, chunk, nameInfo);
141141
}
142142
}
143143
}
@@ -402,7 +402,7 @@ public void visit(NodeTraversal t, Node n, Node parent) {
402402
}
403403
}
404404

405-
private void addSymbolUse(String name, JSChunk module, SymbolType type) {
405+
private void addSymbolUse(String name, JSChunk chunk, SymbolType type) {
406406
NameInfo info = getNameInfoForName(name, type);
407407
NameInfo def = null;
408408
// Skip all anonymous nodes. We care only about symbols with names.
@@ -413,7 +413,7 @@ private void addSymbolUse(String name, JSChunk module, SymbolType type) {
413413
}
414414
}
415415
if (!def.equals(info)) {
416-
symbolGraph.connect(def, module, info);
416+
symbolGraph.connect(def, chunk, info);
417417
}
418418
}
419419

@@ -583,8 +583,8 @@ private void processMemberDef(NodeTraversal t, Node n) {
583583
return maybeName.isName() ? t.getScope().getVar(maybeName.getString()) : null;
584584
}
585585

586-
private void addGlobalUseOfSymbol(String name, JSChunk module, SymbolType type) {
587-
symbolGraph.connect(globalNode, module, getNameInfoForName(name, type));
586+
private void addGlobalUseOfSymbol(String name, JSChunk chunk, SymbolType type) {
587+
symbolGraph.connect(globalNode, chunk, getNameInfoForName(name, type));
588588
}
589589
}
590590

@@ -623,7 +623,7 @@ private class PropagateReferences implements EdgeCallback<NameInfo, JSChunk> {
623623
public boolean traverseEdge(NameInfo start, JSChunk edge, NameInfo dest) {
624624
if (start.isReferenced()) {
625625
JSChunk startModule = start.getDeepestCommonModuleRef();
626-
if (startModule != null && moduleGraph.dependsOn(startModule, edge)) {
626+
if (startModule != null && chunkGraph.dependsOn(startModule, edge)) {
627627
return dest.markReference(startModule);
628628
} else {
629629
return dest.markReference(edge);
@@ -639,7 +639,7 @@ interface Symbol {
639639
Var getRootVar();
640640

641641
/** Returns the module where this appears. */
642-
JSChunk getModule();
642+
JSChunk getChunk();
643643
}
644644

645645
private enum SymbolType {
@@ -653,15 +653,15 @@ private enum SymbolType {
653653
*/
654654
static class GlobalFunction implements Symbol {
655655
private final Var var;
656-
private final JSChunk module;
656+
private final JSChunk chunk;
657657

658-
GlobalFunction(Node nameNode, Var var, JSChunk module) {
658+
GlobalFunction(Node nameNode, Var var, JSChunk chunk) {
659659
Node parent = nameNode.getParent();
660660
checkState(
661661
(NodeUtil.isNameDeclaration(parent) && var.isGlobal())
662662
|| NodeUtil.isFunctionDeclaration(parent));
663663
this.var = var;
664-
this.module = module;
664+
this.chunk = chunk;
665665
}
666666

667667
@Override
@@ -670,8 +670,8 @@ public Var getRootVar() {
670670
}
671671

672672
@Override
673-
public JSChunk getModule() {
674-
return module;
673+
public JSChunk getChunk() {
674+
return chunk;
675675
}
676676
}
677677

@@ -684,14 +684,14 @@ interface Property extends Symbol {}
684684
static class ClassMemberFunction implements Property {
685685
private final Node node;
686686
private final Var var;
687-
private final JSChunk module;
687+
private final JSChunk chunk;
688688

689-
ClassMemberFunction(Node node, Var var, JSChunk module) {
689+
ClassMemberFunction(Node node, Var var, JSChunk chunk) {
690690
checkState(node.getParent().isClassMembers());
691691
checkState(node.isMemberFunctionDef() || node.isSetterDef() || node.isGetterDef());
692692
this.node = node;
693693
this.var = var;
694-
this.module = module;
694+
this.chunk = chunk;
695695
}
696696

697697
@Override
@@ -700,8 +700,8 @@ public Var getRootVar() {
700700
}
701701

702702
@Override
703-
public JSChunk getModule() {
704-
return module;
703+
public JSChunk getChunk() {
704+
return chunk;
705705
}
706706

707707
/** Returns the function node within the definition. */
@@ -751,13 +751,15 @@ interface PrototypeProperty extends Property {
751751
static class AssignmentPrototypeProperty implements PrototypeProperty {
752752
private final Node exprNode;
753753
private final Var rootVar;
754-
private final JSChunk module;
754+
private final JSChunk chunk;
755755

756-
/** @param node An EXPR node. */
757-
AssignmentPrototypeProperty(Node node, Var rootVar, JSChunk module) {
756+
/**
757+
* @param node An EXPR node.
758+
*/
759+
AssignmentPrototypeProperty(Node node, Var rootVar, JSChunk chunk) {
758760
this.exprNode = node;
759761
this.rootVar = rootVar;
760-
this.module = module;
762+
this.chunk = chunk;
761763
}
762764

763765
@Override
@@ -780,8 +782,8 @@ private Node getAssignNode() {
780782
}
781783

782784
@Override
783-
public JSChunk getModule() {
784-
return module;
785+
public JSChunk getChunk() {
786+
return chunk;
785787
}
786788
}
787789

@@ -795,13 +797,13 @@ static class LiteralPrototypeProperty implements PrototypeProperty {
795797
private final Node value;
796798
private final Node assign;
797799
private final Var rootVar;
798-
private final JSChunk module;
800+
private final JSChunk chunk;
799801

800-
LiteralPrototypeProperty(Node value, Node assign, Var rootVar, JSChunk module) {
802+
LiteralPrototypeProperty(Node value, Node assign, Var rootVar, JSChunk chunk) {
801803
this.value = value;
802804
this.assign = assign;
803805
this.rootVar = rootVar;
804-
this.module = module;
806+
this.chunk = chunk;
805807
}
806808

807809
@Override
@@ -820,8 +822,8 @@ public Node getValue() {
820822
}
821823

822824
@Override
823-
public JSChunk getModule() {
824-
return module;
825+
public JSChunk getChunk() {
826+
return chunk;
825827
}
826828
}
827829

@@ -849,7 +851,7 @@ class NameInfo {
849851

850852
private boolean referenced = false;
851853
private final Deque<Symbol> declarations = new ArrayDeque<>();
852-
private @Nullable JSChunk deepestCommonModuleRef = null;
854+
private @Nullable JSChunk deepestCommonChunkRef = null;
853855

854856
// True if this property is a function that reads a variable from an
855857
// outer scope which isn't the global scope.
@@ -896,23 +898,23 @@ boolean referencesSuper() {
896898
* @param module The module where it was referenced.
897899
* @return Whether the name info has changed.
898900
*/
899-
boolean markReference(@Nullable JSChunk module) {
901+
boolean markReference(@Nullable JSChunk chunk) {
900902
boolean hasChanged = false;
901903
if (!referenced) {
902904
referenced = true;
903905
hasChanged = true;
904906
}
905907

906-
JSChunk originalDeepestCommon = deepestCommonModuleRef;
908+
JSChunk originalDeepestCommon = deepestCommonChunkRef;
907909

908-
if (deepestCommonModuleRef == null) {
909-
deepestCommonModuleRef = module;
910+
if (deepestCommonChunkRef == null) {
911+
deepestCommonChunkRef = chunk;
910912
} else {
911-
deepestCommonModuleRef =
912-
moduleGraph.getDeepestCommonDependencyInclusive(deepestCommonModuleRef, module);
913+
deepestCommonChunkRef =
914+
chunkGraph.getDeepestCommonDependencyInclusive(deepestCommonChunkRef, chunk);
913915
}
914916

915-
if (originalDeepestCommon != deepestCommonModuleRef) {
917+
if (originalDeepestCommon != deepestCommonChunkRef) {
916918
hasChanged = true;
917919
}
918920

@@ -921,7 +923,7 @@ boolean markReference(@Nullable JSChunk module) {
921923

922924
/** Returns the deepest common module of all the references to this property. */
923925
JSChunk getDeepestCommonModuleRef() {
924-
return deepestCommonModuleRef;
926+
return deepestCommonChunkRef;
925927
}
926928

927929
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ String callName() {
221221
this.compiler = compiler;
222222
this.checker = new Checker(compiler, moduleMetadataMap);
223223
this.namespacesSeen = new HashSet<>();
224-
this.chunkGraph = compiler.getModuleGraph();
224+
this.chunkGraph = compiler.getChunkGraph();
225225
}
226226

227227
@Override

0 commit comments

Comments
 (0)