Skip to content

Commit

Permalink
[NTI] Make ConformanceRules use TypeI.
Browse files Browse the repository at this point in the history
No new NTI functionality implemented yet, this is just groundwork.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=129239340
  • Loading branch information
aravind-pg authored and Dimitris Vardoulakis committed Aug 5, 2016
1 parent 2b0bdc6 commit 2b01aca
Show file tree
Hide file tree
Showing 13 changed files with 225 additions and 107 deletions.
183 changes: 85 additions & 98 deletions src/com/google/javascript/jscomp/ConformanceRules.java

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions src/com/google/javascript/jscomp/GlobalTypeInfo.java
Expand Up @@ -44,6 +44,7 @@
import com.google.javascript.jscomp.newtypes.UniqueNameGenerator;
import com.google.javascript.rhino.IR;
import com.google.javascript.rhino.JSDocInfo;
import com.google.javascript.rhino.JSTypeExpression;
import com.google.javascript.rhino.Node;
import com.google.javascript.rhino.TypeI;
import com.google.javascript.rhino.TypeIRegistry;
Expand Down Expand Up @@ -2510,4 +2511,14 @@ public JSType getType(String typeName) {
return globalScope.getType(typeName);
}
}

@Override
public JSType createUnionType(List<? extends TypeI> variants) {
throw new UnsupportedOperationException("createUnionType not implemented yet");
}

@Override
public TypeI evaluateTypeExpressionInGlobalScope(JSTypeExpression expr) {
throw new UnsupportedOperationException("evaluateInEmptyScope not implemented yet");
}
}
3 changes: 1 addition & 2 deletions src/com/google/javascript/jscomp/ProcessDefines.java
Expand Up @@ -29,7 +29,6 @@
import com.google.javascript.rhino.Node;
import com.google.javascript.rhino.TypeI;
import com.google.javascript.rhino.TypeIRegistry;

