Skip to content

Commit

Permalink
Use consistent naming for JSChunk identifiers in JSCompiler.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
rishipal authored and copybara-github committed Sep 5, 2023
1 parent 454e6c8 commit d04cb02
Show file tree
Hide file tree
Showing 40 changed files with 931 additions and 961 deletions.
177 changes: 88 additions & 89 deletions src/com/google/javascript/jscomp/AbstractCommandLineRunner.java

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/com/google/javascript/jscomp/AbstractCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ void afterPass(String passName) {}
public abstract @Nullable Node getScriptNode(String filename);

/** Gets the module graph. */
abstract @Nullable JSChunkGraph getModuleGraph();
abstract @Nullable JSChunkGraph getChunkGraph();

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

abstract TypeValidator getTypeValidator();

Expand Down
32 changes: 14 additions & 18 deletions src/com/google/javascript/jscomp/AliasStrings.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class AliasStrings implements CompilerPass, NodeTraversal.Callback {

private final AbstractCompiler compiler;

private final JSChunkGraph moduleGraph;
private final JSChunkGraph chunkGraph;

private final boolean outputStringUsage;

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

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

/** package private. This value is AND-ed with the hash function to allow
* unit tests to reduce the range of hash values to test collision cases */
Expand All @@ -82,18 +82,18 @@ class AliasStrings implements CompilerPass, NodeTraversal.Callback {
* Creates an instance.
*
* @param compiler The compiler
* @param moduleGraph The module graph, or null if there are no modules
* @param chunkGraph The module graph, or null if there are no modules
* @param outputStringUsage Outputs all strings and the number of times they were used in the
* application to the server log.
* @param aliasStringsMode The alias strings policy set to either ALL or LARGE
*/
AliasStrings(
AbstractCompiler compiler,
JSChunkGraph moduleGraph,
JSChunkGraph chunkGraph,
boolean outputStringUsage,
AliasStringsMode aliasStringsMode) {
this.compiler = compiler;
this.moduleGraph = moduleGraph;
this.chunkGraph = chunkGraph;
this.outputStringUsage = outputStringUsage;
this.aliasStringsMode = aliasStringsMode;
}
Expand Down Expand Up @@ -154,26 +154,22 @@ public void visit(NodeTraversal t, Node n, Node parent) {

info.occurrences.add(occurrence);

// The current module.
JSChunk module = t.getChunk();
// The current chunk.
JSChunk chunk = t.getChunk();
if (info.occurrences.size() != 1) {
// Check whether the current module depends on the module containing
// the declaration.
if (module != null
&& info.moduleToContainDecl != null
&& module != info.moduleToContainDecl) {
if (chunk != null && info.chunkToContainDecl != null && chunk != info.chunkToContainDecl) {
// We need to declare this string in the deepest module in the
// module dependency graph that both of these modules depend on.
module =
moduleGraph.getDeepestCommonDependencyInclusive(module, info.moduleToContainDecl);
chunk = chunkGraph.getDeepestCommonDependencyInclusive(chunk, info.chunkToContainDecl);
} else {
// use the previously saved insertion location.
return;
}
}
Node varParent =
moduleVarParentMap.computeIfAbsent(module, compiler::getNodeForCodeInsertion);
info.moduleToContainDecl = module;
Node varParent = chunkVarParentMap.computeIfAbsent(chunk, compiler::getNodeForCodeInsertion);
info.chunkToContainDecl = chunk;
info.parentForNewVarDecl = varParent;
info.siblingToInsertVarDeclBefore = varParent.getFirstChild();
}
Expand Down Expand Up @@ -302,7 +298,7 @@ private final class StringInfo {

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

JSChunk moduleToContainDecl;
JSChunk chunkToContainDecl;
Node parentForNewVarDecl;
Node siblingToInsertVarDeclBefore;

Expand Down
92 changes: 47 additions & 45 deletions src/com/google/javascript/jscomp/AnalyzePrototypeProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class AnalyzePrototypeProperties implements CompilerPass {
private final boolean anchorUnusedVars;
private final boolean rootScopeUsesAreGlobal;

private final JSChunkGraph moduleGraph;
private final JSChunkGraph chunkGraph;
private final @Nullable JSChunk firstModule;

// Properties that are implicitly used as part of the JS language.
Expand Down Expand Up @@ -100,7 +100,7 @@ class AnalyzePrototypeProperties implements CompilerPass {
* Creates a new pass for analyzing prototype properties.
*
* @param compiler The compiler.
* @param moduleGraph The graph for resolving module dependencies.
* @param chunkGraph The graph for resolving module dependencies.
* @param canModifyExterns If true, then we can move prototype properties that are declared in the
* externs file.
* @param anchorUnusedVars If true, then we must mark all vars as referenced, even if they are
Expand All @@ -110,18 +110,18 @@ class AnalyzePrototypeProperties implements CompilerPass {
*/
AnalyzePrototypeProperties(
AbstractCompiler compiler,
JSChunkGraph moduleGraph,
JSChunkGraph chunkGraph,
boolean canModifyExterns,
boolean anchorUnusedVars,
boolean rootScopeUsesAreGlobal) {
this.compiler = compiler;
this.moduleGraph = moduleGraph;
this.chunkGraph = chunkGraph;
this.canModifyExterns = canModifyExterns;
this.anchorUnusedVars = anchorUnusedVars;
this.rootScopeUsesAreGlobal = rootScopeUsesAreGlobal;

if (moduleGraph.getChunkCount() > 1) {
firstModule = moduleGraph.getRootChunk();
if (chunkGraph.getChunkCount() > 1) {
firstModule = chunkGraph.getRootChunk();
} else {
firstModule = null;
}
Expand All @@ -133,11 +133,11 @@ class AnalyzePrototypeProperties implements CompilerPass {

for (String property : IMPLICITLY_USED_PROPERTIES) {
NameInfo nameInfo = getNameInfoForName(property, PROPERTY);
if (moduleGraph == null) {
if (chunkGraph == null) {
symbolGraph.connect(externNode, null, nameInfo);
} else {
for (JSChunk module : moduleGraph.getAllChunks()) {
symbolGraph.connect(externNode, module, nameInfo);
for (JSChunk chunk : chunkGraph.getAllChunks()) {
symbolGraph.connect(externNode, chunk, nameInfo);
}
}
}
Expand Down Expand Up @@ -402,7 +402,7 @@ public void visit(NodeTraversal t, Node n, Node parent) {
}
}

private void addSymbolUse(String name, JSChunk module, SymbolType type) {
private void addSymbolUse(String name, JSChunk chunk, SymbolType type) {
NameInfo info = getNameInfoForName(name, type);
NameInfo def = null;
// Skip all anonymous nodes. We care only about symbols with names.
Expand All @@ -413,7 +413,7 @@ private void addSymbolUse(String name, JSChunk module, SymbolType type) {
}
}
if (!def.equals(info)) {
symbolGraph.connect(def, module, info);
symbolGraph.connect(def, chunk, info);
}
}

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

private void addGlobalUseOfSymbol(String name, JSChunk module, SymbolType type) {
symbolGraph.connect(globalNode, module, getNameInfoForName(name, type));
private void addGlobalUseOfSymbol(String name, JSChunk chunk, SymbolType type) {
symbolGraph.connect(globalNode, chunk, getNameInfoForName(name, type));
}
}

Expand Down Expand Up @@ -623,7 +623,7 @@ private class PropagateReferences implements EdgeCallback<NameInfo, JSChunk> {
public boolean traverseEdge(NameInfo start, JSChunk edge, NameInfo dest) {
if (start.isReferenced()) {
JSChunk startModule = start.getDeepestCommonModuleRef();
if (startModule != null && moduleGraph.dependsOn(startModule, edge)) {
if (startModule != null && chunkGraph.dependsOn(startModule, edge)) {
return dest.markReference(startModule);
} else {
return dest.markReference(edge);
Expand All @@ -639,7 +639,7 @@ interface Symbol {
Var getRootVar();

/** Returns the module where this appears. */
JSChunk getModule();
JSChunk getChunk();
}

private enum SymbolType {
Expand All @@ -653,15 +653,15 @@ private enum SymbolType {
*/
static class GlobalFunction implements Symbol {
private final Var var;
private final JSChunk module;
private final JSChunk chunk;

GlobalFunction(Node nameNode, Var var, JSChunk module) {
GlobalFunction(Node nameNode, Var var, JSChunk chunk) {
Node parent = nameNode.getParent();
checkState(
(NodeUtil.isNameDeclaration(parent) && var.isGlobal())
|| NodeUtil.isFunctionDeclaration(parent));
this.var = var;
this.module = module;
this.chunk = chunk;
}

@Override
Expand All @@ -670,8 +670,8 @@ public Var getRootVar() {
}

@Override
public JSChunk getModule() {
return module;
public JSChunk getChunk() {
return chunk;
}
}

Expand All @@ -684,14 +684,14 @@ interface Property extends Symbol {}
static class ClassMemberFunction implements Property {
private final Node node;
private final Var var;
private final JSChunk module;
private final JSChunk chunk;

ClassMemberFunction(Node node, Var var, JSChunk module) {
ClassMemberFunction(Node node, Var var, JSChunk chunk) {
checkState(node.getParent().isClassMembers());
checkState(node.isMemberFunctionDef() || node.isSetterDef() || node.isGetterDef());
this.node = node;
this.var = var;
this.module = module;
this.chunk = chunk;
}

@Override
Expand All @@ -700,8 +700,8 @@ public Var getRootVar() {
}

@Override
public JSChunk getModule() {
return module;
public JSChunk getChunk() {
return chunk;
}

/** Returns the function node within the definition. */
Expand Down Expand Up @@ -751,13 +751,15 @@ interface PrototypeProperty extends Property {
static class AssignmentPrototypeProperty implements PrototypeProperty {
private final Node exprNode;
private final Var rootVar;
private final JSChunk module;
private final JSChunk chunk;

/** @param node An EXPR node. */
AssignmentPrototypeProperty(Node node, Var rootVar, JSChunk module) {
/**
* @param node An EXPR node.
*/
AssignmentPrototypeProperty(Node node, Var rootVar, JSChunk chunk) {
this.exprNode = node;
this.rootVar = rootVar;
this.module = module;
this.chunk = chunk;
}

@Override
Expand All @@ -780,8 +782,8 @@ private Node getAssignNode() {
}

@Override
public JSChunk getModule() {
return module;
public JSChunk getChunk() {
return chunk;
}
}

Expand All @@ -795,13 +797,13 @@ static class LiteralPrototypeProperty implements PrototypeProperty {
private final Node value;
private final Node assign;
private final Var rootVar;
private final JSChunk module;
private final JSChunk chunk;

LiteralPrototypeProperty(Node value, Node assign, Var rootVar, JSChunk module) {
LiteralPrototypeProperty(Node value, Node assign, Var rootVar, JSChunk chunk) {
this.value = value;
this.assign = assign;
this.rootVar = rootVar;
this.module = module;
this.chunk = chunk;
}

@Override
Expand All @@ -820,8 +822,8 @@ public Node getValue() {
}

@Override
public JSChunk getModule() {
return module;
public JSChunk getChunk() {
return chunk;
}
}

Expand Down Expand Up @@ -849,7 +851,7 @@ class NameInfo {

private boolean referenced = false;
private final Deque<Symbol> declarations = new ArrayDeque<>();
private @Nullable JSChunk deepestCommonModuleRef = null;
private @Nullable JSChunk deepestCommonChunkRef = null;

// True if this property is a function that reads a variable from an
// outer scope which isn't the global scope.
Expand Down Expand Up @@ -896,23 +898,23 @@ boolean referencesSuper() {
* @param module The module where it was referenced.
* @return Whether the name info has changed.
*/
boolean markReference(@Nullable JSChunk module) {
boolean markReference(@Nullable JSChunk chunk) {
boolean hasChanged = false;
if (!referenced) {
referenced = true;
hasChanged = true;
}

JSChunk originalDeepestCommon = deepestCommonModuleRef;
JSChunk originalDeepestCommon = deepestCommonChunkRef;

if (deepestCommonModuleRef == null) {
deepestCommonModuleRef = module;
if (deepestCommonChunkRef == null) {
deepestCommonChunkRef = chunk;
} else {
deepestCommonModuleRef =
moduleGraph.getDeepestCommonDependencyInclusive(deepestCommonModuleRef, module);
deepestCommonChunkRef =
chunkGraph.getDeepestCommonDependencyInclusive(deepestCommonChunkRef, chunk);
}

if (originalDeepestCommon != deepestCommonModuleRef) {
if (originalDeepestCommon != deepestCommonChunkRef) {
hasChanged = true;
}

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

/** Returns the deepest common module of all the references to this property. */
JSChunk getDeepestCommonModuleRef() {
return deepestCommonModuleRef;
return deepestCommonChunkRef;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/com/google/javascript/jscomp/CheckClosureImports.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ String callName() {
this.compiler = compiler;
this.checker = new Checker(compiler, moduleMetadataMap);
this.namespacesSeen = new HashSet<>();
this.chunkGraph = compiler.getModuleGraph();
this.chunkGraph = compiler.getChunkGraph();
}

@Override
Expand Down
Loading

0 comments on commit d04cb02

Please sign in to comment.