Search in sources :

Example 56 with JSType

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

the class JSTypeReconserializer method reconcileObjectTypes.

private TypeProto reconcileObjectTypes(SeenTypeRecord seen) {
    TreeSet<String> debugTypenames = new TreeSet<>();
    LinkedHashSet<Integer> instancePointers = new LinkedHashSet<>();
    LinkedHashSet<Integer> prototypePointers = new LinkedHashSet<>();
    LinkedHashSet<Integer> ownProperties = new LinkedHashSet<>();
    boolean isClosureAssert = false;
    boolean isConstructor = false;
    boolean isInvalidating = false;
    boolean propertiesKeepOriginalName = false;
    for (JSType type : seen.jstypes) {
        ObjectType objType = checkNotNull(type.toMaybeObjectType(), type);
        if (this.serializationMode.includeDebugInfo()) {
            debugTypenames.add(debugNameOf(type));
        }
        if (objType.isFunctionType()) {
            FunctionType fnType = objType.toMaybeFunctionType();
            // appear on the AST, optimizations need to know that at runtime these types may be present.
            if (fnType.hasInstanceType() && fnType.getInstanceType() != null) {
                instancePointers.add(this.serializeType(fnType.getInstanceType()));
                prototypePointers.add(this.serializeType(fnType.getPrototype()));
                isConstructor |= fnType.isConstructor();
            }
            isClosureAssert |= isClosureAssert(fnType.getClosurePrimitive());
        }
        for (String ownProperty : objType.getOwnPropertyNames()) {
            // save space.
            if (shouldPropagatePropertyName.test(ownProperty)) {
                ownProperties.add(this.stringPoolBuilder.put(ownProperty));
            }
        }
        isInvalidating |= this.invalidatingTypes.isInvalidating(objType);
        /**
         * To support legacy code, property disambiguation never renames properties of enums (e.g. 'A'
         * in '/** @enum * / const E = {A: 0}`). In theory this would be safe to remove if we clean up
         * code depending on the lack of renaming
         */
        propertiesKeepOriginalName |= objType.isEnumType();
    }
    ObjectTypeProto.Builder objectProto = ObjectTypeProto.newBuilder().addAllInstanceType(instancePointers).addAllOwnProperty(ownProperties).addAllPrototype(prototypePointers).setClosureAssert(isClosureAssert).setIsInvalidating(isInvalidating).setMarkedConstructor(isConstructor).setPropertiesKeepOriginalName(propertiesKeepOriginalName).setUuid(seen.colorId.asByteString());
    if (!debugTypenames.isEmpty()) {
        objectProto.getDebugInfoBuilder().addAllTypename(debugTypenames);
    }
    return TypeProto.newBuilder().setObject(objectProto).build();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ObjectType(com.google.javascript.rhino.jstype.ObjectType) JSType(com.google.javascript.rhino.jstype.JSType) TreeSet(java.util.TreeSet) FunctionType(com.google.javascript.rhino.jstype.FunctionType)

Example 57 with JSType

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

the class SemanticReverseAbstractInterpreter method caseInstanceOf.

@CheckReturnValue
private FlowScope caseInstanceOf(Node left, Node right, FlowScope blindScope, Outcome outcome) {
    JSType leftType = getTypeIfRefinable(left, blindScope);
    if (leftType == null) {
        return blindScope;
    }
    JSType rightType = right.getJSType();
    ObjectType targetType = typeRegistry.getNativeObjectType(JSTypeNative.UNKNOWN_TYPE);
    if (rightType != null && rightType.isFunctionType()) {
        targetType = rightType.toMaybeFunctionType();
    }
    Visitor<JSType> visitor;
    if (outcome.isTruthy()) {
        visitor = new RestrictByTrueInstanceOfResultVisitor(targetType);
    } else {
        visitor = new RestrictByFalseInstanceOfResultVisitor(targetType);
    }
    return maybeRestrictName(blindScope, left, leftType, leftType.visit(visitor));
}
Also used : ObjectType(com.google.javascript.rhino.jstype.ObjectType) JSType(com.google.javascript.rhino.jstype.JSType) CheckReturnValue(com.google.errorprone.annotations.CheckReturnValue)

Example 58 with JSType

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

the class SemanticReverseAbstractInterpreter method caseEquality.

@CheckReturnValue
private FlowScope caseEquality(Node left, Node right, FlowScope blindScope, Function<TypePair, TypePair> merging) {
    // left type
    JSType leftType = getTypeIfRefinable(left, blindScope);
    boolean leftIsRefineable;
    if (leftType != null) {
        leftIsRefineable = true;
    } else {
        leftIsRefineable = false;
        leftType = left.getJSType();
    }
    // right type
    JSType rightType = getTypeIfRefinable(right, blindScope);
    boolean rightIsRefineable;
    if (rightType != null) {
        rightIsRefineable = true;
    } else {
        rightIsRefineable = false;
        rightType = right.getJSType();
    }
    // merged types
    TypePair merged = merging.apply(new TypePair(leftType, rightType));
    // creating new scope
    if (merged != null) {
        return maybeRestrictTwoNames(blindScope, left, leftType, leftIsRefineable ? merged.typeA : null, right, rightType, rightIsRefineable ? merged.typeB : null);
    }
    return blindScope;
}
Also used : JSType(com.google.javascript.rhino.jstype.JSType) TypePair(com.google.javascript.rhino.jstype.JSType.TypePair) CheckReturnValue(com.google.errorprone.annotations.CheckReturnValue)

Example 59 with JSType

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

the class InferJSDocInfoTest method testJSDocIsPropagatedToFunctionTypesFromMethodAssigments.

@Test
public void testJSDocIsPropagatedToFunctionTypesFromMethodAssigments() {
    // Given
    testSame(srcs(lines("/** @constructor */", "function Foo() {};", "", "/**", " * I'm a method.", " * @return {number} a", " */", "Foo.prototype.method = function() { };", "", // Just a hook to access type "Foo::method".
    "var x = Foo.prototype.method;")));
    JSType xType = inferredTypeOfName("x");
    assertThat(xType.toString()).isEqualTo("function(this:Foo): number");
    // Then
    assertThat(xType.getJSDocInfo().getBlockDescription()).isEqualTo("I'm a method.");
}
Also used : JSType(com.google.javascript.rhino.jstype.JSType) Test(org.junit.Test)

Example 60 with JSType

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

the class InferJSDocInfoTest method testJSDocIsPropagatedToScopedTypes.

@Test
public void testJSDocIsPropagatedToScopedTypes() {
    // Given
    testSame(srcs(lines("(() => {", "  /**", "   * I'm a scoped user class.", "   * @constructor", "   */", "  var Foo = function() {};", "", // Just a hook to access type "Foo".
    "  var x = new Foo();", "})();")));
    JSType xType = inferredTypeOfName("x");
    assertThat(xType.toString()).isEqualTo("Foo");
    // Then
    assertThat(xType.getJSDocInfo().getBlockDescription()).isEqualTo("I'm a scoped user class.");
}
Also used : JSType(com.google.javascript.rhino.jstype.JSType) Test(org.junit.Test)

Aggregations

JSType (com.google.javascript.rhino.jstype.JSType)447 Test (org.junit.Test)182 Node (com.google.javascript.rhino.Node)158 FunctionType (com.google.javascript.rhino.jstype.FunctionType)71 ObjectType (com.google.javascript.rhino.jstype.ObjectType)71 NodeSubject.assertNode (com.google.javascript.rhino.testing.NodeSubject.assertNode)30 JSDocInfo (com.google.javascript.rhino.JSDocInfo)19 TemplateType (com.google.javascript.rhino.jstype.TemplateType)14 FlowScope (com.google.javascript.jscomp.type.FlowScope)13 CheckReturnValue (com.google.errorprone.annotations.CheckReturnValue)11 JSTypeExpression (com.google.javascript.rhino.JSTypeExpression)9 UnionType (com.google.javascript.rhino.jstype.UnionType)9 JSTypeRegistry (com.google.javascript.rhino.jstype.JSTypeRegistry)8 TemplateTypeMap (com.google.javascript.rhino.jstype.TemplateTypeMap)8 LinkedHashMap (java.util.LinkedHashMap)8 ImmutableList (com.google.common.collect.ImmutableList)7 Color (com.google.javascript.jscomp.colors.Color)7 StaticTypedSlot (com.google.javascript.rhino.jstype.StaticTypedSlot)7 ArrayList (java.util.ArrayList)7 ImmutableMap (com.google.common.collect.ImmutableMap)6