Skip to content

Commit

Permalink
Fix a crash in ScopedAliases when run in type-only summary generation…
Browse files Browse the repository at this point in the history
… mode

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=187673532
  • Loading branch information
blickly authored and Tyler Breisacher committed Mar 3, 2018
1 parent b4d8204 commit 6797f5b
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 8 deletions.
33 changes: 26 additions & 7 deletions src/com/google/javascript/jscomp/ScopedAliases.java
Expand Up @@ -221,21 +221,40 @@ public boolean referencesOtherAlias() {
public abstract void applyAlias(AbstractCompiler compiler); public abstract void applyAlias(AbstractCompiler compiler);
} }


private static boolean isValidAliasRhs(Node rhs) {
switch (rhs.getToken()) {
case GETPROP:
return isValidAliasRhs(rhs.getFirstChild());
case NAME:
return true;
case CALL:
return NodeUtil.isCallTo(rhs, "goog.module.get");
default:
return false;
}
}

private static boolean isAliasDefinition(Node nameNode) { private static boolean isAliasDefinition(Node nameNode) {
if (!nameNode.hasChildren()) { if (!nameNode.hasChildren()) {
return false; return false;
} }
Node rhs = nameNode.getLastChild(); Node rhs = nameNode.getLastChild();
return rhs.isQualifiedName() || NodeUtil.isCallTo(rhs, "goog.module.get"); return isValidAliasRhs(rhs);
} }


private static String getAliasedNamespace(Node aliasDefinition) { private static String getAliasedNamespace(Node rhs) {
if (aliasDefinition.isQualifiedName()) { switch (rhs.getToken()) {
return aliasDefinition.getQualifiedName(); case GETPROP:
return getAliasedNamespace(rhs.getFirstChild()) + '.' + rhs.getLastChild().getString();
case NAME:
return rhs.getString();
case CALL:
checkState(NodeUtil.isCallTo(rhs, "goog.module.get"), rhs);
checkState(rhs.hasTwoChildren(), rhs);
return rhs.getLastChild().getString();
default:
throw new RuntimeException("Invalid alias RHS:" + rhs);
} }
checkState(NodeUtil.isCallTo(aliasDefinition, "goog.module.get"), aliasDefinition);
checkState(aliasDefinition.hasTwoChildren(), aliasDefinition);
return aliasDefinition.getLastChild().getString();
} }


private static class AliasedNode extends AliasUsage { private static class AliasedNode extends AliasUsage {
Expand Down
37 changes: 36 additions & 1 deletion test/com/google/javascript/jscomp/ScopedAliasesTest.java
Expand Up @@ -1010,7 +1010,7 @@ public void testIssue2211c() {
"});")); "});"));
} }


public void testGoogModuleGet() { public void testGoogModuleGet1() {
test( test(
lines( lines(
"goog.provide('provided');", "goog.provide('provided');",
Expand All @@ -1027,6 +1027,41 @@ public void testGoogModuleGet() {
"")); ""));
} }


public void testGoogModuleGet2() {
test(
lines(
"goog.provide('foo.baz');",
"",
"goog.scope(function() {",
"",
"const a = goog.module.get('other.thing');",
"const b = a.b;",
"foo.baz = b",
"",
"}); // goog.scope"),
lines(
"goog.provide('foo.baz');",
"foo.baz = goog.module.get('other.thing').b;",
""));
}

public void testGoogModuleGet3() {
test(
lines(
"goog.provide('foo.baz');",
"",
"goog.scope(function() {",
"",
"const a = goog.module.get('other.thing').b;",
"foo.baz = a",
"",
"}); // goog.scope"),
lines(
"goog.provide('foo.baz');",
"foo.baz = goog.module.get('other.thing').b;",
""));
}

public void testObjectPattern() { public void testObjectPattern() {
testScopedNoChanges("", "{foo: ({bar}) => baz};"); testScopedNoChanges("", "{foo: ({bar}) => baz};");
} }
Expand Down

0 comments on commit 6797f5b

Please sign in to comment.