Search in sources :

Example 6 with TypeDeclarationNode

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

the class TypeDeclarationsIR method functionType.

/**
 * Represents a function type.
 * Closure has syntax like {@code {function(string, boolean):number}}
 * Closure doesn't include parameter names. If the parameter types are unnamed,
 * arbitrary names can be substituted, eg. p1, p2, etc.
 *
 * <p>Example:
 * <pre>
 * FUNCTION_TYPE
 *   NUMBER_TYPE
 *   STRING_KEY p1 [declared_type_expr: STRING_TYPE]
 *   STRING_KEY p2 [declared_type_expr: BOOLEAN_TYPE]
 * </pre>
 * @param returnType the type returned by the function, possibly ANY_TYPE
 * @param requiredParams the names and types of the required parameters.
 * @param optionalParams the names and types of the optional parameters.
 * @param restName the name of the rest parameter, if any.
 * @param restType the type of the rest parameter, if any.
 */
public static TypeDeclarationNode functionType(Node returnType, LinkedHashMap<String, TypeDeclarationNode> requiredParams, LinkedHashMap<String, TypeDeclarationNode> optionalParams, String restName, TypeDeclarationNode restType) {
    TypeDeclarationNode node = new TypeDeclarationNode(Token.FUNCTION_TYPE, returnType);
    checkNotNull(requiredParams);
    checkNotNull(optionalParams);
    for (Map.Entry<String, TypeDeclarationNode> param : requiredParams.entrySet()) {
        Node name = IR.name(param.getKey());
        node.addChildToBack(maybeAddType(name, param.getValue()));
    }
    for (Map.Entry<String, TypeDeclarationNode> param : optionalParams.entrySet()) {
        Node name = IR.name(param.getKey());
        name.putBooleanProp(Node.OPT_ES6_TYPED, true);
        node.addChildToBack(maybeAddType(name, param.getValue()));
    }
    if (restName != null) {
        Node rest = new Node(Token.REST, IR.name(restName));
        node.addChildToBack(maybeAddType(rest, restType));
    }
    return node;
}
Also used : TypeDeclarationNode(com.google.javascript.rhino.Node.TypeDeclarationNode) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) TypeDeclarationNode(com.google.javascript.rhino.Node.TypeDeclarationNode)

Example 7 with TypeDeclarationNode

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

the class TypeDeclarationsIR method unionType.

/**
 * Represents a union type, which can be one of the given types.
 * Closure accepts syntax like {@code {(number|boolean)}}
 *
 * <p>Example:
 * <pre>
 * UNION_TYPE
 *   NUMBER_TYPE
 *   BOOLEAN_TYPE
 * </pre>
 * @param options the types which are accepted
 * @return a new node representing the union type
 */
public static TypeDeclarationNode unionType(Iterable<TypeDeclarationNode> options) {
    checkArgument(!Iterables.isEmpty(options), "union must have at least one option");
    TypeDeclarationNode node = new TypeDeclarationNode(Token.UNION_TYPE);
    for (Node option : options) {
        node.addChildToBack(option);
    }
    return node;
}
Also used : TypeDeclarationNode(com.google.javascript.rhino.Node.TypeDeclarationNode) TypeDeclarationNode(com.google.javascript.rhino.Node.TypeDeclarationNode)

Example 8 with TypeDeclarationNode

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

the class TypeDeclarationsIR method namedType.

/**
 * Produces a tree structure similar to the Rhino AST of a qualified name
 * expression, under a top-level NAMED_TYPE node.
 *
 * <p>Example:
 * <pre>
 * NAMED_TYPE
 *   NAME goog
 *     STRING ui
 *       STRING Window
 * </pre>
 */
public static TypeDeclarationNode namedType(Iterable<String> segments) {
    Iterator<String> segmentsIt = segments.iterator();
    Node node = IR.name(segmentsIt.next());
    while (segmentsIt.hasNext()) {
        node = IR.getprop(node, IR.string(segmentsIt.next()));
    }
    return new TypeDeclarationNode(Token.NAMED_TYPE, node);
}
Also used : TypeDeclarationNode(com.google.javascript.rhino.Node.TypeDeclarationNode) TypeDeclarationNode(com.google.javascript.rhino.Node.TypeDeclarationNode)

Example 9 with TypeDeclarationNode

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

the class Es6TypedToEs6Converter method maybeProcessOptionalProperty.

private Node maybeProcessOptionalProperty(Node n, Node type) {
    if (n.isOptionalEs6Typed()) {
        n.putBooleanProp(Node.OPT_ES6_TYPED, false);
        TypeDeclarationNode baseType = (TypeDeclarationNode) maybeCreateAnyType(n, type);
        type = TypeDeclarationsIR.unionType(ImmutableList.of(baseType, TypeDeclarationsIR.undefinedType()));
        type.useSourceInfoIfMissingFromForTree(baseType);
    } else {
        type = maybeCreateAnyType(n, type);
    }
    return convertWithLocation(type);
}
Also used : TypeDeclarationNode(com.google.javascript.rhino.Node.TypeDeclarationNode)

Example 10 with TypeDeclarationNode

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

the class Es6TypedToEs6Converter method convertMemberFunctionToMemberVariable.

private Node convertMemberFunctionToMemberVariable(Node member) {
    Node function = member.getFirstChild();
    Node memberVariable = Node.newString(Token.MEMBER_VARIABLE_DEF, member.getString());
    memberVariable.useSourceInfoFrom(member);
    if (!processedOverloads.contains(function)) {
        Node returnType = maybeCreateAnyType(function, function.getDeclaredTypeExpression());
        LinkedHashMap<String, TypeDeclarationNode> required = new LinkedHashMap<>();
        LinkedHashMap<String, TypeDeclarationNode> optional = new LinkedHashMap<>();
        String restName = null;
        TypeDeclarationNode restType = null;
        for (Node param : function.getSecondChild().children()) {
            if (param.isName()) {
                if (param.isOptionalEs6Typed()) {
                    optional.put(param.getString(), param.getDeclaredTypeExpression());
                } else {
                    required.put(param.getString(), param.getDeclaredTypeExpression());
                }
            } else if (param.isRest()) {
                restName = param.getFirstChild().getString();
                restType = param.getDeclaredTypeExpression();
            }
        }
        TypeDeclarationNode type = TypeDeclarationsIR.functionType(returnType, required, optional, restName, restType);
        memberVariable.setDeclaredTypeExpression(type);
    } else {
        memberVariable.setDeclaredTypeExpression(TypeDeclarationsIR.namedType("Function"));
    }
    memberVariable.putBooleanProp(Node.OPT_ES6_TYPED, function.isOptionalEs6Typed());
    member.replaceWith(memberVariable);
    NodeUtil.markFunctionsDeleted(member, compiler);
    return memberVariable;
}
Also used : TypeDeclarationNode(com.google.javascript.rhino.Node.TypeDeclarationNode) Node(com.google.javascript.rhino.Node) TypeDeclarationNode(com.google.javascript.rhino.Node.TypeDeclarationNode) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

TypeDeclarationNode (com.google.javascript.rhino.Node.TypeDeclarationNode)13 Node (com.google.javascript.rhino.Node)5 NodeSubject.assertNode (com.google.javascript.jscomp.testing.NodeSubject.assertNode)4 LinkedHashMap (java.util.LinkedHashMap)3 Map (java.util.Map)2