Search in sources :

Example 26 with TypeI

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

the class TypeMismatch method registerMismatch.

static void registerMismatch(List<TypeMismatch> mismatches, List<TypeMismatch> implicitInterfaceUses, TypeI found, TypeI required, JSError error) {
    // Don't register a mismatch for differences in null or undefined or if the
    // code didn't downcast.
    found = removeNullUndefinedAndTemplates(found);
    required = removeNullUndefinedAndTemplates(required);
    if (found.isSubtypeOf(required) || required.isSubtypeOf(found)) {
        boolean strictMismatch = !found.isSubtypeWithoutStructuralTyping(required) && !required.isSubtypeWithoutStructuralTyping(found);
        if (strictMismatch && bothAreNotTypeVariables(found, required)) {
            implicitInterfaceUses.add(new TypeMismatch(found, required, Suppliers.ofInstance(error)));
        }
        return;
    }
    if (bothAreNotTypeVariables(found, required)) {
        mismatches.add(new TypeMismatch(found, required, Suppliers.ofInstance(error)));
    }
    if (found.isFunctionType() && required.isFunctionType()) {
        FunctionTypeI fnTypeA = found.toMaybeFunctionType();
        FunctionTypeI fnTypeB = required.toMaybeFunctionType();
        Iterator<TypeI> paramItA = fnTypeA.getParameterTypes().iterator();
        Iterator<TypeI> paramItB = fnTypeB.getParameterTypes().iterator();
        while (paramItA.hasNext() && paramItB.hasNext()) {
            TypeMismatch.registerIfMismatch(mismatches, implicitInterfaceUses, paramItA.next(), paramItB.next(), error);
        }
        TypeMismatch.registerIfMismatch(mismatches, implicitInterfaceUses, fnTypeA.getReturnType(), fnTypeB.getReturnType(), error);
    }
}
Also used : TypeI(com.google.javascript.rhino.TypeI) ObjectTypeI(com.google.javascript.rhino.ObjectTypeI) FunctionTypeI(com.google.javascript.rhino.FunctionTypeI) FunctionTypeI(com.google.javascript.rhino.FunctionTypeI)

Example 27 with TypeI

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

the class JSType method acceptsArguments.

@Override
public final boolean acceptsArguments(List<? extends TypeI> argumentTypes) {
    checkState(this.isFunctionType());
    int numArgs = argumentTypes.size();
    FunctionType fnType = this.getFunTypeIfSingletonObj();
    if (numArgs < fnType.getMinArity() || numArgs > fnType.getMaxArity()) {
        return false;
    }
    for (int i = 0; i < numArgs; i++) {
        TypeI ithArgType = argumentTypes.get(i);
        JSType ithParamType = fnType.getFormalType(i);
        if (!ithArgType.isSubtypeOf(ithParamType)) {
            return false;
        }
    }
    return true;
}
Also used : FunctionTypeI(com.google.javascript.rhino.FunctionTypeI) TypeI(com.google.javascript.rhino.TypeI) ObjectTypeI(com.google.javascript.rhino.ObjectTypeI)

Example 28 with TypeI

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

the class DevirtualizePrototypeMethodsTest method checkTypeOfRewrittenMethods.

private void checkTypeOfRewrittenMethods() {
    TypeI thisType = getTypeAtPosition(0).toMaybeFunctionType().getInstanceType();
    FunctionTypeI fooType = getTypeAtPosition(1, 0, 0).toMaybeFunctionType();
    FunctionTypeI barType = getTypeAtPosition(2, 0, 0).toMaybeFunctionType();
    FunctionTypeI bazType = getTypeAtPosition(3, 0, 0).toMaybeFunctionType();
    TypeI fooResultType = getTypeAtPosition(5, 0);
    TypeI barResultType = getTypeAtPosition(6, 0);
    TypeI bazResultType = getTypeAtPosition(7, 0);
    TypeI number = fooResultType;
    TypeI receiver = fooType.getTypeOfThis();
    assertTrue("Expected number: " + number, number.isNumberValueType());
    // NOTE: OTI has the receiver as unknown, NTI has it as null.
    assertTrue("Expected null or unknown: " + receiver, receiver == null || receiver.isUnknownType());
    assertThat(barResultType).isEqualTo(number);
    // Check that foo's type is {function(A): number}
    assertThat(fooType.getParameterTypes()).containsExactly(thisType);
    assertThat(fooType.getReturnType()).isEqualTo(number);
    assertThat(fooType.getTypeOfThis()).isEqualTo(receiver);
    // Check that bar's type is {function(A, number): number}
    assertThat(barType.getParameterTypes()).containsExactly(thisType, number).inOrder();
    assertThat(barType.getReturnType()).isEqualTo(number);
    assertThat(barType.getTypeOfThis()).isEqualTo(receiver);
    // Check that baz's type is {function(A): undefined} in OTI and {function(A): ?} in NTI
    assertThat(bazType.getParameterTypes()).containsExactly(thisType);
    assertThat(bazType.getTypeOfThis()).isEqualTo(receiver);
    // TODO(sdh): NTI currently fails to infer the result of the baz() call (b/37351897)
    // so we handle it more carefully.  When methods are deferred, this should be changed
    // to check that it's exactly unknown.
    assertTrue("Expected undefined or unknown: " + bazResultType, bazResultType.isVoidType() || bazResultType.isUnknownType());
    assertTrue("Expected undefined: " + bazType.getReturnType(), bazType.getReturnType().isVoidType());
}
Also used : TypeI(com.google.javascript.rhino.TypeI) FunctionTypeI(com.google.javascript.rhino.FunctionTypeI) FunctionTypeI(com.google.javascript.rhino.FunctionTypeI)

