Skip to content

Commit

Permalink
More prework for adding support for non-global scope types to JSTypeR…
Browse files Browse the repository at this point in the history
…egistry

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=188603342
  • Loading branch information
concavelenz authored and blickly committed Mar 12, 2018
1 parent e98010b commit 99cf082
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 24 deletions.
6 changes: 3 additions & 3 deletions src/com/google/javascript/jscomp/CheckMissingReturn.java
Expand Up @@ -199,16 +199,16 @@ private JSType explicitReturnExpected(Node scopeRoot) {
* with an empty body
*/
private static boolean isEmptyFunction(Node function) {
return function.getChildCount() == 3 &&
!function.getSecondChild().getNext().hasChildren();
return function.getChildCount() == 3
&& !function.getSecondChild().getNext().hasChildren();
}

/**
* @return {@code true} if returnType is void, unknown, or a union
* containing void or unknown
*/
private boolean isVoidOrUnknown(JSType returnType) {
final JSType voidType = compiler.getTypeIRegistry().getNativeType(JSTypeNative.VOID_TYPE);
final JSType voidType = compiler.getTypeRegistry().getNativeType(JSTypeNative.VOID_TYPE);
return voidType.isSubtype(returnType);
}
}
2 changes: 1 addition & 1 deletion src/com/google/javascript/jscomp/Es6ToEs3Util.java
Expand Up @@ -119,7 +119,7 @@ static TypeI createGenericType(
if (!shouldCreate) {
return null;
}
ObjectTypeI genericType = registry.getNativeType(typeName);
ObjectTypeI genericType = (ObjectTypeI) (registry.getNativeType(typeName));
ObjectTypeI uninstantiated = genericType.getRawType();
return registry.instantiateGenericType(uninstantiated, ImmutableList.of(typeArg));
}
Expand Down
5 changes: 3 additions & 2 deletions src/com/google/javascript/jscomp/RemoveSuperMethodsPass.java
Expand Up @@ -128,8 +128,9 @@ private boolean functionNameMatches(String enclosingMethodName, Node call) {

// Check that call references the superclass
String calledClass = callNameSplittedByPrototypeMarker[0];
TypeI subclassType = compiler.getTypeIRegistry().getType(enclosingClassName);
TypeI calledClassType = compiler.getTypeIRegistry().getType(calledClass);
// TODO(moz): fix this to handle shadowing local type names
TypeI subclassType = compiler.getTypeIRegistry().getGlobalType(enclosingClassName);
TypeI calledClassType = compiler.getTypeIRegistry().getGlobalType(calledClass);
if (subclassType == null || calledClassType == null) {
return false;
}
Expand Down
Expand Up @@ -201,7 +201,7 @@ boolean isTypeCompatible(TypeI receiverType, String typeName) {
// Look up the typename in the registry. All the polyfilled method
// receiver types are built-in JS types, so they had better not be
// missing from the registry.
TypeI type = compiler.getTypeIRegistry().getType(typeName);
TypeI type = compiler.getTypeIRegistry().getGlobalType(typeName);
if (type == null) {
throw new RuntimeException("Missing built-in type: " + typeName);
}
Expand Down
11 changes: 10 additions & 1 deletion src/com/google/javascript/jscomp/TypeTransformation.java
Expand Up @@ -31,7 +31,9 @@
import com.google.javascript.rhino.TypeI;
import com.google.javascript.rhino.TypeIEnv;
import com.google.javascript.rhino.TypeIRegistry;
import com.google.javascript.rhino.jstype.JSType;
import com.google.javascript.rhino.jstype.JSTypeNative;
import com.google.javascript.rhino.jstype.StaticTypedScope;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -130,8 +132,15 @@ private Keywords nameToKeyword(String s) {
return TypeTransformationParser.Keywords.valueOf(s.toUpperCase());
}

@SuppressWarnings("unchecked")
private TypeI getType(String typeName) {
TypeI type = registry.getType(typeName);
TypeI type;
if (typeEnv instanceof StaticTypedScope) {
type = registry.getType((StaticTypedScope<JSType>) typeEnv, typeName);
} else {
// TODO(johnlenz): remove this branch once NTI is deleted.
type = registry.getType(null, typeName);
}
if (type != null) {
return type;
}
Expand Down
5 changes: 3 additions & 2 deletions src/com/google/javascript/jscomp/TypedCodeGenerator.java
Expand Up @@ -253,11 +253,12 @@ private ObjectTypeI findMethodOwner(Node n) {
if (parent.isAssign()) {
Node target = parent.getFirstChild();
if (NodeUtil.isPrototypeProperty(target)) {
TypeI type = registry.getType(target.getFirstFirstChild().getQualifiedName());
// TODO(johnlenz): handle non-global types
TypeI type = registry.getGlobalType(target.getFirstFirstChild().getQualifiedName());
ctor = type != null ? ((ObjectTypeI) type).getConstructor() : null;
}
} else if (parent.isClass()) {
// TODO(sdh): test this case once NTI understands ES6 classes
// TODO(sdh): test this case once the type checker understands ES6 classes
ctor = parent.getTypeI().toMaybeFunctionType();
}
return ctor != null ? ctor.getInstanceType() : null;
Expand Down
22 changes: 11 additions & 11 deletions src/com/google/javascript/rhino/jstype/PrototypeObjectType.java
Expand Up @@ -224,15 +224,15 @@ public void setPropertyNode(String propertyName, Node defSite) {

@Override
public boolean matchesNumberContext() {
return isNumberObjectType() || isDateType() || isBooleanObjectType() ||
isStringObjectType() || hasOverridenNativeProperty("valueOf");
return isNumberObjectType() || isDateType() || isBooleanObjectType()
|| isStringObjectType() || hasOverriddenNativeProperty("valueOf");
}

@Override
public boolean matchesStringContext() {
return isTheObjectType() || isStringObjectType() || isDateType() ||
isRegexpType() || isArrayType() || isNumberObjectType() ||
isBooleanObjectType() || hasOverridenNativeProperty("toString");
return isTheObjectType() || isStringObjectType() || isDateType()
|| isRegexpType() || isArrayType() || isNumberObjectType()
|| isBooleanObjectType() || hasOverriddenNativeProperty("toString");
}

@Override
Expand All @@ -244,16 +244,16 @@ public boolean matchesSymbolContext() {
* Given the name of a native object property, checks whether the property is
* present on the object and different from the native one.
*/
private boolean hasOverridenNativeProperty(String propertyName) {
private boolean hasOverriddenNativeProperty(String propertyName) {
if (isNativeObjectType()) {
return false;
}

JSType propertyType = getPropertyType(propertyName);
ObjectType nativeType =
isFunctionType() ?
registry.getNativeObjectType(JSTypeNative.FUNCTION_PROTOTYPE) :
registry.getNativeObjectType(JSTypeNative.OBJECT_PROTOTYPE);
isFunctionType()
? registry.getNativeObjectType(JSTypeNative.FUNCTION_PROTOTYPE)
: registry.getNativeObjectType(JSTypeNative.OBJECT_PROTOTYPE);
JSType nativePropertyType = nativeType.getPropertyType(propertyName);
return propertyType != nativePropertyType;
}
Expand Down Expand Up @@ -297,8 +297,8 @@ StringBuilder appendTo(StringBuilder sb, boolean forAnnotations) {
// Use a tree set so that the properties are sorted.
Set<String> propertyNames = new TreeSet<>();
for (ObjectType current = this;
current != null && !current.isNativeObjectType() &&
propertyNames.size() <= MAX_PRETTY_PRINTED_PROPERTIES;
current != null && !current.isNativeObjectType()
&& propertyNames.size() <= MAX_PRETTY_PRINTED_PROPERTIES;
current = current.getImplicitPrototype()) {
propertyNames.addAll(current.getOwnPropertyNames());
}
Expand Down
Expand Up @@ -675,7 +675,7 @@ public void testSourceNodeOfFunctionTypesUpdated() {
Result result = compiler.getResult();
assertNoWarningsOrErrors(result);

JSType type = compiler.getTypeRegistry().getType("ns.Foo");
JSType type = compiler.getTypeRegistry().getGlobalType("ns.Foo");
FunctionType fnType = type.toObjectType().getConstructor();
Node srcNode = fnType.getSource();
assertNode(srcNode).hasLineno(6);
Expand Down Expand Up @@ -742,15 +742,15 @@ public void testPrototypeSlotChangedOnCompile() {

Compiler compiler = runFullCompile(
getOptions(), ImmutableList.of(CLOSURE_BASE, src), 0, 0, false);
JSType type = compiler.getTypeRegistry().getType("ns.Foo");
JSType type = compiler.getTypeRegistry().getGlobalType("ns.Foo");
FunctionType fnType = type.toObjectType().getConstructor();
StaticTypedSlot<JSType> originalSlot = fnType.getSlot("prototype");

doReplaceScript(compiler, src, 1);

assertNoWarningsOrErrors(compiler.getResult());

type = compiler.getTypeRegistry().getType("ns.Foo");
type = compiler.getTypeRegistry().getGlobalType("ns.Foo");
fnType = type.toObjectType().getConstructor();
StaticTypedSlot<JSType> newSlot = fnType.getSlot("prototype");
assertNotSame(originalSlot, newSlot);
Expand Down

0 comments on commit 99cf082

Please sign in to comment.