Skip to content

Commit

Permalink
[NTI] Clean-up how we set the type of the global this.
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=175838449
  • Loading branch information
dimvar authored and Tyler Breisacher committed Nov 15, 2017
1 parent e994b8d commit bead661
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 20 deletions.
13 changes: 9 additions & 4 deletions src/com/google/javascript/jscomp/GlobalTypeInfoCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import com.google.javascript.jscomp.newtypes.JSTypeCreatorFromJSDoc.FunctionAndSlotType;
import com.google.javascript.jscomp.newtypes.JSTypes;
import com.google.javascript.jscomp.newtypes.Namespace;
import com.google.javascript.jscomp.newtypes.NamespaceLit;
import com.google.javascript.jscomp.newtypes.NominalType;
import com.google.javascript.jscomp.newtypes.NominalTypeBuilderNti;
import com.google.javascript.jscomp.newtypes.ObjectKind;
Expand Down Expand Up @@ -385,7 +386,7 @@ public void process(Node externs, Node root) {
}
checkAndFreezeNominalType(rawType);
}
JSType globalThisType;
JSType globalThisType = null;
if (this.window != null) {
// Copy properties from window to Window.prototype, because sometimes
// people pass window around rather than using it directly.
Expand All @@ -396,9 +397,13 @@ public void process(Node externs, Node root) {
for (RawNominalType rawType : windows) {
checkAndFreezeNominalType(rawType);
}
// Type the global THIS as window
globalThisType = this.window.getInstanceAsJSType();
} else {
if (winNs != null) {
((NamespaceLit) winNs).setWindowType(this.window.getAsNominalType());
// Type the global THIS as window
globalThisType = winNs.toJSType();
}
}
if (globalThisType == null) {
// Type the global THIS as a loose object
globalThisType = getCommonTypes().getTopObject().withLoose();
}
Expand Down
10 changes: 1 addition & 9 deletions src/com/google/javascript/jscomp/NTIScope.java
Original file line number Diff line number Diff line change
Expand Up @@ -800,18 +800,10 @@ void freezeScope() {
for (Map.Entry<String, Namespace> entry : localNamespaces.entrySet()) {
String name = entry.getKey();
Namespace ns = entry.getValue();
JSType t;
if (ns instanceof NamespaceLit) {
constVars.add(name);
NamespaceLit nslit = (NamespaceLit) ns;
// The argument to maybeSetWindowInstance should only be non-null for
// window, but we don't check here to avoid hard-coding the name.
// Enforced in GlobalTypeInfo.
nslit.maybeSetWindowInstance(externs.get(name));
t = nslit.toJSType();
} else {
t = ns.toJSType();
}
JSType t = ns.toJSType();
if (externs.containsKey(name)) {
externs.put(name, t);
} else {
Expand Down
6 changes: 2 additions & 4 deletions src/com/google/javascript/jscomp/newtypes/NamespaceLit.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,8 @@ NominalType getWindowType() {
return this.window;
}

public void maybeSetWindowInstance(JSType obj) {
if (obj != null) {
this.window = obj.getNominalTypeIfSingletonObj();
}
public void setWindowType(NominalType window) {
this.window = window;
}

@Override
Expand Down
3 changes: 2 additions & 1 deletion src/com/google/javascript/jscomp/newtypes/ObjectType.java
Original file line number Diff line number Diff line change
Expand Up @@ -1739,7 +1739,8 @@ StringBuilder appendTo(StringBuilder builder, ToStringContext ctx) {
// More thorough stringification when annotation support is not needed.
if (!nominalType.isFunction()
&& !nominalType.isBuiltinObject()
&& !nominalType.isLiteralObject()) {
&& !nominalType.isLiteralObject()
&& !isNamespace()) {
nominalType.appendTo(builder, ctx);
} else if (isStruct()) {
builder.append("struct");
Expand Down
9 changes: 7 additions & 2 deletions test/com/google/javascript/jscomp/NewTypeInferenceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19299,9 +19299,14 @@ public void testDontCrashWhenDeclaringFunctionOnScalar() {
}

public void testDontTypeGlobalThisAsUknown() {
typeCheck(
typeCheckMessageContents(
"var /** null */ x = this;",
NewTypeInference.MISTYPED_ASSIGN_RHS);
NewTypeInference.MISTYPED_ASSIGN_RHS,
LINE_JOINER.join(
"The right side in the assignment is not a subtype of the left side.",
"Expected : null",
"Found : window",
""));

typeCheck("var /** !Window */ x = this;");

Expand Down

0 comments on commit bead661

Please sign in to comment.