Search in sources :

Example 1 with FunctionType

use of com.google.javascript.rhino.jstype.FunctionType in project ow by vtst.

the class JSElementInfo method writeFunctionInfo.

/**
   * @param fnNode A node for a function for which to generate a type annotation
   */
private void writeFunctionInfo(Node fnNode) {
    if (fnNode == null)
        return;
    Preconditions.checkState(fnNode.getType() == Token.FUNCTION);
    JSDocInfo fnDocInfo = NodeUtil.getFunctionJSDocInfo(fnNode);
    JSType type = fnNode.getJSType();
    if (type == null || type.isUnknownType()) {
        return;
    }
    FunctionType funType = type.toMaybeFunctionType();
    //     NAME param2
    if (fnNode != null) {
        openSection(messages.getString("jsdoc_parameters"));
        Node paramNode = NodeUtil.getFunctionParameters(fnNode).getFirstChild();
        // Param types
        for (Node n : funType.getParameters()) {
            // Bail out if the paramNode is not there.
            if (paramNode == null) {
                break;
            }
            openItem();
            writeParameter(paramNode, n, fnDocInfo.getDescriptionForParameter(paramNode.getString()));
            closeItem();
            paramNode = paramNode.getNext();
        }
        closeSection();
    }
    // Return type
    JSType retType = funType.getReturnType();
    if (retType != null && !retType.isUnknownType() && !retType.isEmptyType()) {
        openSectionAndItem(messages.getString("jsdoc_return"));
        writeType(retType);
        writeTypeDescription(fnDocInfo.getReturnDescription());
        closeSectionAndItem();
    }
    // Constructor/interface
    if (funType.isConstructor() || funType.isInterface()) {
        FunctionType superConstructor = funType.getSuperClassConstructor();
        if (superConstructor != null) {
            ObjectType superInstance = funType.getSuperClassConstructor().getInstanceType();
            if (!superInstance.toString().equals("Object")) {
                openSectionAndItem(messages.getString("jsdoc_extends"));
                buf.append(superInstance.toString());
                closeSectionAndItem();
            }
        }
        if (funType.isInterface()) {
            for (ObjectType interfaceType : funType.getExtendedInterfaces()) {
                openSectionAndItem(messages.getString("jsdoc_extends"));
                buf.append(interfaceType.toString());
                closeSectionAndItem();
            }
        }
        // Avoid duplicates, add implemented type to a set first
        Set<String> interfaces = Sets.newTreeSet();
        for (ObjectType interfaze : funType.getImplementedInterfaces()) {
            interfaces.add(interfaze.toString());
        }
        if (!interfaces.isEmpty()) {
            openSectionAndItem(messages.getString("jsdoc_implements"));
            boolean first = true;
            for (String interfaze : interfaces) {
                if (first)
                    first = false;
                else
                    buf.append("<p>");
                buf.append(interfaze.toString());
            }
            closeSectionAndItem();
        }
    }
}
Also used : ObjectType(com.google.javascript.rhino.jstype.ObjectType) JSType(com.google.javascript.rhino.jstype.JSType) FunctionType(com.google.javascript.rhino.jstype.FunctionType) Node(com.google.javascript.rhino.Node) JSDocInfo(com.google.javascript.rhino.JSDocInfo)

Example 2 with FunctionType

use of com.google.javascript.rhino.jstype.FunctionType in project ow by vtst.

the class ClosureCompletionProposal method addFragmentsForFunctionParameters.

/**
   * Add the fragments for the parameters of a function.
   * @param list  The list to which the fragments will be added.
   * @param fnNode  The function node.
   */
private void addFragmentsForFunctionParameters(List<Fragment> list, Node fnNode) {
    if (fnNode == null)
        return;
    Preconditions.checkState(fnNode.getType() == Token.FUNCTION);
    JSType type = fnNode.getJSType();
    if (type == null || type.isUnknownType())
        return;
    FunctionType funType = type.toMaybeFunctionType();
    Node paramNode = NodeUtil.getFunctionParameters(fnNode).getFirstChild();
    list.add(new Fragment("("));
    boolean first = true;
    for (@SuppressWarnings("unused") Node parameterTypeNode : funType.getParameters()) {
        // Bail out if the paramNode is not there.
        if (paramNode == null)
            break;
        if (first)
            first = false;
        else
            list.add(new Fragment(", "));
        list.add(new LinkedFragment(paramNode.getString()));
        paramNode = paramNode.getNext();
    }
    list.add(new Fragment(")"));
}
Also used : JSType(com.google.javascript.rhino.jstype.JSType) FunctionType(com.google.javascript.rhino.jstype.FunctionType) Node(com.google.javascript.rhino.Node)

Aggregations

Node (com.google.javascript.rhino.Node)2 FunctionType (com.google.javascript.rhino.jstype.FunctionType)2 JSType (com.google.javascript.rhino.jstype.JSType)2 JSDocInfo (com.google.javascript.rhino.JSDocInfo)1 ObjectType (com.google.javascript.rhino.jstype.ObjectType)1