Skip to content

Commit

Permalink
[NTI] Clean up remaining static fields in JSType.
Browse files Browse the repository at this point in the history
Fixes #2004 on github.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=133430551
  • Loading branch information
dimvar authored and blickly committed Sep 19, 2016
1 parent ee94ad5 commit 0109a47
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 96 deletions.
16 changes: 6 additions & 10 deletions src/com/google/javascript/jscomp/ClosureCodingConvention.java
Expand Up @@ -29,7 +29,6 @@
import com.google.javascript.rhino.jstype.JSTypeNative;
import com.google.javascript.rhino.jstype.JSTypeRegistry;
import com.google.javascript.rhino.jstype.ObjectType;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -403,13 +402,10 @@ public boolean isPrivate(String name) {
@Override
public Collection<AssertionFunctionSpec> getAssertionFunctions() {
return ImmutableList.of(
new AssertionFunctionSpec("goog.asserts.assert", JSType.TRUTHY),
new AssertionFunctionSpec("goog.asserts.assertNumber",
JSType.NUMBER, JSTypeNative.NUMBER_TYPE),
new AssertionFunctionSpec("goog.asserts.assertString",
JSType.STRING, JSTypeNative.STRING_TYPE),
new AssertionFunctionSpec("goog.asserts.assertObject",
JSType.TOP_OBJECT, JSTypeNative.OBJECT_TYPE),
new AssertionFunctionSpec("goog.asserts.assert", JSTypeNative.TRUTHY),
new AssertionFunctionSpec("goog.asserts.assertNumber", JSTypeNative.NUMBER_TYPE),
new AssertionFunctionSpec("goog.asserts.assertString", JSTypeNative.STRING_TYPE),
new AssertionFunctionSpec("goog.asserts.assertObject", JSTypeNative.OBJECT_TYPE),
new AssertFunctionByTypeName("goog.asserts.assertFunction", "Function"),
new AssertFunctionByTypeName("goog.asserts.assertArray", "Array"),
new AssertFunctionByTypeName("goog.asserts.assertElement", "Element"),
Expand Down Expand Up @@ -504,7 +500,7 @@ private static Node safeNext(Node n) {
*/
public static class AssertInstanceofSpec extends AssertionFunctionSpec {
public AssertInstanceofSpec(String functionName) {
super(functionName, JSType.TOP_OBJECT, JSTypeNative.OBJECT_TYPE);
super(functionName, JSTypeNative.OBJECT_TYPE);
}

/**
Expand Down Expand Up @@ -549,7 +545,7 @@ public JSType getAssertedNewType(Node call, DeclaredTypeRegistry scope) {
}
}
}
return JSType.UNKNOWN;
return scope.getCommonTypes().UNKNOWN;
}
}

Expand Down
17 changes: 4 additions & 13 deletions src/com/google/javascript/jscomp/CodingConvention.java
Expand Up @@ -25,7 +25,6 @@
import com.google.javascript.rhino.jstype.JSTypeRegistry;
import com.google.javascript.rhino.jstype.ObjectType;
import com.google.javascript.rhino.jstype.StaticTypedScope;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -478,24 +477,15 @@ static class ObjectLiteralCast {
*/
public class AssertionFunctionSpec {
protected final String functionName;
// Old type system type
protected final JSTypeNative assertedType;
// New type system type
protected final JSType assertedNewType;

@Deprecated
public AssertionFunctionSpec(String functionName) {
this(functionName, JSType.UNKNOWN, null);
}

public AssertionFunctionSpec(String functionName, JSType assertedNewType) {
this(functionName, assertedNewType, null);
this(functionName, null);
}

public AssertionFunctionSpec(String functionName,
JSType assertedNewType, JSTypeNative assertedType) {
public AssertionFunctionSpec(String functionName, JSTypeNative assertedType) {
this.functionName = functionName;
this.assertedNewType = assertedNewType;
this.assertedType = assertedType;
}

Expand Down Expand Up @@ -527,7 +517,8 @@ public Node getAssertedParam(Node firstParam) {
* @param call The asserting call
*/
public JSType getAssertedNewType(Node call, DeclaredTypeRegistry scope) {
return assertedNewType;
return assertedType != null
? scope.getCommonTypes().getNativeType(assertedType) : null;
}
}
}
32 changes: 2 additions & 30 deletions src/com/google/javascript/jscomp/GlobalTypeInfo.java
Expand Up @@ -431,7 +431,7 @@ public void process(Node externs, Node root) {
globalThisType = win.getInstanceAsJSType();
} else {
// Type the global THIS as a loose object
globalThisType = JSType.TOP_OBJECT.withLoose();
globalThisType = this.commonTypes.TOP_OBJECT.withLoose();
}
this.globalScope.setDeclaredType(
(new FunctionTypeBuilder(this.commonTypes)).
Expand Down Expand Up @@ -2593,35 +2593,7 @@ public JSType getNativeObjectType(JSTypeNative typeId) {

@Override
public JSType getNativeType(JSTypeNative typeId) {
// NOTE(aravindpg): not all JSTypeNative variants are handled here; add more as-needed.
switch (typeId) {
case ALL_TYPE:
return commonTypes.TOP;
case NO_TYPE:
return commonTypes.BOTTOM;
case UNKNOWN_TYPE:
return commonTypes.UNKNOWN;
case VOID_TYPE:
return commonTypes.UNDEFINED;
case NULL_TYPE:
return commonTypes.NULL;
case BOOLEAN_TYPE:
return commonTypes.BOOLEAN;
case STRING_TYPE:
return commonTypes.STRING;
case NUMBER_TYPE:
return commonTypes.NUMBER;
case NUMBER_STRING_BOOLEAN:
return JSType.join(commonTypes.NUMBER_OR_STRING, commonTypes.BOOLEAN);
case REGEXP_TYPE:
return commonTypes.getRegexpType();
case ARRAY_TYPE:
return commonTypes.getArrayInstance();
case OBJECT_TYPE:
return commonTypes.TOP_OBJECT;
default:
throw new RuntimeException("Native type " + typeId.name() + " not found");
}
return this.commonTypes.getNativeType(typeId);
}

@Override
Expand Down
5 changes: 5 additions & 0 deletions src/com/google/javascript/jscomp/NTIScope.java
Expand Up @@ -122,6 +122,11 @@ String getName() {
return name;
}

@Override
public JSTypes getCommonTypes() {
return this.commonTypes;
}

void setDeclaredType(DeclaredFunctionType declaredType) {
Preconditions.checkNotNull(declaredType);
this.declaredType = declaredType;
Expand Down
4 changes: 2 additions & 2 deletions src/com/google/javascript/jscomp/NewTypeInference.java
Expand Up @@ -2220,12 +2220,12 @@ private EnvTypePair analyzeArrayLitFwd(Node expr, TypeEnv inEnv) {
// logical operators.
private EnvTypePair analyzeCastFwd(Node expr, TypeEnv inEnv, JSType specializedType) {
Node parent = expr.getParent();
JSType newSpecType = JSType.UNKNOWN;
JSType newSpecType = this.commonTypes.UNKNOWN;
if ((parent.isOr() || parent.isAnd()) && expr == parent.getFirstChild()) {
newSpecType = specializedType;
}
EnvTypePair pair =
analyzeExprFwd(expr.getFirstChild(), inEnv, JSType.UNKNOWN, newSpecType);
analyzeExprFwd(expr.getFirstChild(), inEnv, this.commonTypes.UNKNOWN, newSpecType);
JSType fromType = pair.type;
JSType toType = symbolTable.getCastType(expr);
if (!fromType.isInterfaceInstance()
Expand Down
Expand Up @@ -37,4 +37,10 @@ public interface DeclaredTypeRegistry {
* or null if the identifier is not defined.
*/
JSType getDeclaredTypeOf(String name);

/**
* Returns an object that defines the built-in types, and contains other state
* used throughout type checking.
*/
JSTypes getCommonTypes();
}
Expand Up @@ -66,9 +66,9 @@ public FunctionTypeBuilder addPlaceholderFormal() {
if (restFormals != null) {
// Nothing to do here, since there is no way to add a placeholder.
} else if (!optionalFormals.isEmpty()) {
optionalFormals.add(JSType.UNKNOWN);
optionalFormals.add(this.commonTypes.UNKNOWN);
} else {
requiredFormals.add(JSType.UNKNOWN);
requiredFormals.add(this.commonTypes.UNKNOWN);
}
return this;
}
Expand Down
50 changes: 18 additions & 32 deletions src/com/google/javascript/jscomp/newtypes/JSType.java
Expand Up @@ -232,13 +232,6 @@ && getEnums().isEmpty()) {
return ((getMask() & TYPEVAR_MASK) != 0) == (getTypeVar() != null);
}

// TODO(dimvar): used in coding conventions. Delete in follow-up CL
public static JSType NUMBER;
public static JSType STRING;
public static JSType TRUTHY;
public static JSType UNKNOWN;
public static JSType TOP_OBJECT;

static Map<String, JSType> createScalars(JSTypes commonTypes) {
LinkedHashMap<String, JSType> types = new LinkedHashMap<>();

Expand All @@ -265,18 +258,9 @@ static Map<String, JSType> createScalars(JSTypes commonTypes) {
types.put("NULL_OR_UNDEFINED", new MaskType(commonTypes, NULL_MASK | UNDEFINED_MASK));
types.put("NUMBER_OR_STRING", new MaskType(commonTypes, NUMBER_MASK | STRING_MASK));

NUMBER = types.get("NUMBER");
STRING = types.get("STRING");
TRUTHY = types.get("TRUTHY");
UNKNOWN = types.get("UNKNOWN");

return types;
}

static void initObjects(JSTypes commonTypes) {
TOP_OBJECT = commonTypes.TOP_OBJECT;
}

@Override
public boolean isTop() {
return TOP_MASK == getMask();
Expand Down Expand Up @@ -563,7 +547,7 @@ public static JSType join(JSType lhs, JSType rhs) {
return commonTypes.TOP;
}
if (lhs.isUnknown() || rhs.isUnknown()) {
return UNKNOWN;
return commonTypes.UNKNOWN;
}
if (lhs.isBottom()) {
return rhs;
Expand All @@ -573,12 +557,12 @@ public static JSType join(JSType lhs, JSType rhs) {
}
if (lhs.hasTruthyMask() || lhs.hasFalsyMask()
|| rhs.hasTruthyMask() || rhs.hasFalsyMask()) {
return UNKNOWN;
return commonTypes.UNKNOWN;
}
if (lhs.getTypeVar() != null && rhs.getTypeVar() != null
&& !lhs.getTypeVar().equals(rhs.getTypeVar())) {
// For now return ? when joining two type vars. This is probably uncommon.
return UNKNOWN;
return commonTypes.UNKNOWN;
}

int newMask = lhs.getMask() | rhs.getMask();
Expand Down Expand Up @@ -1019,7 +1003,7 @@ private JSType makeTruthy() {
return this;
}
if (this.isUnknown()) {
return TRUTHY;
return this.commonTypes.TRUTHY;
}
return makeType(this.commonTypes,
getMask() & ~NULL_MASK & ~FALSE_MASK & ~UNDEFINED_MASK,
Expand All @@ -1038,12 +1022,13 @@ private JSType makeFalsy() {
}

public static JSType plus(JSType lhs, JSType rhs) {
if (!lhs.isUnknown() && !lhs.isBottom() && lhs.isSubtypeOf(STRING)
|| !rhs.isUnknown() && !rhs.isBottom() && rhs.isSubtypeOf(STRING)) {
return STRING;
JSTypes commonTypes = lhs.commonTypes;
if (!lhs.isUnknown() && !lhs.isBottom() && lhs.isSubtypeOf(commonTypes.STRING)
|| !rhs.isUnknown() && !rhs.isBottom() && rhs.isSubtypeOf(commonTypes.STRING)) {
return commonTypes.STRING;
}
if (lhs.isUnknown() || lhs.isTop() || rhs.isUnknown() || rhs.isTop()) {
return UNKNOWN;
return commonTypes.UNKNOWN;
}
// If either has string, string in the result.
int newtype = (lhs.getMask() | rhs.getMask()) & STRING_MASK;
Expand All @@ -1063,9 +1048,9 @@ public JSType negate() {
if (isTrueOrTruthy()) {
return this.commonTypes.FALSY;
} else if (isFalseOrFalsy()) {
return TRUTHY;
return this.commonTypes.TRUTHY;
}
return UNKNOWN;
return this.commonTypes.UNKNOWN;
}

public JSType toBoolean() {
Expand Down Expand Up @@ -1164,8 +1149,9 @@ private static void whyNotUnionSubtypes(
JSTypes commonTypes = found.commonTypes;
if (commonTypes.NUMBER.isSubtypeOf(found) && !commonTypes.NUMBER.isSubtypeOf(expected)) {
boxedInfo[0] = MismatchInfo.makeUnionTypeMismatch(commonTypes.NUMBER);
} else if (STRING.isSubtypeOf(found) && !STRING.isSubtypeOf(expected)) {
boxedInfo[0] = MismatchInfo.makeUnionTypeMismatch(STRING);
} else if (commonTypes.STRING.isSubtypeOf(found)
&& !commonTypes.STRING.isSubtypeOf(expected)) {
boxedInfo[0] = MismatchInfo.makeUnionTypeMismatch(commonTypes.STRING);
} else if (commonTypes.BOOLEAN.isSubtypeOf(found)
&& !commonTypes.BOOLEAN.isSubtypeOf(expected)) {
boxedInfo[0] = MismatchInfo.makeUnionTypeMismatch(commonTypes.BOOLEAN);
Expand Down Expand Up @@ -1304,7 +1290,7 @@ public JSType withLoose() {

public JSType getProp(QualifiedName qname) {
if (isBottom() || isUnknown() || hasTruthyMask()) {
return UNKNOWN;
return this.commonTypes.UNKNOWN;
}
Preconditions.checkState(!getObjs().isEmpty() || !getEnums().isEmpty(),
"Can't getProp %s of type %s", qname, this);
Expand All @@ -1315,7 +1301,7 @@ public JSType getProp(QualifiedName qname) {

public JSType getDeclaredProp(QualifiedName qname) {
if (isUnknown()) {
return UNKNOWN;
return this.commonTypes.UNKNOWN;
}
Preconditions.checkState(!getObjs().isEmpty() || !getEnums().isEmpty());
return nullAcceptingJoin(
Expand Down Expand Up @@ -1378,7 +1364,7 @@ public JSType withDeclaredProperty(
QualifiedName qname, JSType type, boolean isConstant) {
Preconditions.checkState(!getObjs().isEmpty());
if (type == null && isConstant) {
type = JSType.UNKNOWN;
type = this.commonTypes.UNKNOWN;
}
return makeType(this.commonTypes,
getMask(),
Expand All @@ -1405,7 +1391,7 @@ public JSType findSubtypeWithProp(QualifiedName pname) {
// If it has enums, return bottom.
if (this.commonTypes.NUMBER.isSubtypeOf(this)
&& this.commonTypes.getNumberInstance().mayHaveProp(pname)
|| STRING.isSubtypeOf(this)
|| this.commonTypes.STRING.isSubtypeOf(this)
&& this.commonTypes.getNumberInstance().mayHaveProp(pname)
|| this.commonTypes.BOOLEAN.isSubtypeOf(this)
&& this.commonTypes.getBooleanInstance().mayHaveProp(pname)) {
Expand Down

0 comments on commit 0109a47

Please sign in to comment.