Skip to content

Commit

Permalink
Allow goog.module.get inside of goog.module files.
Browse files Browse the repository at this point in the history
Previously, only the specific pattern of:
    let x = goog.forwardDeclare();

    function f() {
      x = goog.module.get();
    }
was supported, but goog.module.get can be generally useful without creating
the alias.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=154216580
  • Loading branch information
blickly authored and Tyler Breisacher committed Apr 25, 2017
1 parent 43754fc commit d2e98cb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 16 deletions.
32 changes: 16 additions & 16 deletions src/com/google/javascript/jscomp/ClosureRewriteModule.java
Expand Up @@ -816,8 +816,9 @@ private void recordGoogModuleGet(NodeTraversal t, Node call) {
}

String aliasName = null;
boolean isFillingAnAlias =
call.getParent().isAssign() && call.getParent().getFirstChild().isName();
Node maybeAssign = call.getParent();
boolean isFillingAnAlias = maybeAssign.isAssign() && maybeAssign.getFirstChild().isName()
&& maybeAssign.getParent().isExprResult();
if (isFillingAnAlias && currentScript.isModule) {
aliasName = call.getParent().getFirstChild().getString();

Expand All @@ -839,6 +840,12 @@ private void recordGoogModuleGet(NodeTraversal t, Node call) {
t.report(call, INVALID_GET_ALIAS);
return;
}

// Each goog.module.get() calling filling an alias will have the alias importing logic
// handed at the goog.forwardDeclare call, and the corresponding goog.module.get can simply
// be removed.
compiler.reportChangeToEnclosingScope(maybeAssign);
maybeAssign.getParent().detach();
}
}

Expand Down Expand Up @@ -1110,20 +1117,13 @@ private void updateGoogModuleGetCall(Node call) {
String legacyNamespace = legacyNamespaceNode.getString();

compiler.reportChangeToEnclosingScope(call);
if (currentScript.isModule) {
// In a module each goog.module.get() will have a paired goog.forwardDeclare() and the module
// alias importing will be handled there so that the alias is available in the entire module
// scope.
NodeUtil.getEnclosingStatement(call).detach();
} else {
// In a non-module script goog.module.get() is used inside of a goog.scope() and it's
// imported alias need only be made available within that scope.
// Replace "goog.module.get('pkg.Foo');" with "module$exports$pkg$Foo;".
String exportedNamespace = rewriteState.getExportedNamespaceOrScript(legacyNamespace);
Node exportedNamespaceName = NodeUtil.newQName(compiler, exportedNamespace).srcrefTree(call);
exportedNamespaceName.putProp(Node.ORIGINALNAME_PROP, legacyNamespace);
call.replaceWith(exportedNamespaceName);
}
// Remaining calls to goog.module.get() are not alias updates,
// and should be replaced by a reference to the proper name.
// Replace "goog.module.get('pkg.Foo')" with either "pkg.Foo" or "module$exports$pkg$Foo".
String exportedNamespace = rewriteState.getExportedNamespaceOrScript(legacyNamespace);
Node exportedNamespaceName = NodeUtil.newQName(compiler, exportedNamespace).srcrefTree(call);
exportedNamespaceName.putProp(Node.ORIGINALNAME_PROP, legacyNamespace);
call.replaceWith(exportedNamespaceName);
}

private void recordExportsPropertyAssignment(NodeTraversal t, Node getpropNode) {
Expand Down
22 changes: 22 additions & 0 deletions test/com/google/javascript/jscomp/ClosureRewriteModuleTest.java
Expand Up @@ -932,6 +932,28 @@ public void testGoogModuleGet2() {
"}")});
}

public void testGoogModuleGet3() {
testEs6(
new String[] {
LINE_JOINER.join(
"goog.module('a.b.c');",
"exports = class {};"),
LINE_JOINER.join(
"goog.module('x.y.z');",
"",
"function f() {",
" return new (goog.module.get('a.b.c'))();",
"}")},

new String[] {
"/** @const */ var module$exports$a$b$c = class {};",
LINE_JOINER.join(
"/** @const */ var module$exports$x$y$z = {};",
"function module$contents$x$y$z_f() {",
" return new module$exports$a$b$c();",
"}")});
}

public void testAliasedGoogModuleGet1() {
test(
new String[] {
Expand Down

0 comments on commit d2e98cb

Please sign in to comment.