Skip to content

Commit

Permalink
Minor cleanups:
Browse files Browse the repository at this point in the history
 * Rename isModuleFile to isGoogModuleFile to make it clear we're talking about goog.module not ES6 modules.
 * Add preconditions to make it clearer what's happening in ClosureRewriteModule.
 * Reword a comment to be more accurate/clear.
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=122275236
  • Loading branch information
tbreisacher authored and blickly committed May 13, 2016
1 parent f381437 commit 416984c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/com/google/javascript/jscomp/ClosureCheckModule.java
Expand Up @@ -101,7 +101,7 @@ public void hotSwapScript(Node scriptRoot, Node originalRoot) {
@Override @Override
public boolean shouldTraverse(NodeTraversal t, Node n, Node parent) { public boolean shouldTraverse(NodeTraversal t, Node n, Node parent) {
if (n.isScript()) { if (n.isScript()) {
if (NodeUtil.isModuleFile(n)) { if (NodeUtil.isGoogModuleFile(n)) {
n.putBooleanProp(Node.GOOG_MODULE, true); n.putBooleanProp(Node.GOOG_MODULE, true);
return true; return true;
} }
Expand Down
20 changes: 13 additions & 7 deletions src/com/google/javascript/jscomp/ClosureRewriteModule.java
Expand Up @@ -222,7 +222,11 @@ private final class ScriptDescription {
*/ */
boolean willCreateExportsObject; boolean willCreateExportsObject;
boolean hasCreatedExportObject; boolean hasCreatedExportObject;
Node rootNode; // For recognizing top level names. Changes when unwrapping goog.scope()s.
// The root of the module. The SCRIPT node (or for goog.loadModule, the body of the
// function) that contains the module contents. For recognizing top level names. Changes when
// unwrapping a goog.loadModule() call.
Node rootNode;


public void addChildScript(ScriptDescription childScript) { public void addChildScript(ScriptDescription childScript) {
childScripts.addLast(childScript); childScripts.addLast(childScript);
Expand All @@ -236,7 +240,7 @@ public ScriptDescription removeFirstChildScript() {
private class ScriptRecorder extends AbstractPreOrderCallback { private class ScriptRecorder extends AbstractPreOrderCallback {
@Override @Override
public boolean shouldTraverse(NodeTraversal t, Node n, Node parent) { public boolean shouldTraverse(NodeTraversal t, Node n, Node parent) {
if (NodeUtil.isModuleFile(n)) { if (NodeUtil.isGoogModuleFile(n)) {
checkAndSetStrictModeDirective(t, n); checkAndSetStrictModeDirective(t, n);
} }


Expand Down Expand Up @@ -316,7 +320,7 @@ public boolean shouldTraverse(NodeTraversal t, Node n, Node parent) {
updateGoogForwardDeclare(t, n); updateGoogForwardDeclare(t, n);
} }
if (isCallTo(n, "goog.module.get")) { if (isCallTo(n, "goog.module.get")) {
updateGoogModuleGetCall(t, n); updateGoogModuleGetCall(n);
} }
break; break;


Expand Down Expand Up @@ -352,7 +356,7 @@ public boolean shouldTraverse(NodeTraversal t, Node n, Node parent) {
public void visit(NodeTraversal t, Node n, Node parent) { public void visit(NodeTraversal t, Node n, Node parent) {
switch (n.getType()) { switch (n.getType()) {
case Token.SCRIPT: case Token.SCRIPT:
updateEndScript(n); updateEndScript();
break; break;
} }
} }
Expand Down Expand Up @@ -889,7 +893,7 @@ private void updateGoogForwardDeclare(NodeTraversal t, Node call) {
updateGoogRequire(t, call); updateGoogRequire(t, call);
} }


private void updateGoogModuleGetCall(NodeTraversal t, Node call) { private void updateGoogModuleGetCall(Node call) {
Node legacyNamespaceNode = call.getSecondChild(); Node legacyNamespaceNode = call.getSecondChild();
String legacyNamespace = legacyNamespaceNode.getString(); String legacyNamespace = legacyNamespaceNode.getString();


Expand All @@ -915,9 +919,11 @@ private void updateExportsPropertyAssignment(Node getpropNode) {
} }


Node parent = getpropNode.getParent(); Node parent = getpropNode.getParent();
Preconditions.checkState(parent.isAssign() || parent.isExprResult(), parent);


// Update "exports.foo = Foo" to "module$exports$pkg$Foo.foo = Foo"; // Update "exports.foo = Foo" to "module$exports$pkg$Foo.foo = Foo";
Node exportsNameNode = parent.getFirstFirstChild(); Node exportsNameNode = getpropNode.getFirstChild();
Preconditions.checkState(exportsNameNode.getString().equals("exports"));
safeSetString(exportsNameNode, currentScript.binaryNamespace); safeSetString(exportsNameNode, currentScript.binaryNamespace);


Node jsdocNode = parent.isAssign() ? parent : getpropNode; Node jsdocNode = parent.isAssign() ? parent : getpropNode;
Expand Down Expand Up @@ -1094,7 +1100,7 @@ private void updateModuleReturn(Node returnNode) {
popScript(); popScript();
} }


private void updateEndScript(Node scriptNode) { private void updateEndScript() {
if (!currentScript.isModule) { if (!currentScript.isModule) {
return; return;
} }
Expand Down
2 changes: 1 addition & 1 deletion src/com/google/javascript/jscomp/NodeUtil.java
Expand Up @@ -4397,7 +4397,7 @@ private static boolean isGoogModuleCall(Node n) {
/** /**
* @return Whether the node is a goog.module file's SCRIPT node. * @return Whether the node is a goog.module file's SCRIPT node.
*/ */
static boolean isModuleFile(Node n) { static boolean isGoogModuleFile(Node n) {
return n.isScript() && n.hasChildren() && isGoogModuleCall(n.getFirstChild()); return n.isScript() && n.hasChildren() && isGoogModuleCall(n.getFirstChild());
} }


Expand Down
Expand Up @@ -41,7 +41,7 @@ public void process(Node externs, Node root) {


@Override @Override
public void hotSwapScript(Node scriptRoot, Node originalRoot) { public void hotSwapScript(Node scriptRoot, Node originalRoot) {
if (!NodeUtil.isModuleFile(scriptRoot)) { if (!NodeUtil.isGoogModuleFile(scriptRoot)) {
return; return;
} }


Expand Down

0 comments on commit 416984c

Please sign in to comment.