Search in sources :

Example 1 with JSTypeExpression

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

the class PolymerClassRewriter method appendPropertiesToBlock.

/**
 * Appends all properties in the ClassDefinition to the prototype of the custom element.
 */
private void appendPropertiesToBlock(final PolymerClassDefinition cls, Node block, String basePath) {
    for (MemberDefinition prop : cls.props) {
        Node propertyNode = IR.exprResult(NodeUtil.newQName(compiler, basePath + prop.name.getString()));
        // If a property string is quoted, make sure the added prototype properties are also quoted
        if (prop.name.isQuotedString()) {
            continue;
        }
        propertyNode.useSourceInfoIfMissingFromForTree(prop.name);
        JSDocInfoBuilder info = JSDocInfoBuilder.maybeCopyFrom(prop.info);
        JSTypeExpression propType = PolymerPassStaticUtils.getTypeFromProperty(prop, compiler);
        if (propType == null) {
            return;
        }
        info.recordType(propType);
        propertyNode.getFirstChild().setJSDocInfo(info.build());
        block.addChildToBack(propertyNode);
    }
}
Also used : Node(com.google.javascript.rhino.Node) JSTypeExpression(com.google.javascript.rhino.JSTypeExpression) JSDocInfoBuilder(com.google.javascript.rhino.JSDocInfoBuilder) MemberDefinition(com.google.javascript.jscomp.PolymerPass.MemberDefinition)

Example 2 with JSTypeExpression

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

the class NodeUtilTest method testGetDeclaredTypeExpression4.

public void testGetDeclaredTypeExpression4() {
    Node ast = parse("/** @param {number=} x */ function f(x = -1) {}");
    Node x = getNameNode(ast, "x");
    JSTypeExpression typeExpr = NodeUtil.getDeclaredTypeExpression(x);
    assertNode(typeExpr.getRoot()).hasType(Token.EQUALS);
    assertThat(typeExpr.getRoot().getFirstChild().getString()).isEqualTo("number");
}
Also used : Node(com.google.javascript.rhino.Node) NodeSubject.assertNode(com.google.javascript.jscomp.testing.NodeSubject.assertNode) JSTypeExpression(com.google.javascript.rhino.JSTypeExpression)

Example 3 with JSTypeExpression

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

the class NodeUtilTest method testGetDeclaredTypeExpression1.

public void testGetDeclaredTypeExpression1() {
    Node ast = parse("function f(/** string */ x) {}");
    Node x = getNameNode(ast, "x");
    JSTypeExpression typeExpr = NodeUtil.getDeclaredTypeExpression(x);
    assertThat(typeExpr.getRoot().getString()).isEqualTo("string");
}
Also used : Node(com.google.javascript.rhino.Node) NodeSubject.assertNode(com.google.javascript.jscomp.testing.NodeSubject.assertNode) JSTypeExpression(com.google.javascript.rhino.JSTypeExpression)

Example 4 with JSTypeExpression

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

the class JSTypeCreatorFromJSDoc method fillInFormalParameterTypes.

private void fillInFormalParameterTypes(JSDocInfo jsdoc, Node funNode, ImmutableList<String> typeParameters, DeclaredTypeRegistry registry, FunctionTypeBuilder builder, boolean ignoreJsdoc) /* for when the jsdoc is malformed */
{
    boolean ignoreFunNode = !funNode.isFunction();
    Node params = ignoreFunNode ? null : funNode.getSecondChild();
    ParamIterator iterator = new ParamIterator(params, jsdoc);
    while (iterator.hasNext()) {
        String pname = iterator.nextString();
        Node param = iterator.getNode();
        ParameterKind p = ParameterKind.REQUIRED;
        if (param != null && convention.isOptionalParameter(param)) {
            p = ParameterKind.OPTIONAL;
        } else if (param != null && convention.isVarArgsParameter(param)) {
            p = ParameterKind.REST;
        }
        ParameterType inlineParamType = (ignoreJsdoc || ignoreFunNode || param.getJSDocInfo() == null) ? null : parseParameter(param.getJSDocInfo().getType(), p, registry, typeParameters);
        ParameterType fnParamType = inlineParamType;
        JSTypeExpression jsdocExp = jsdoc == null ? null : jsdoc.getParameterType(pname);
        if (jsdocExp != null) {
            if (inlineParamType == null) {
                fnParamType = parseParameter(jsdocExp, p, registry, typeParameters);
            } else {
                warnings.add(JSError.make(param, TWO_JSDOCS, "formal parameter " + pname));
            }
        }
        JSType t = null;
        if (fnParamType != null) {
            p = fnParamType.kind;
            t = fnParamType.type;
        }
        switch(p) {
            case REQUIRED:
                builder.addReqFormal(t);
                break;
            case OPTIONAL:
                builder.addOptFormal(t);
                break;
            case REST:
                builder.addRestFormals(t != null ? t : this.commonTypes.UNKNOWN);
                break;
        }
    }
}
Also used : Node(com.google.javascript.rhino.Node) JSTypeExpression(com.google.javascript.rhino.JSTypeExpression)

Example 5 with JSTypeExpression

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

the class JSTypeCreatorFromJSDoc method resolveTypedef.

public void resolveTypedef(Typedef td, DeclaredTypeRegistry registry) {
    checkState(td != null, "getTypedef should only be called when we know that the typedef is defined");
    if (td.isResolved()) {
        return;
    }
    JSTypeExpression texp = td.getTypeExpr();
    JSType tdType;
    if (texp == null) {
        warnings.add(JSError.make(td.getTypeExprForErrorReporting().getRoot(), CIRCULAR_TYPEDEF_ENUM));
        tdType = this.commonTypes.UNKNOWN;
    } else {
        tdType = getTypeFromJSTypeExpression(texp, registry, null);
        // If the typedef is an object-literal type, record the names of the properties.
        if (tdType.isSingletonObj()) {
            Node texpRoot = texp.getRoot();
            if (texpRoot.getToken() == Token.LC) {
                for (Node propNode : texpRoot.getFirstChild().children()) {
                    Node propNameNode = propNode.hasChildren() ? propNode.getFirstChild() : propNode;
                    this.recordPropertyName.apply(propNameNode);
                }
            }
        }
    }
    td.resolveTypedef(tdType);
}
Also used : Node(com.google.javascript.rhino.Node) JSTypeExpression(com.google.javascript.rhino.JSTypeExpression)

Aggregations

JSTypeExpression (com.google.javascript.rhino.JSTypeExpression)101 Node (com.google.javascript.rhino.Node)67 JSDocInfo (com.google.javascript.rhino.JSDocInfo)58 Test (org.junit.Test)26 JSDocInfoBuilder (com.google.javascript.rhino.JSDocInfoBuilder)18 MemberDefinition (com.google.javascript.jscomp.PolymerPass.MemberDefinition)9 JSType (com.google.javascript.rhino.jstype.JSType)9 ArrayList (java.util.ArrayList)8 TypeDeclarationNode (com.google.javascript.rhino.Node.TypeDeclarationNode)7 Map (java.util.Map)6 NodeSubject.assertNode (com.google.javascript.jscomp.testing.NodeSubject.assertNode)4 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)4 ImmutableList (com.google.common.collect.ImmutableList)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 Visibility (com.google.javascript.rhino.JSDocInfo.Visibility)3 LinkedHashMap (java.util.LinkedHashMap)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 Name (com.google.javascript.jscomp.GlobalNamespace.Name)2 Ref (com.google.javascript.jscomp.GlobalNamespace.Ref)2