Example 29 with TypeI

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

the class CheckAccessControls method getClassOfMethod.

/**
 * Gets the type of the class that "owns" a method, or null if
 * we know that its un-owned.
 */
private TypeI getClassOfMethod(Node n, Node parent) {
    checkState(n.isFunction(), n);
    if (parent.isAssign()) {
        Node lValue = parent.getFirstChild();
        if (NodeUtil.isGet(lValue)) {
            // We have an assignment of the form "a.b = ...".
            TypeI lValueType = lValue.getTypeI();
            if (lValueType != null && (lValueType.isConstructor() || lValueType.isInterface())) {
                // belongs to the "a.b" type.
                return (lValueType.toMaybeFunctionType()).getInstanceType();
            } else if (NodeUtil.isPrototypeProperty(lValue)) {
                return normalizeClassType(NodeUtil.getPrototypeClassName(lValue).getTypeI());
            } else {
                return normalizeClassType(lValue.getFirstChild().getTypeI());
            }
        } else {
            // type off the "a".
            return normalizeClassType(lValue.getTypeI());
        }
    } else if (NodeUtil.isFunctionDeclaration(n) || parent.isName()) {
        return normalizeClassType(n.getTypeI());
    } else if (parent.isStringKey() || parent.isGetterDef() || parent.isSetterDef()) {
        Node objectLitParent = parent.getGrandparent();
        if (!objectLitParent.isAssign()) {
            return null;
        }
        Node className = NodeUtil.getPrototypeClassName(objectLitParent.getFirstChild());
        if (className != null) {
            return normalizeClassType(className.getTypeI());
        }
    }
    return null;
}
Also used : Node(com.google.javascript.rhino.Node) TypeI(com.google.javascript.rhino.TypeI) ObjectTypeI(com.google.javascript.rhino.ObjectTypeI) FunctionTypeI(com.google.javascript.rhino.FunctionTypeI)

Example 30 with TypeI

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

the class CheckAccessControls method checkPrivatePropertyVisibility.

private void checkPrivatePropertyVisibility(NodeTraversal t, Node getprop, Node parent, boolean isClassType, TypeI ownerType) {
    if (isClassType && isValidPrivateConstructorAccess(parent)) {
        return;
    }
    // private access is not allowed outside the file from a different
    // enclosing class.
    TypeI accessedType = getprop.getFirstChild().getTypeI();
    String propertyName = getprop.getLastChild().getString();
    String readableTypeName = ownerType.equals(accessedType) ? typeRegistry.getReadableTypeName(getprop.getFirstChild()) : ownerType.toString();
    // TODO(tbreisacher): Should we also include the filename where ownerType is defined?
    compiler.report(t.makeError(getprop, BAD_PRIVATE_PROPERTY_ACCESS, propertyName, readableTypeName));
}
Also used : TypeI(com.google.javascript.rhino.TypeI) ObjectTypeI(com.google.javascript.rhino.ObjectTypeI) FunctionTypeI(com.google.javascript.rhino.FunctionTypeI)

Aggregations

TypeI (com.google.javascript.rhino.TypeI)37 ObjectTypeI (com.google.javascript.rhino.ObjectTypeI)24 FunctionTypeI (com.google.javascript.rhino.FunctionTypeI)23 Node (com.google.javascript.rhino.Node)10 TypeIRegistry (com.google.javascript.rhino.TypeIRegistry)4 LinkedHashMap (java.util.LinkedHashMap)4 JSType (com.google.javascript.jscomp.newtypes.JSType)3 JSDocInfo (com.google.javascript.rhino.JSDocInfo)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 IdentityHashMap (java.util.IdentityHashMap)2 Map (java.util.Map)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 DiGraphNode (com.google.javascript.jscomp.graph.DiGraph.DiGraphNode)1 Visibility (com.google.javascript.rhino.JSDocInfo.Visibility)1 JSDocInfoBuilder (com.google.javascript.rhino.JSDocInfoBuilder)1 JSTypeExpression (com.google.javascript.rhino.JSTypeExpression)1 Token (com.google.javascript.rhino.Token)1 ArrayList (java.util.ArrayList)1