import java.text.MessageFormat;
import java.util.ArrayDeque;
import java.util.ArrayList;
Expand Down Expand Up @@ -157,7 +156,7 @@ private static String format(MessageFormat format, Object... params) {
*/
private boolean isValidDefineType(JSTypeExpression expression) {
TypeIRegistry registry = compiler.getTypeIRegistry();
TypeI type = expression.evaluateInEmptyScope(registry);
TypeI type = registry.evaluateTypeExpressionInGlobalScope(expression);
return !type.isUnknownType()
&& type.isSubtypeOf(registry.getNativeType(NUMBER_STRING_BOOLEAN));
}
Expand Down
61 changes: 60 additions & 1 deletion src/com/google/javascript/jscomp/newtypes/JSType.java
Expand Up @@ -29,7 +29,6 @@
import com.google.javascript.rhino.Node;
import com.google.javascript.rhino.ObjectTypeI;
import com.google.javascript.rhino.TypeI;

import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
Expand Down Expand Up @@ -1535,6 +1534,36 @@ public boolean isUnknownType() {
return isUnknown();
}

@Override
public boolean isAllType() {
throw new UnsupportedOperationException("isAllType not implemented yet");
}

@Override
public boolean isNoResolvedType() {
throw new UnsupportedOperationException("isUnresolved not implemented yet");
}

@Override
public boolean isUnionType() {
throw new UnsupportedOperationException("isUnionType not implemented yet");
}

@Override
public boolean isVoidable() {
throw new UnsupportedOperationException("isVoidable not implemented yet");
}

@Override
public boolean isEmptyType() {
throw new UnsupportedOperationException("isEmpty not implemented yet");
}

@Override
public boolean isTemplateType() {
throw new UnsupportedOperationException("isGeneric not implemented yet");
}

@Override
public TypeI restrictByNotNullOrUndefined() {
return this.removeType(NULL_OR_UNDEF);
Expand Down Expand Up @@ -1628,6 +1657,21 @@ public TypeI getTypeOfThis() {
return getFunTypeIfSingletonObj().getThisType();
}

@Override
public int getMinArguments() {
throw new UnsupportedOperationException("getMinArguments not implemented yet");
}

@Override
public int getMaxArguments() {
throw new UnsupportedOperationException("getMaxArguments not implemented yet");
}

@Override
public Iterable<Node> getParameters() {
throw new UnsupportedOperationException("getParameters not implemented yet");
}

@Override
public boolean hasProperties() {
throw new UnsupportedOperationException("hasProperties not implemented yet");
Expand Down Expand Up @@ -1689,6 +1733,11 @@ public JSDocInfo getOwnPropertyJSDocInfo(String propertyName) {
return null;
}

@Override
public JSDocInfo getPropertyJSDocInfo(String propertyName) {
throw new UnsupportedOperationException("getPropertyJSDocInfo not implemented yet");
}

@Override
public Node getOwnPropertyDefSite(String propertyName) {
Preconditions.checkState(this.isSingletonObj());
Expand All @@ -1701,6 +1750,11 @@ public Node getPropertyDefSite(String propertyName) {
return this.getObjTypeIfSingletonObj().getPropertyDefSite(propertyName);
}

@Override
public Iterable<String> getOwnPropertyNames() {
throw new UnsupportedOperationException("getOwnPropertyNames not implemented yet");
}

@Override
public boolean isPrototypeObject() {
// TODO(aravindpg): this is just a complete stub to ensure that we never enter a codepath
Expand All @@ -1725,6 +1779,11 @@ public boolean hasProperty(String propertyName) {
Preconditions.checkArgument(!propertyName.contains("."));
return hasProp(new QualifiedName(propertyName));
}

@Override
public Iterable<TypeI> getUnionMembers() {
throw new UnsupportedOperationException("getUnionMembers not implemented yet");
}
}

final class UnionType extends JSType {
Expand Down
6 changes: 6 additions & 0 deletions src/com/google/javascript/rhino/FunctionTypeI.java
Expand Up @@ -76,4 +76,10 @@ public interface FunctionTypeI extends TypeI {
boolean hasProperties();

void setSource(Node n);

int getMinArguments();

int getMaxArguments();

Iterable<Node> getParameters();
}
5 changes: 0 additions & 5 deletions src/com/google/javascript/rhino/JSTypeExpression.java
Expand Up @@ -42,7 +42,6 @@
import com.google.javascript.rhino.jstype.JSType;
import com.google.javascript.rhino.jstype.JSTypeRegistry;
import com.google.javascript.rhino.jstype.StaticTypedScope;

import java.io.Serializable;

/**
Expand Down Expand Up @@ -104,10 +103,6 @@ public JSType evaluate(StaticTypedScope<JSType> scope, TypeIRegistry registry) {
return null;
}

public TypeI evaluateInEmptyScope(TypeIRegistry registry) {
return evaluate(null, registry);
}

@Override
public boolean equals(Object other) {
return other instanceof JSTypeExpression &&
Expand Down
4 changes: 4 additions & 0 deletions src/com/google/javascript/rhino/ObjectTypeI.java
Expand Up @@ -63,6 +63,8 @@ public interface ObjectTypeI extends TypeI {

JSDocInfo getOwnPropertyJSDocInfo(String propertyName);

JSDocInfo getPropertyJSDocInfo(String propertyName);

Node getOwnPropertyDefSite(String propertyName);

Node getPropertyDefSite(String propertyName);
Expand All @@ -74,4 +76,6 @@ public interface ObjectTypeI extends TypeI {
boolean isInstanceType();

boolean hasProperty(String propertyName);

Iterable<String> getOwnPropertyNames();
}
18 changes: 18 additions & 0 deletions src/com/google/javascript/rhino/TypeI.java
Expand Up @@ -50,6 +50,14 @@ public interface TypeI {

boolean isBottom();

boolean isAllType();

boolean isTemplateType();

boolean isEmptyType();

boolean isNoResolvedType();

boolean isConstructor();

boolean isEquivalentTo(TypeI type);
Expand All @@ -64,8 +72,12 @@ public interface TypeI {

boolean isUnknownType();

boolean isUnionType();

boolean isNullable();

boolean isVoidable();

boolean isPrototypeObject();

boolean isInstanceofObject();
Expand All @@ -92,5 +104,11 @@ public interface TypeI {
*/
ObjectTypeI toMaybeObjectType();

/**
* If this type is a union type, returns a list of its members. Otherwise
* returns null.
*/
Iterable<? extends TypeI> getUnionMembers();

String getDisplayName();
}
5 changes: 5 additions & 0 deletions src/com/google/javascript/rhino/TypeIRegistry.java
Expand Up @@ -40,6 +40,7 @@
package com.google.javascript.rhino;

import com.google.javascript.rhino.jstype.JSTypeNative;
import java.util.List;

/**
* @author blickly@google.com (Ben Lickly)
Expand All @@ -62,4 +63,8 @@ public interface TypeIRegistry {
String getReadableTypeName(Node n);

<T extends TypeI> T getType(String typeName);

TypeI createUnionType(List<? extends TypeI> variants);

TypeI evaluateTypeExpressionInGlobalScope(JSTypeExpression expr);
}
4 changes: 4 additions & 0 deletions src/com/google/javascript/rhino/jstype/FunctionType.java
Expand Up @@ -300,6 +300,7 @@ public boolean hasImplementedInterfaces() {
return false;
}

@Override
public Iterable<Node> getParameters() {
Node n = getParametersNode();
if (n != null) {
Expand All @@ -315,6 +316,7 @@ public Node getParametersNode() {
}

/** Gets the minimum number of arguments that this function requires. */
@Override
public int getMinArguments() {
// NOTE(nicksantos): There are some native functions that have optional
// parameters before required parameters. This algorithm finds the position
Expand All @@ -334,6 +336,7 @@ public int getMinArguments() {
* Gets the maximum number of arguments that this function requires,
* or Integer.MAX_VALUE if this is a variable argument function.
*/
@Override
public int getMaxArguments() {
Node params = getParametersNode();
if (params != null) {
Expand Down Expand Up @@ -900,6 +903,7 @@ private FunctionType tryMergeFunctionPiecewise(
* Given a constructor or an interface type, get its superclass constructor
* or {@code null} if none exists.
*/
@Override
public FunctionType getSuperClassConstructor() {
Preconditions.checkArgument(isConstructor() || isInterface());
ObjectType maybeSuperInstanceType = getPrototype().getImplicitPrototype();
Expand Down
14 changes: 13 additions & 1 deletion src/com/google/javascript/rhino/jstype/JSType.java
Expand Up @@ -48,7 +48,6 @@
import com.google.javascript.rhino.ErrorReporter;
import com.google.javascript.rhino.JSDocInfo;
import com.google.javascript.rhino.TypeI;

import java.io.Serializable;
import java.util.Comparator;
import java.util.IdentityHashMap;
Expand Down Expand Up @@ -158,6 +157,7 @@ public boolean isNoType() {
return false;
}

@Override
public boolean isNoResolvedType() {
return false;
}
Expand All @@ -166,6 +166,7 @@ public boolean isNoObjectType() {
return false;
}

@Override
public final boolean isEmptyType() {
return isNoType() || isNoObjectType() || isNoResolvedType() ||
(registry.getNativeFunctionType(
Expand Down Expand Up @@ -248,6 +249,7 @@ public boolean isVoidType() {
return false;
}

@Override
public boolean isAllType() {
return false;
}
Expand All @@ -261,6 +263,7 @@ public boolean isCheckedUnknownType() {
return false;
}

@Override
public final boolean isUnionType() {
return toMaybeUnionType() != null;
}
Expand Down Expand Up @@ -449,6 +452,7 @@ public TemplatizedType toMaybeTemplatizedType() {
return null;
}

@Override
public final boolean isTemplateType() {
return toMaybeTemplateType() != null;
}
Expand Down Expand Up @@ -952,6 +956,7 @@ public boolean isNullable() {
/**
* Tests whether this type is voidable.
*/
@Override
public boolean isVoidable() {
return isSubtype(getNativeType(JSTypeNative.VOID_TYPE));
}
Expand Down Expand Up @@ -1266,6 +1271,13 @@ public TypePair getTypesUnderShallowInequality(JSType that) {
}
}

@Override
public Iterable<JSType> getUnionMembers() {
return isUnionType()
? this.toMaybeUnionType().getAlternates()
: null;
}

/**
* If this is a union type, returns a union type that does not include
* the null or undefined type.
Expand Down
11 changes: 11 additions & 0 deletions src/com/google/javascript/rhino/jstype/JSTypeRegistry.java
Expand Up @@ -53,6 +53,7 @@
import com.google.common.collect.Multimap;
import com.google.javascript.rhino.ErrorReporter;
import com.google.javascript.rhino.JSDocInfo;
import com.google.javascript.rhino.JSTypeExpression;
import com.google.javascript.rhino.Node;
import com.google.javascript.rhino.SimpleErrorReporter;
import com.google.javascript.rhino.Token;
Expand Down Expand Up @@ -1140,6 +1141,11 @@ public void resolveTypesInScope(StaticTypedScope<JSType> scope) {
}
}

@Override
public JSType evaluateTypeExpressionInGlobalScope(JSTypeExpression expr) {
return expr.evaluate(null, this);
}

/**
* Creates a type representing optional values of the given type.
* @return the union of the type and the void type
Expand Down Expand Up @@ -1194,6 +1200,11 @@ public JSType createUnionType(JSType... variants) {
return builder.build();
}

@Override
public JSType createUnionType(List<? extends TypeI> variants) {
return createUnionType(variants.toArray(new JSType[0]));
}

/**
* Creates a union type whose variants are the built-in types specified
* by the arguments.
Expand Down

0 comments on commit 2b01aca

Please sign in to comment.