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());
}
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());
}
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[];");
}
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);
}
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;
}
Aggregations