Skip to content

Commit

Permalink
Make sure that module rewriting works the same for goog.loadModule.
Browse files Browse the repository at this point in the history
In particular, before we were getting extraneous destrucutring import
not allowed here errors in the goog.loadModule case because those style
modules are rewritten later in this pass.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=139814379
  • Loading branch information
blickly committed Nov 21, 2016
1 parent c4a1040 commit b5620d3
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 30 deletions.
4 changes: 2 additions & 2 deletions src/com/google/javascript/jscomp/ClosureRewriteModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -1086,9 +1086,9 @@ private void recordExportsPropertyAssignment(NodeTraversal t, Node getpropNode)
Node exportsNameNode = getpropNode.getFirstChild();
Preconditions.checkState(exportsNameNode.getString().equals("exports"));

// Would be t.inModuleScope() if this ran before the inlineModuleIntoGlobal() call
// Would be just t.inModuleScope() if this ran before the inlineModuleIntoGlobal() call
// that happens at the beginning of module rewriting.
if (t.inGlobalScope()) {
if (t.inModuleScope() || t.inGlobalScope()) {
String exportName = getpropNode.getLastChild().getString();
currentScript.namedExports.add(exportName);
}
Expand Down
148 changes: 120 additions & 28 deletions test/com/google/javascript/jscomp/ClosureRewriteModuleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import static com.google.javascript.jscomp.ClosureRewriteModule.LATE_PROVIDE_ERROR;
import static com.google.javascript.jscomp.ClosureRewriteModule.QUALIFIED_REFERENCE_TO_GOOG_MODULE;

import com.google.javascript.jscomp.CompilerOptions.LanguageMode;

/**
* Unit tests for ClosureRewriteModule
* @author johnlenz@google.com (John Lenz)
Expand Down Expand Up @@ -594,34 +596,64 @@ public void testBundle5() {
"var module$contents$xid_xid = xid"));
}

// public void testBundle6() {
// test(
// LINE_JOINER.join(
// "goog.loadModule(function(exports) {",
// " goog.module('goog.asserts');",
// " return exports;",
// "});",
// "goog.loadModule(function(exports) {",
// " 'use strict';",
// " goog.module('xid');",
// " goog.module.declareLegacyNamespace();",
// " var asserts = goog.require('goog.asserts');",
// " var xid = function(id) {",
// " return xid.internal_(id);",
// " };",
// " xid.internal_ = function(id) {};",
// " exports = xid;",
// " return exports;",
// "});"),
// LINE_JOINER.join(
// "/** @const */ var module$exports$goog$asserts = {};",
// "goog.provide('xid');",
// "/** @const */ xid = function(id) {",
// " return xid.internal_(id);",
// "};",
// "xid.internal_ = function(id) {};",
// "var module$contents$xid_xid = xid"));
// }
public void testBundle6() {
test(
LINE_JOINER.join(
"goog.loadModule(function(exports) {",
" goog.module('goog.asserts');",
" return exports;",
"});",
"goog.loadModule(function(exports) {",
" 'use strict';",
" goog.module('xid');",
" goog.module.declareLegacyNamespace();",
" var asserts = goog.require('goog.asserts');",
" var xid = function(id) {",
" return xid.internal_(id);",
" };",
" xid.internal_ = function(id) {};",
" exports = xid;",
" return exports;",
"});"),
LINE_JOINER.join(
"/** @const */ var module$exports$goog$asserts = {};",
"goog.provide('xid');",
"var module$contents$xid_xid = function(id) {",
" return module$contents$xid_xid.internal_(id);",
"};",
"module$contents$xid_xid.internal_ = function(id) {};",
"/** @const */ xid = module$contents$xid_xid "));
}

public void testBundleWithDestructuringImport() {
testEs6(
LINE_JOINER.join(
"goog.loadModule(function(exports) { 'use strict';",
" goog.module('mod_B');",
"",
" /** @interface */ function B(){}",
"",
" exports.B = B;",
" return exports;",
"});",
"goog.loadModule(function(exports) { 'use strict';",
" goog.module('mod_A');",
"",
" var {B} = goog.require('mod_B');",
"",
" /** @constructor @implements {B} */",
" function A() {}",
" return exports;",
"});"),
LINE_JOINER.join(
"/** @const */ var module$exports$mod_B = {};",
"/** @interface */ function module$contents$mod_B_B(){}",
"/** @const */ module$exports$mod_B.B = module$contents$mod_B_B;",
"",
"/** @const */ var module$exports$mod_A = {};",
"/** @constructor @implements {module$exports$mod_B.B} */",
"function module$contents$mod_A_A(){}"));
}

public void testGoogLoadModuleString() {
testSame("goog.loadModule(\"goog.module('a.b.c'); exports = class {};\");");
Expand Down Expand Up @@ -1901,4 +1933,64 @@ public void testRewriteGoogModuleAliasesWithPrototypeGets2() {
});
}

public void testIjsFileInExterns() {
allowExternsChanges(true);
test(
LINE_JOINER.join(
"/** @externs */",
"goog.module('mod_B');",
"",
"/** @interface */ function B(){}",
"",
"exports = B;"),
LINE_JOINER.join(
"goog.module('mod_A');",
"",
"var B = goog.require('mod_B');",
"",
"/** @constructor @implements {B} */",
"function A() {}"),
(String) null, null, null);

test(
LINE_JOINER.join(
"/** @externs */",
"goog.loadModule(function(exports) { 'use strict';",
" goog.module('mod_B');",
"",
" /** @interface */ function B(){}",
"",
" exports = B;",
" return exports;",
"});"),
LINE_JOINER.join(
"goog.module('mod_A');",
"",
"var B = goog.require('mod_B');",
"",
"/** @constructor @implements {B} */",
"function A() {}"),
(String) null, null, null);

setAcceptedLanguage(LanguageMode.ECMASCRIPT_NEXT);
test(
LINE_JOINER.join(
"/** @externs */",
"goog.loadModule(function(exports) { 'use strict';",
" goog.module('mod_B');",
"",
" /** @interface */ function B(){}",
"",
" exports.B = B;",
" return exports;",
"});"),
LINE_JOINER.join(
"goog.module('mod_A');",
"",
"var {B} = goog.require('mod_B');",
"",
"/** @constructor @implements {B} */",
"function A() {}"),
(String) null, null, null);
}
}

0 comments on commit b5620d3

Please sign in to comment.