Search in sources :

Example 31 with TypeI

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

the class CheckAccessControls method enterScope.

@Override
public void enterScope(NodeTraversal t) {
    Node n = t.getScopeRoot();
    if (n.isFunction()) {
        Node parent = n.getParent();
        if (isDeprecatedFunction(n)) {
            deprecatedDepth++;
        }
        TypeI prevClass = getCurrentClass();
        TypeI currentClass = prevClass == null ? getClassOfMethod(n, parent) : prevClass;
        // ArrayDeques can't handle nulls, so we reuse the bottom type
        // as a null sentinel.
        currentClassStack.addFirst(currentClass == null ? noTypeSentinel : currentClass);
    }
}
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 32 with TypeI

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

the class CheckAccessControls method checkProtectedPropertyVisibility.

private void checkProtectedPropertyVisibility(NodeTraversal t, Node getprop, TypeI ownerType) {
    // There are 3 types of legal accesses of a protected property:
    // 1) Accesses in the same file
    // 2) Overriding the property in a subclass
    // 3) Accessing the property from inside a subclass
    // The first two have already been checked for.
    TypeI currentClass = getCurrentClass();
    if (currentClass == null || !currentClass.isSubtypeOf(ownerType)) {
        String propertyName = getprop.getLastChild().getString();
        compiler.report(t.makeError(getprop, BAD_PROTECTED_PROPERTY_ACCESS, propertyName, typeRegistry.getReadableTypeName(getprop.getFirstChild())));
    }
}
Also used : TypeI(com.google.javascript.rhino.TypeI) ObjectTypeI(com.google.javascript.rhino.ObjectTypeI) FunctionTypeI(com.google.javascript.rhino.FunctionTypeI)

Example 33 with TypeI

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

the class Es6ForOfConverter method visitForOf.

private void visitForOf(NodeTraversal t, Node node, Node parent) {
    Node variable = node.removeFirstChild();
    Node iterable = node.removeFirstChild();
    Node body = node.removeFirstChild();
    TypeI typeParam = unknownType;
    if (addTypes) {
        // TODO(sdh): This is going to be null if the iterable is nullable or unknown. We might want
        // to consider some way of unifying rather than simply looking at the nominal type.
        ObjectTypeI iterableType = iterable.getTypeI().autobox().toMaybeObjectType();
        if (iterableType != null) {
            TypeIRegistry registry = compiler.getTypeIRegistry();
            TypeI iterableBaseType = registry.getNativeType(JSTypeNative.ITERABLE_TYPE);
            typeParam = iterableType.getInstantiatedTypeArgument(iterableBaseType);
        }
    }
    TypeI iteratorType = createGenericType(JSTypeNative.ITERATOR_TYPE, typeParam);
    TypeI iIterableResultType = createGenericType(JSTypeNative.I_ITERABLE_RESULT_TYPE, typeParam);
    TypeI iteratorNextType = addTypes ? iteratorType.toMaybeObjectType().getPropertyType("next") : null;
    JSDocInfo varJSDocInfo = variable.getJSDocInfo();
    Node iterName = withType(IR.name(ITER_BASE + compiler.getUniqueNameIdSupplier().get()), iteratorType);
    iterName.makeNonIndexable();
    Node getNext = withType(IR.call(withType(IR.getprop(iterName.cloneTree(), withStringType(IR.string("next"))), iteratorNextType)), iIterableResultType);
    String variableName;
    Token declType;
    if (variable.isName()) {
        declType = Token.NAME;
        variableName = variable.getQualifiedName();
    } else {
        Preconditions.checkState(NodeUtil.isNameDeclaration(variable), "Expected var, let, or const. Got %s", variable);
        declType = variable.getToken();
        variableName = variable.getFirstChild().getQualifiedName();
    }
    Node iterResult = withType(IR.name(ITER_RESULT + variableName), iIterableResultType);
    iterResult.makeNonIndexable();
    Node call = Es6ToEs3Util.makeIterator(compiler, iterable);
    if (addTypes) {
        TypeI jscompType = t.getScope().getVar("$jscomp").getNode().getTypeI();
        TypeI makeIteratorType = jscompType.toMaybeObjectType().getPropertyType("makeIterator");
        call.getFirstChild().setTypeI(makeIteratorType);
        call.getFirstFirstChild().setTypeI(jscompType);
    }
    Node init = IR.var(iterName.cloneTree(), withType(call, iteratorType));
    Node initIterResult = iterResult.cloneTree();
    initIterResult.addChildToFront(getNext.cloneTree());
    init.addChildToBack(initIterResult);
    Node cond = withBooleanType(IR.not(withBooleanType(IR.getprop(iterResult.cloneTree(), withStringType(IR.string("done"))))));
    Node incr = withType(IR.assign(iterResult.cloneTree(), getNext.cloneTree()), iIterableResultType);
    Node declarationOrAssign;
    if (declType == Token.NAME) {
        declarationOrAssign = withType(IR.assign(withType(IR.name(variableName).useSourceInfoFrom(variable), typeParam), withType(IR.getprop(iterResult.cloneTree(), withStringType(IR.string("value"))), typeParam)), typeParam);
        declarationOrAssign.setJSDocInfo(varJSDocInfo);
        declarationOrAssign = IR.exprResult(declarationOrAssign);
    } else {
        declarationOrAssign = new Node(declType, withType(IR.name(variableName).useSourceInfoFrom(variable.getFirstChild()), typeParam));
        declarationOrAssign.getFirstChild().addChildToBack(withType(IR.getprop(iterResult.cloneTree(), withStringType(IR.string("value"))), typeParam));
        declarationOrAssign.setJSDocInfo(varJSDocInfo);
    }
    Node newBody = IR.block(declarationOrAssign, body).useSourceInfoFrom(body);
    Node newFor = IR.forNode(init, cond, incr, newBody);
    newFor.useSourceInfoIfMissingFromForTree(node);
    parent.replaceChild(node, newFor);
    compiler.reportChangeToEnclosingScope(newFor);
}
Also used : ObjectTypeI(com.google.javascript.rhino.ObjectTypeI) Node(com.google.javascript.rhino.Node) TypeI(com.google.javascript.rhino.TypeI) ObjectTypeI(com.google.javascript.rhino.ObjectTypeI) Token(com.google.javascript.rhino.Token) TypeIRegistry(com.google.javascript.rhino.TypeIRegistry) JSDocInfo(com.google.javascript.rhino.JSDocInfo)

