Skip to content

Commit

Permalink
Avoid crash in type-summary generation with this properties in global…
Browse files Browse the repository at this point in the history
… scope

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=189638078
  • Loading branch information
blickly authored and brad4d committed Mar 20, 2018
1 parent 999cffe commit bdd0573
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/com/google/javascript/jscomp/ijs/ClassUtil.java
Expand Up @@ -16,11 +16,13 @@
package com.google.javascript.jscomp.ijs;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;

import com.google.javascript.jscomp.NodeUtil;
import com.google.javascript.rhino.JSDocInfo;
import com.google.javascript.rhino.Node;
import javax.annotation.Nullable;

/**
* Static utility methods for dealing with classes. The primary benefit is for papering over
Expand All @@ -30,15 +32,28 @@ final class ClassUtil {
private ClassUtil() {}

static boolean isThisProp(Node getprop) {
return getprop.isGetProp() && getprop.getFirstChild().isThis();
return getClassNameOfThisProp(getprop) != null;
}

static String getPrototypeNameOfThisProp(Node getprop) {
checkArgument(isThisProp(getprop));
String className = checkNotNull(getClassNameOfThisProp(getprop));
return className + ".prototype." + getprop.getLastChild().getString();
}

@Nullable
private static String getClassNameOfThisProp(Node getprop) {
if (!getprop.isGetProp() || !getprop.getFirstChild().isThis()) {
return null;
}
Node function = NodeUtil.getEnclosingFunction(getprop);
if (function == null) {
return null;
}
String className = getClassName(function);
checkState(className != null && !className.isEmpty());
return className + ".prototype." + getprop.getLastChild().getString();
if (className == null || className.isEmpty()) {
return null;
}
return className;
}

static String getFullyQualifiedNameOfMethod(Node function) {
Expand Down
Expand Up @@ -280,6 +280,13 @@ public void testMultipleSameNamedThisProperties() {

}

public void testGlobalThis() {
testSame("/** @const */ this.globalNamespace = {};");
test(
"/** @const */ this.globalNamespace = this.globalNamespace || {};",
"/** @const */ this.globalNamespace = {};");
}

public void testGoogAddSingletonGetter() {
testSame("class Foo {} goog.addSingletonGetter(Foo);");
}
Expand Down

0 comments on commit bdd0573

Please sign in to comment.