Search in sources :

Example 6 with TypeI

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

the class ReplaceStrings method findMatchingClass.

/**
 * @return The Config object for the class match the specified type or null
 * if no match was found.
 */
private Config findMatchingClass(TypeI callClassType, Collection<String> declarationNames) {
    if (!callClassType.isBottom() && !callClassType.isSomeUnknownType()) {
        for (String declarationName : declarationNames) {
            String className = getClassFromDeclarationName(declarationName);
            TypeI methodClassType = registry.getGlobalType(className);
            if (methodClassType != null && callClassType.isSubtypeOf(methodClassType)) {
                return functions.get(declarationName);
            }
        }
    }
    return null;
}
Also used : TypeI(com.google.javascript.rhino.TypeI)

Example 7 with TypeI

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

the class FunctionType method acceptsArguments.

@Override
public boolean acceptsArguments(List<? extends TypeI> argumentTypes) {
    // NOTE(aravindpg): This code is essentially lifted from TypeCheck::visitParameterList,
    // but what small differences there are make it very painful to refactor out the shared code.
    Iterator<? extends TypeI> arguments = argumentTypes.iterator();
    Iterator<Node> parameters = this.getParameters().iterator();
    Node parameter = null;
    TypeI argument = null;
    while (arguments.hasNext() && (parameters.hasNext() || parameter != null && parameter.isVarArgs())) {
        // above implies that this must be a var_args function.
        if (parameters.hasNext()) {
            parameter = parameters.next();
        }
        argument = arguments.next();
        if (!argument.isSubtypeOf(parameter.getTypeI())) {
            return false;
        }
    }
    int numArgs = argumentTypes.size();
    return this.getMinArity() <= numArgs && numArgs <= this.getMaxArity();
}
Also used : Node(com.google.javascript.rhino.Node) FunctionTypeI(com.google.javascript.rhino.FunctionTypeI) TypeI(com.google.javascript.rhino.TypeI) ObjectTypeI(com.google.javascript.rhino.ObjectTypeI)

Example 8 with TypeI

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

the class CheckAccessControls method checkFinalClassOverrides.

/**
 * Checks if a constructor is trying to override a final class.
 */
private void checkFinalClassOverrides(NodeTraversal t, Node fn, Node parent) {
    checkState(fn.isFunction(), fn);
    TypeI type = fn.getTypeI().toMaybeFunctionType();
    if (type != null && type.isConstructor()) {
        TypeI finalParentClass = getSuperClassInstanceIfFinal(getClassOfMethod(fn, parent));
        if (finalParentClass != null) {
            compiler.report(t.makeError(fn, EXTEND_FINAL_CLASS, type.getDisplayName(), finalParentClass.getDisplayName()));
        }
    }
}
Also used : TypeI(com.google.javascript.rhino.TypeI) ObjectTypeI(com.google.javascript.rhino.ObjectTypeI) FunctionTypeI(com.google.javascript.rhino.FunctionTypeI)

Example 9 with TypeI

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

the class CollapseProperties method flattenNameRef.

/**
 * Replaces a GETPROP a.b.c with a NAME a$b$c.
 *
 * @param alias A flattened prefix name (e.g. "a$b")
 * @param n The GETPROP node corresponding to the original name (e.g. "a.b")
 * @param parent {@code n}'s parent
 * @param originalName String version of the property name.
 */
private void flattenNameRef(String alias, Node n, Node parent, String originalName) {
    Preconditions.checkArgument(n.isGetProp(), "Expected GETPROP, found %s. Node: %s", n.getToken(), n);
    // BEFORE:
    // getprop
    // getprop
    // name a
    // string b
    // string c
    // AFTER:
    // name a$b$c
    Node ref = NodeUtil.newName(compiler, alias, n, originalName);
    NodeUtil.copyNameAnnotations(n.getLastChild(), ref);
    if (parent.isCall() && n == parent.getFirstChild()) {
        // The node was a call target, we are deliberately flatten these as
        // we node the "this" isn't provided by the namespace. Mark it as such:
        parent.putBooleanProp(Node.FREE_CALL, true);
    }
    TypeI type = n.getTypeI();
    if (type != null) {
        ref.setTypeI(type);
    }
    parent.replaceChild(n, ref);
    compiler.reportChangeToEnclosingScope(ref);
}
Also used : Node(com.google.javascript.rhino.Node) TypeI(com.google.javascript.rhino.TypeI)

Example 10 with TypeI

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

the class EsNextToEs8Converter method visitObjectWithSpread.

/*
   * Convert '{first: b, c, ...spread, d: e, last}' to:
   *
   * Object.assign({}, {first:b, c}, spread, {d:e, last});
   */
private void visitObjectWithSpread(Node obj) {
    checkArgument(obj.isObjectLit());
    TypeI simpleObjectType = createType(addTypes, compiler.getTypeIRegistry(), JSTypeNative.EMPTY_OBJECT_LITERAL_TYPE);
    TypeI resultType = simpleObjectType;
    Node result = withType(IR.call(NodeUtil.newQName(compiler, "Object.assign")), resultType);
    // Add an empty target object literal so changes made by Object.assign will not affect any other
    // variables.
    result.addChildToBack(withType(IR.objectlit(), simpleObjectType));
    // An indicator whether the current last thing in the param list is an object literal to which
    // properties may be added.  Initialized to null since nothing should be added to the empty
    // object literal in first position of the param list.
    Node trailingObjectLiteral = null;
    for (Node child : obj.children()) {
        if (child.isSpread()) {
            // Add the object directly to the param list.
            Node spreaded = child.removeFirstChild();
            result.addChildToBack(spreaded);
            // Properties should not be added to the trailing object.
            trailingObjectLiteral = null;
        } else {
            if (trailingObjectLiteral == null) {
                // Add a new object to which properties may be added.
                trailingObjectLiteral = withType(IR.objectlit(), simpleObjectType);
                result.addChildToBack(trailingObjectLiteral);
            }
            // Add the property to the object literal.
            trailingObjectLiteral.addChildToBack(child.detach());
        }
    }
    result.useSourceInfoIfMissingFromForTree(obj);
    obj.replaceWith(result);
    compiler.reportChangeToEnclosingScope(result);
}
Also used : TypeI(com.google.javascript.rhino.TypeI) Node(com.google.javascript.rhino.Node)

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