Skip to content

Commit

Permalink
Automated g4 rollback of changelist 186667476.
Browse files Browse the repository at this point in the history
*** Original change description ***

Update DepsGenerator to support goog.require's with es6 modules:

- goog.require(path) is now allowed in goog.modules for ES6 modules.
- ES6 modules no longer provide any symbols in a deps file.
- Requires for ES6 modules in a deps file will be their relative path to Closure rather than a munged symbol.

***

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=186689028
  • Loading branch information
johnplaisted authored and dimvar committed Feb 25, 2018
1 parent 06a8747 commit 2b9c8b8
Show file tree
Hide file tree
Showing 20 changed files with 380 additions and 901 deletions.
4 changes: 2 additions & 2 deletions src/com/google/javascript/jscomp/Compiler.java
Expand Up @@ -1807,7 +1807,7 @@ Node parseInputs() {
// In this case we must force module rewriting to occur on the imported file // In this case we must force module rewriting to occur on the imported file
Map<String, CompilerInput> inputsToRewrite = new HashMap<>(); Map<String, CompilerInput> inputsToRewrite = new HashMap<>();
for (CompilerInput input : inputs) { for (CompilerInput input : inputs) {
for (String require : input.getKnownRequiredSymbols()) { for (String require : input.getKnownRequires()) {
if (inputModuleIdentifiers.containsKey(require) if (inputModuleIdentifiers.containsKey(require)
&& !inputsToRewrite.containsKey(require)) { && !inputsToRewrite.containsKey(require)) {
inputsToRewrite.put(require, inputModuleIdentifiers.get(require)); inputsToRewrite.put(require, inputModuleIdentifiers.get(require));
Expand Down Expand Up @@ -2015,7 +2015,7 @@ private List<CompilerInput> depthFirstDependenciesFromInput(
this.moduleTypesByName.put(input.getPath().toModuleName(), input.getJsModuleType()); this.moduleTypesByName.put(input.getPath().toModuleName(), input.getJsModuleType());


ArrayList<String> allDeps = new ArrayList<>(); ArrayList<String> allDeps = new ArrayList<>();
allDeps.addAll(input.getRequiredSymbols()); allDeps.addAll(input.getRequires());
allDeps.addAll(input.getDynamicRequires()); allDeps.addAll(input.getDynamicRequires());
for (String requiredNamespace : allDeps) { for (String requiredNamespace : allDeps) {
CompilerInput requiredInput = null; CompilerInput requiredInput = null;
Expand Down
51 changes: 27 additions & 24 deletions src/com/google/javascript/jscomp/CompilerInput.java
Expand Up @@ -40,12 +40,12 @@
import java.util.TreeMap; import java.util.TreeMap;


/** /**
* A class for the internal representation of an input to the compiler. Wraps a {@link SourceAst} * A class for the internal representation of an input to the compiler.
* and maintain state such as module for the input and whether the input is an extern. Also * Wraps a {@link SourceAst} and maintain state such as module for the input and
* calculates provided and required types. * whether the input is an extern. Also calculates provided and required types.
* *
*/ */
public class CompilerInput extends DependencyInfo.Base implements SourceAst { public class CompilerInput implements SourceAst, DependencyInfo {


private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;


Expand All @@ -58,9 +58,9 @@ public class CompilerInput extends DependencyInfo.Base implements SourceAst {


// DependencyInfo to delegate to. // DependencyInfo to delegate to.
private DependencyInfo dependencyInfo; private DependencyInfo dependencyInfo;
private final List<Require> extraRequires = new ArrayList<>(); private final List<String> extraRequires = new ArrayList<>();
private final List<String> extraProvides = new ArrayList<>(); private final List<String> extraProvides = new ArrayList<>();
private final List<Require> orderedRequires = new ArrayList<>(); private final List<String> orderedRequires = new ArrayList<>();
private final List<String> dynamicRequires = new ArrayList<>(); private final List<String> dynamicRequires = new ArrayList<>();
private boolean hasFullParseDependencyInfo = false; private boolean hasFullParseDependencyInfo = false;
private ModuleType jsModuleType = ModuleType.NONE; private ModuleType jsModuleType = ModuleType.NONE;
Expand Down Expand Up @@ -162,7 +162,7 @@ public void setCompiler(AbstractCompiler compiler) {


/** Gets a list of types depended on by this input. */ /** Gets a list of types depended on by this input. */
@Override @Override
public ImmutableList<Require> getRequires() { public ImmutableList<String> getRequires() {
if (hasFullParseDependencyInfo) { if (hasFullParseDependencyInfo) {
return ImmutableList.copyOf(orderedRequires); return ImmutableList.copyOf(orderedRequires);
} }
Expand All @@ -176,16 +176,14 @@ public ImmutableList<String> getWeakRequires() {
} }


/** /**
* Gets a list of namespaces and paths depended on by this input, but does not attempt to * Gets a list of types depended on by this input,
* regenerate the dependency information. Typically this occurs from module rewriting. * but does not attempt to regenerate the dependency information.
* Typically this occurs from module rewriting.
*/ */
ImmutableCollection<Require> getKnownRequires() { ImmutableCollection<String> getKnownRequires() {
return concat( return concat(
dependencyInfo != null ? dependencyInfo.getRequires() : ImmutableList.of(), extraRequires); dependencyInfo != null ? dependencyInfo.getRequires() : ImmutableList.<String>of(),
} extraRequires);

ImmutableList<String> getKnownRequiredSymbols() {
return Require.asSymbolList(getKnownRequires());
} }


/** Gets a list of types provided by this input. */ /** Gets a list of types provided by this input. */
Expand Down Expand Up @@ -214,7 +212,7 @@ public void addProvide(String provide) {
} }


/** Registers a type that this input depends on in the order seen in the file. */ /** Registers a type that this input depends on in the order seen in the file. */
public boolean addOrderedRequire(Require require) { public boolean addOrderedRequire(String require) {
if (!orderedRequires.contains(require)) { if (!orderedRequires.contains(require)) {
orderedRequires.add(require); orderedRequires.add(require);
return true; return true;
Expand Down Expand Up @@ -257,7 +255,7 @@ public void setJsModuleType(ModuleType moduleType) {
} }


/** Registers a type that this input depends on. */ /** Registers a type that this input depends on. */
public void addRequire(Require require) { public void addRequire(String require) {
extraRequires.add(require); extraRequires.add(require);
} }


Expand Down Expand Up @@ -343,7 +341,7 @@ private DependencyInfo generateDependencyInfo() {
private static class DepsFinder { private static class DepsFinder {
private final Map<String, String> loadFlags = new TreeMap<>(); private final Map<String, String> loadFlags = new TreeMap<>();
private final List<String> provides = new ArrayList<>(); private final List<String> provides = new ArrayList<>();
private final List<Require> requires = new ArrayList<>(); private final List<String> requires = new ArrayList<>();
private final ModulePath modulePath; private final ModulePath modulePath;


DepsFinder(ModulePath modulePath) { DepsFinder(ModulePath modulePath) {
Expand Down Expand Up @@ -371,13 +369,14 @@ void visitSubtree(Node n, Node parent) {
&& n.getFirstChild().isGetProp() && n.getFirstChild().isGetProp()
&& n.getFirstFirstChild().matchesQualifiedName("goog")) { && n.getFirstFirstChild().matchesQualifiedName("goog")) {


if (!requires.contains(Require.BASE)) { if (!requires.contains("goog")) {
requires.add(Require.BASE); requires.add("goog");
} }


Node callee = n.getFirstChild(); Node callee = n.getFirstChild();
Node argument = n.getLastChild(); Node argument = n.getLastChild();
switch (callee.getLastChild().getString()) { switch (callee.getLastChild().getString()) {

case "module": case "module":
loadFlags.put("module", "goog"); loadFlags.put("module", "goog");
// Fall-through // Fall-through
Expand All @@ -392,7 +391,7 @@ void visitSubtree(Node n, Node parent) {
if (!argument.isString()) { if (!argument.isString()) {
return; return;
} }
requires.add(Require.googRequireSymbol(argument.getString())); requires.add(argument.getString());
return; return;


case "loadModule": case "loadModule":
Expand Down Expand Up @@ -457,8 +456,7 @@ void visitEs6ModuleName(Node n, Node parent) {
// into ModuleLoader. // into ModuleLoader.
String moduleName = n.getString(); String moduleName = n.getString();
if (moduleName.startsWith("goog:")) { if (moduleName.startsWith("goog:")) {
// cut off the "goog:" prefix requires.add(moduleName.substring(5)); // cut off the "goog:" prefix
requires.add(Require.googRequireSymbol(moduleName.substring(5)));
return; return;
} }
ModulePath importedModule = ModulePath importedModule =
Expand All @@ -469,7 +467,7 @@ void visitEs6ModuleName(Node n, Node parent) {
importedModule = modulePath.resolveModuleAsPath(moduleName); importedModule = modulePath.resolveModuleAsPath(moduleName);
} }


requires.add(Require.es6Import(importedModule.toModuleName(), n.getString())); requires.add(importedModule.toModuleName());
} }
} }


Expand Down Expand Up @@ -527,6 +525,11 @@ public ImmutableMap<String, String> getLoadFlags() {
return getDependencyInfo().getLoadFlags(); return getDependencyInfo().getLoadFlags();
} }


@Override
public boolean isModule() {
return "goog".equals(getLoadFlags().get("module"));
}

private static <T> ImmutableSet<T> concat(Iterable<T> first, Iterable<T> second) { private static <T> ImmutableSet<T> concat(Iterable<T> first, Iterable<T> second) {
return ImmutableSet.<T>builder().addAll(first).addAll(second).build(); return ImmutableSet.<T>builder().addAll(first).addAll(second).build();
} }
Expand Down
15 changes: 7 additions & 8 deletions src/com/google/javascript/jscomp/FindModuleDependencies.java
Expand Up @@ -21,7 +21,6 @@
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.javascript.jscomp.CompilerInput.ModuleType; import com.google.javascript.jscomp.CompilerInput.ModuleType;
import com.google.javascript.jscomp.Es6RewriteModules.FindGoogProvideOrGoogModule; import com.google.javascript.jscomp.Es6RewriteModules.FindGoogProvideOrGoogModule;
import com.google.javascript.jscomp.deps.DependencyInfo.Require;
import com.google.javascript.jscomp.deps.ModuleLoader; import com.google.javascript.jscomp.deps.ModuleLoader;
import com.google.javascript.rhino.Node; import com.google.javascript.rhino.Node;
import com.google.javascript.rhino.Token; import com.google.javascript.rhino.Token;
Expand Down Expand Up @@ -74,8 +73,8 @@ public void process(Node root) {
// and add "goog" as a dependency. If "goog" is a dependency of the // and add "goog" as a dependency. If "goog" is a dependency of the
// file we add it here to the ordered requires so that it's always // file we add it here to the ordered requires so that it's always
// first. // first.
if (input.getRequires().contains(Require.BASE)) { if (input.getRequires().contains("goog")) {
input.addOrderedRequire(Require.BASE); input.addOrderedRequire("goog");
} }


NodeTraversal.traverseEs6(compiler, root, this); NodeTraversal.traverseEs6(compiler, root, this);
Expand Down Expand Up @@ -160,7 +159,7 @@ public void visit(NodeTraversal t, Node n, Node parent) {
.matchesQualifiedName("__webpack_require__.e"))) { .matchesQualifiedName("__webpack_require__.e"))) {
t.getInput().addDynamicRequire(modulePath.toModuleName()); t.getInput().addDynamicRequire(modulePath.toModuleName());
} else { } else {
t.getInput().addOrderedRequire(Require.commonJs(modulePath.toModuleName(), path)); t.getInput().addOrderedRequire(modulePath.toModuleName());
} }
} }
} }
Expand All @@ -176,9 +175,9 @@ public void visit(NodeTraversal t, Node n, Node parent) {
&& n.getSecondChild().isString()) { && n.getSecondChild().isString()) {
String namespace = n.getSecondChild().getString(); String namespace = n.getSecondChild().getString();
if (namespace.startsWith("goog.")) { if (namespace.startsWith("goog.")) {
t.getInput().addOrderedRequire(Require.BASE); t.getInput().addOrderedRequire("goog");
} }
t.getInput().addOrderedRequire(Require.googRequireSymbol(namespace)); t.getInput().addOrderedRequire(namespace);
} }
} }


Expand All @@ -198,9 +197,9 @@ public void exitScope(NodeTraversal t) {
private void addEs6ModuleImportToGraph(NodeTraversal t, Node n) { private void addEs6ModuleImportToGraph(NodeTraversal t, Node n) {
String moduleName = getEs6ModuleNameFromImportNode(t, n); String moduleName = getEs6ModuleNameFromImportNode(t, n);
if (moduleName.startsWith("goog.")) { if (moduleName.startsWith("goog.")) {
t.getInput().addOrderedRequire(Require.BASE); t.getInput().addOrderedRequire("goog");
} }
t.getInput().addOrderedRequire(Require.es6Import(moduleName, n.getLastChild().getString())); t.getInput().addOrderedRequire(moduleName);
} }


/** /**
Expand Down
12 changes: 6 additions & 6 deletions src/com/google/javascript/jscomp/JSModule.java
Expand Up @@ -35,11 +35,11 @@
import java.util.Set; import java.util.Set;


/** /**
* A JavaScript module has a unique name, consists of a list of compiler inputs, and can depend on * A JavaScript module has a unique name, consists of a list of compiler inputs,
* other modules. * and can depend on other modules.
* *
*/ */
public final class JSModule extends DependencyInfo.Base implements Serializable { public final class JSModule implements DependencyInfo, Serializable {
private static final long serialVersionUID = 1; private static final long serialVersionUID = 1;


/** Module name */ /** Module name */
Expand Down Expand Up @@ -86,10 +86,10 @@ public ImmutableList<String> getProvides() {
} }


@Override @Override
public ImmutableList<Require> getRequires() { public ImmutableList<String> getRequires() {
ImmutableList.Builder<Require> builder = ImmutableList.builder(); ImmutableList.Builder<String> builder = ImmutableList.builder();
for (JSModule m : deps) { for (JSModule m : deps) {
builder.add(Require.compilerModule(m.getName())); builder.add(m.getName());
} }
return builder.build(); return builder.build();
} }
Expand Down
2 changes: 1 addition & 1 deletion src/com/google/javascript/jscomp/JSModuleGraph.java
Expand Up @@ -532,7 +532,7 @@ private List<CompilerInput> getDepthFirstDependenciesOf(
return orderedInputs; return orderedInputs;
} }


for (String importedNamespace : rootInput.getRequiredSymbols()) { for (String importedNamespace : rootInput.getRequires()) {
CompilerInput dependency = null; CompilerInput dependency = null;
if (inputsByProvide.containsKey(importedNamespace) if (inputsByProvide.containsKey(importedNamespace)
&& unreachedInputs.contains(inputsByProvide.get(importedNamespace))) { && unreachedInputs.contains(inputsByProvide.get(importedNamespace))) {
Expand Down
13 changes: 10 additions & 3 deletions src/com/google/javascript/jscomp/LazyParsedDependencyInfo.java
Expand Up @@ -27,8 +27,10 @@
import java.util.Map; import java.util.Map;
import java.util.TreeMap; import java.util.TreeMap;


/** A DependencyInfo class that determines load flags by parsing the AST just-in-time. */ /**
public class LazyParsedDependencyInfo extends DependencyInfo.Base { * A DependencyInfo class that determines load flags by parsing the AST just-in-time.
*/
public class LazyParsedDependencyInfo implements DependencyInfo {


private final DependencyInfo delegate; private final DependencyInfo delegate;
private final JsAst ast; private final JsAst ast;
Expand Down Expand Up @@ -75,7 +77,7 @@ public String getPathRelativeToClosureBase() {
} }


@Override @Override
public ImmutableList<Require> getRequires() { public ImmutableList<String> getRequires() {
return delegate.getRequires(); return delegate.getRequires();
} }


Expand All @@ -88,4 +90,9 @@ public ImmutableList<String> getWeakRequires() {
public ImmutableList<String> getProvides() { public ImmutableList<String> getProvides() {
return delegate.getProvides(); return delegate.getProvides();
} }

@Override
public boolean isModule() {
return "goog".equals(getLoadFlags().get("module"));
}
} }
Expand Up @@ -131,7 +131,7 @@ private void addDependency(String symbol, Set<String> seen, List<String> list)
} }
} else if (!seen.containsAll(dependency.getProvides())) { } else if (!seen.containsAll(dependency.getProvides())) {
seen.addAll(dependency.getProvides()); seen.addAll(dependency.getProvides());
for (String require : dependency.getRequiredSymbols()) { for (String require : dependency.getRequires()) {
addDependency(require, seen, list); addDependency(require, seen, list);
} }
list.add(dependency.getPathRelativeToClosureBase()); list.add(dependency.getPathRelativeToClosureBase());
Expand All @@ -148,7 +148,7 @@ private static Collection<String> parseRequires(String code, boolean addClosureB
if (addClosureBase) { if (addClosureBase) {
requires.add(CLOSURE_BASE_PROVIDE); requires.add(CLOSURE_BASE_PROVIDE);
} }
requires.addAll(deps.getRequiredSymbols()); requires.addAll(deps.getRequires());
errorManager.generateReport(); errorManager.generateReport();
return requires; return requires;
} }
Expand Down

0 comments on commit 2b9c8b8

Please sign in to comment.