Example 34 with TypeI

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

the class GlobalTypeInfo method createUnionType.

@Override
public TypeI createUnionType(List<? extends TypeI> members) {
    checkArgument(!members.isEmpty(), "Cannot create union type with no members");
    JSType result = commonTypes.BOTTOM;
    for (TypeI t : members) {
        result = JSType.join(result, (JSType) t);
    }
    return result;
}
Also used : JSType(com.google.javascript.jscomp.newtypes.JSType) TypeI(com.google.javascript.rhino.TypeI) ObjectTypeI(com.google.javascript.rhino.ObjectTypeI)

Example 35 with TypeI

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

the class NewTypeInference method instantiateCalleeMaybeWithTTL.

/**
 * Instantiate the generic function using the appropriate type map.
 *
 * If the generic function uses TTL, we need to map the TTL variables to types.
 * In this method, we find the TTL expressions, call TypeTransformation#eval to evaluate them,
 * and update the type map.
 */
private FunctionType instantiateCalleeMaybeWithTTL(FunctionType calleeType, ImmutableMap<String, JSType> typeMap) {
    Map<String, Node> typeTransformations = calleeType.getTypeTransformations();
    if (typeTransformations.isEmpty()) {
        return calleeType.instantiateGenerics(typeMap);
    }
    ImmutableMap<String, TypeI> mapWithOriginalNames = getTypemapWithOriginalNames(typeMap);
    LinkedHashMap<String, JSType> newTypeMap = new LinkedHashMap<>();
    newTypeMap.putAll(typeMap);
    for (Map.Entry<String, Node> entry : typeTransformations.entrySet()) {
        String ttlVar = entry.getKey();
        Node transform = entry.getValue();
        @SuppressWarnings({ "unchecked", "rawtypes" }) JSType t = (JSType) this.ttlObj.eval(transform, mapWithOriginalNames);
        newTypeMap.put(ttlVar, t);
    }
    return calleeType.instantiateGenerics(ImmutableMap.copyOf(newTypeMap));
}
Also used : JSType(com.google.javascript.jscomp.newtypes.JSType) Node(com.google.javascript.rhino.Node) DiGraphNode(com.google.javascript.jscomp.graph.DiGraph.DiGraphNode) TypeI(com.google.javascript.rhino.TypeI) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) LinkedHashMap(java.util.LinkedHashMap)

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