Skip to content

Commit

Permalink
add JSType.restrictByPossiblyFalsy()
Browse files Browse the repository at this point in the history
I intend to use this method in an upcoming change to add code
that creates new AST nodes from existing ones that already have type
information. It'll be used to calculate the right types for new
AND and OR nodes, for example.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=210971649
  • Loading branch information
brad4d authored and blickly committed Aug 31, 2018
1 parent b26f4d0 commit 6147ef3
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/com/google/javascript/rhino/jstype/EnumElementType.java
Original file line number Diff line number Diff line change
Expand Up @@ -255,4 +255,9 @@ JSType resolveInternal(ErrorReporter reporter) {
primitiveObjectType = ObjectType.cast(primitiveType);
return this;
}

@Override
public JSType restrictByPossiblyFalsy() {
return primitiveType.restrictByPossiblyFalsy();
}
}
10 changes: 10 additions & 0 deletions src/com/google/javascript/rhino/jstype/JSType.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
package com.google.javascript.rhino.jstype;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.javascript.rhino.jstype.JSTypeNative.NO_TYPE;

import com.google.common.base.Predicate;
import com.google.common.collect.HashBasedTable;
Expand Down Expand Up @@ -1433,6 +1434,15 @@ public JSType restrictByNotUndefined() {
return this;
}

/**
* If this is a union type, returns a union type that only includes members that may be falsy. For
* example, {@code null|number|Object} would restrict to {@code null|number}, since null and
* number may both be falsy, while {@code Object} is never falsy.
*/
public JSType restrictByPossiblyFalsy() {
return getNativeType(NO_TYPE);
}

/**
* the logic of this method is similar to isSubtype,
* except that it does not perform structural interface matching
Expand Down
5 changes: 5 additions & 0 deletions src/com/google/javascript/rhino/jstype/ProxyObjectType.java
Original file line number Diff line number Diff line change
Expand Up @@ -417,4 +417,9 @@ public boolean hasAnyTemplateTypesInternal() {
public TemplateTypeMap getTemplateTypeMap() {
return referencedType.getTemplateTypeMap();
}

@Override
public JSType restrictByPossiblyFalsy() {
return referencedType.restrictByPossiblyFalsy();
}
}
7 changes: 6 additions & 1 deletion src/com/google/javascript/rhino/jstype/SymbolType.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@

package com.google.javascript.rhino.jstype;

import static com.google.javascript.rhino.jstype.JSTypeNative.NO_TYPE;
import static com.google.javascript.rhino.jstype.JSTypeNative.SYMBOL_OBJECT_TYPE;
import static com.google.javascript.rhino.jstype.JSTypeNative.SYMBOL_VALUE_OR_OBJECT_TYPE;
import static com.google.javascript.rhino.jstype.TernaryValue.FALSE;
import static com.google.javascript.rhino.jstype.TernaryValue.UNKNOWN;


/**
* Symbol type.
* @author johnlenz@google.com (John Lenz)
Expand Down Expand Up @@ -118,4 +118,9 @@ public BooleanLiteralSet getPossibleToBooleanOutcomes() {
public <T> T visit(Visitor<T> visitor) {
return visitor.caseSymbolType();
}

@Override
public JSType restrictByPossiblyFalsy() {
return registry.getNativeType(NO_TYPE);
}
}
9 changes: 9 additions & 0 deletions src/com/google/javascript/rhino/jstype/UnionType.java
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,15 @@ public JSType restrictByNotUndefined() {
return restricted.build();
}

@Override
public JSType restrictByPossiblyFalsy() {
UnionTypeBuilder restricted = UnionTypeBuilder.create(registry);
for (JSType t : alternatesRetainingStructuralSubtypes) {
restricted.addAlternate(t.restrictByPossiblyFalsy());
}
return restricted.build();
}

@Override
public TernaryValue testForEquality(JSType that) {
TernaryValue result = null;
Expand Down
5 changes: 5 additions & 0 deletions src/com/google/javascript/rhino/jstype/UnknownType.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,9 @@ JSType resolveInternal(ErrorReporter reporter) {
int recursionUnsafeHashCode() {
return System.identityHashCode(this);
}

@Override
public JSType restrictByPossiblyFalsy() {
return this;
}
}
5 changes: 5 additions & 0 deletions src/com/google/javascript/rhino/jstype/ValueType.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,9 @@ final int recursionUnsafeHashCode() {
// Subclasses of this type are unique within a JSTypeRegisty.
return System.identityHashCode(this);
}

@Override
public JSType restrictByPossiblyFalsy() {
return this;
}
}
4 changes: 4 additions & 0 deletions src/com/google/javascript/rhino/testing/TypeSubject.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ public void isNotUnknown() {
check("isUnknownType()").that(actualNonNull().isUnknownType()).isFalse();
}

public void isEmpty() {
check("isEmptyType()").that(actualNonNull().isEmptyType()).isTrue();
}

public void isNotEmpty() {
check("isEmptyType()").that(actualNonNull().isEmptyType()).isFalse();
}
Expand Down
20 changes: 20 additions & 0 deletions test/com/google/javascript/rhino/jstype/UnionTypeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,26 @@ public void testUnionType() throws Exception {
Asserts.assertResolvesToSame(nullOrString);
}

public void testRestrictByNotUndefined() throws Exception {
UnionType nullOrString = (UnionType) createUnionType(NULL_TYPE, STRING_OBJECT_TYPE);
UnionType nullOrStringOrUndefined =
(UnionType) createUnionType(NULL_TYPE, STRING_OBJECT_TYPE, VOID_TYPE);

assertType(nullOrStringOrUndefined.restrictByNotUndefined())
.isStructurallyEqualTo(nullOrString);
}

public void testRestrictByPossiblyFalsy() throws Exception {
UnionType allPossiblyFalsy =
(UnionType) createUnionType(NULL_TYPE, VOID_TYPE, STRING_TYPE, NUMBER_TYPE, BOOLEAN_TYPE);
UnionType nonePossiblyFalsy = (UnionType) createUnionType(ARRAY_TYPE, STRING_OBJECT_TYPE);
UnionType mixture = (UnionType) createUnionType(allPossiblyFalsy, nonePossiblyFalsy);

assertType(allPossiblyFalsy.restrictByPossiblyFalsy()).isStructurallyEqualTo(allPossiblyFalsy);
assertType(mixture.restrictByPossiblyFalsy()).isStructurallyEqualTo(allPossiblyFalsy);
assertType(nonePossiblyFalsy.restrictByPossiblyFalsy()).isEmpty();
}

/**
* Tests {@link JSType#getGreatestSubtype(JSType)} on union types.
*/
Expand Down

0 comments on commit 6147ef3

Please sign in to comment.