Search in sources :

Example 1 with TypeDeclarationNode

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

the class TypeSyntaxTest method assertVarType.

private void assertVarType(String message, TypeDeclarationNode expectedType, String source) {
    Node varDecl = parse(source, source).getFirstChild();
    assertDeclaredType(message, expectedType, varDecl.getFirstChild());
}
Also used : TypeDeclarationNode(com.google.javascript.rhino.Node.TypeDeclarationNode) NodeSubject.assertNode(com.google.javascript.jscomp.testing.NodeSubject.assertNode) Node(com.google.javascript.rhino.Node)

Example 2 with TypeDeclarationNode

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

the class TypeSyntaxTest method testCompositeType.

public void testCompositeType() {
    Node varDecl = parse("var foo: mymod.ns.Type;").getFirstChild();
    TypeDeclarationNode expected = namedType(ImmutableList.of("mymod", "ns", "Type"));
    assertDeclaredType("mymod.ns.Type", expected, varDecl.getFirstChild());
}
Also used : TypeDeclarationNode(com.google.javascript.rhino.Node.TypeDeclarationNode) NodeSubject.assertNode(com.google.javascript.jscomp.testing.NodeSubject.assertNode) Node(com.google.javascript.rhino.Node) TypeDeclarationNode(com.google.javascript.rhino.Node.TypeDeclarationNode)

Example 3 with TypeDeclarationNode

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

the class TypeSyntaxTest method testArrayType_qualifiedType.

public void testArrayType_qualifiedType() {
    TypeDeclarationNode arrayOfString = arrayType(namedType("mymod.ns.Type"));
    assertVarType("string[]", arrayOfString, "var foo: mymod.ns.Type[];");
}
Also used : TypeDeclarationNode(com.google.javascript.rhino.Node.TypeDeclarationNode)

Example 4 with TypeDeclarationNode

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

the class TypeSyntaxTest method testFunctionType.

public void testFunctionType() {
    parse("var n: (p1) => boolean;");
    parse("var n: (p1, p2) => boolean;");
    parse("var n: (p1: string) => boolean;");
    parse("var n: (p1: string, p2: number) => boolean;");
    parse("var n: () => () => number;");
    parse("var n: (p1: string) => {};");
    // parse("(number): () => number => number;");
    Node ast = parse("var n: (p1: string, p2: number) => boolean[];");
    TypeDeclarationNode function = (TypeDeclarationNode) (ast.getFirstFirstChild().getProp(Node.DECLARED_TYPE_EXPR));
    assertNode(function).hasType(Token.FUNCTION_TYPE);
    Node ast2 = parse("var n: (p1: string, p2: number) => boolean | number;");
    TypeDeclarationNode function2 = (TypeDeclarationNode) (ast2.getFirstFirstChild().getProp(Node.DECLARED_TYPE_EXPR));
    assertNode(function2).hasType(Token.FUNCTION_TYPE);
    Node ast3 = parse("var n: (p1: string, p2: number) => Array<Foo>;");
    TypeDeclarationNode function3 = (TypeDeclarationNode) (ast3.getFirstFirstChild().getProp(Node.DECLARED_TYPE_EXPR));
    assertNode(function3).hasType(Token.FUNCTION_TYPE);
}
Also used : TypeDeclarationNode(com.google.javascript.rhino.Node.TypeDeclarationNode) NodeSubject.assertNode(com.google.javascript.jscomp.testing.NodeSubject.assertNode) Node(com.google.javascript.rhino.Node) TypeDeclarationNode(com.google.javascript.rhino.Node.TypeDeclarationNode)

Example 5 with TypeDeclarationNode

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

the class TypeDeclarationsIR method recordType.

/**
 * Represents a structural type.
 * Closure calls this a Record Type and accepts the syntax
 * {@code {myNum: number, myObject}}
 *
 * <p>Example:
 * <pre>
 * RECORD_TYPE
 *   STRING_KEY myNum
 *     NUMBER_TYPE
 *   STRING_KEY myObject
 * </pre>
 * @param properties a map from property name to property type
 * @return a new node representing the record type
 */
public static TypeDeclarationNode recordType(LinkedHashMap<String, TypeDeclarationNode> properties) {
    TypeDeclarationNode node = new TypeDeclarationNode(Token.RECORD_TYPE);
    for (Map.Entry<String, TypeDeclarationNode> prop : properties.entrySet()) {
        Node stringKey = IR.stringKey(prop.getKey());
        node.addChildToBack(stringKey);
        if (prop.getValue() != null) {
            stringKey.addChildToFront(prop.getValue());
        }
    }
    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)

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