Search in sources :

Example 16 with TemplateType

use of com.google.javascript.rhino.jstype.TemplateType in project closure-compiler by google.

the class TypeCheck method isObjectTypeWithNonStringifiableKey.

/**
 * Checks whether current type is Object type with non-stringifable key.
 */
private boolean isObjectTypeWithNonStringifiableKey(JSType type) {
    if (!type.isTemplatizedType()) {
        // TODO(nickreid): Why don't we care about types like `Foo extends Object<Bar, Qux>`?
        return false;
    }
    TemplateTypeMap templateTypeMap = type.getTemplateTypeMap();
    TemplateType objectIndexKey = typeRegistry.getObjectIndexKey();
    if (templateTypeMap.hasTemplateKey(objectIndexKey)) {
        return !isReasonableObjectPropertyKey(templateTypeMap.getResolvedTemplateType(objectIndexKey));
    } else {
        return false;
    }
}
Also used : TemplateTypeMap(com.google.javascript.rhino.jstype.TemplateTypeMap) TemplateType(com.google.javascript.rhino.jstype.TemplateType)

Example 17 with TemplateType

use of com.google.javascript.rhino.jstype.TemplateType in project closure-compiler by google.

the class TypeInferenceTest method testInferringTypeInObjectPattern_fromTemplatizedProperty.

@Test
public void testInferringTypeInObjectPattern_fromTemplatizedProperty() {
    // create type Foo with one property templatized with type T
    TemplateType templateKey = registry.createTemplateType("T");
    FunctionType fooCtor = registry.createConstructorType("Foo", null, registry.createParameters(), null, ImmutableList.of(templateKey), false);
    ObjectType fooInstanceType = fooCtor.getInstanceType();
    fooInstanceType.defineDeclaredProperty("data", templateKey, null);
    // create a variable obj with type Foo<number>
    JSType fooOfNumber = templatize(fooInstanceType, ImmutableList.of(getNativeType(NUMBER_TYPE)));
    assuming("obj", fooOfNumber);
    inFunction(lines(// 
    "const {data} = obj;", "OBJ: obj;", "DATA: data"));
    assertTypeOfExpression("OBJ").toStringIsEqualTo("Foo<number>");
    assertTypeOfExpression("DATA").toStringIsEqualTo("number");
}
Also used : ObjectType(com.google.javascript.rhino.jstype.ObjectType) JSType(com.google.javascript.rhino.jstype.JSType) FunctionType(com.google.javascript.rhino.jstype.FunctionType) TemplateType(com.google.javascript.rhino.jstype.TemplateType) Test(org.junit.Test)

Aggregations

TemplateType (com.google.javascript.rhino.jstype.TemplateType)17 JSType (com.google.javascript.rhino.jstype.JSType)13 FunctionType (com.google.javascript.rhino.jstype.FunctionType)8 Node (com.google.javascript.rhino.Node)7 ObjectType (com.google.javascript.rhino.jstype.ObjectType)3 TemplateTypeMap (com.google.javascript.rhino.jstype.TemplateTypeMap)3 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 JSTypeExpression (com.google.javascript.rhino.JSTypeExpression)2 TemplatizedType (com.google.javascript.rhino.jstype.TemplatizedType)2 UnionType (com.google.javascript.rhino.jstype.UnionType)2 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)2 Set (java.util.Set)2 FlowScope (com.google.javascript.jscomp.type.FlowScope)1 BooleanLiteralSet (com.google.javascript.rhino.jstype.BooleanLiteralSet)1 JSTypeRegistry (com.google.javascript.rhino.jstype.JSTypeRegistry)1 TemplateTypeReplacer (com.google.javascript.rhino.jstype.TemplateTypeReplacer)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1