Search in sources :

Example 1 with ObjectType

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

the class ClosureCompletionProposalCollector method collectProposalsFromType.

/**
   * Collect the completion proposal for a type.
   * @param map  The map in which completion proposals are inserted.
   * @param prefix  The last segment of the prefix.
   * @param alternateType  The type to analyze.
   * @param type  The union type which {@code alternateType} belongs to.
   */
private void collectProposalsFromType(Map<String, ClosureCompletionProposal> map, String prefix, JSType alternateType) {
    if (alternateType instanceof ObjectType) {
        ObjectType alternateObjectType = (ObjectType) alternateType;
        for (String propertyName : alternateObjectType.getPropertyNames()) {
            if (isValidForPrefix(propertyName, prefix) && isVisibleName(propertyName)) {
                ClosureCompletionProposal proposal = map.get(propertyName);
                if (proposal == null) {
                    // This is slightly unefficient, because we build the elementInfo before
                    // checking that the node is concrete.  But I'm not sure it is useful to
                    // complicate the code for this particular case.
                    JSElementInfo elementInfo = JSElementInfo.makeFromProperty(context.getCompilerRun(), alternateObjectType, propertyName);
                    if (isConcreteNode(elementInfo.getNode())) {
                        proposal = new ClosureCompletionProposal(context, propertyName, elementInfo);
                        map.put(propertyName, proposal);
                    }
                }
                if (proposal != null) {
                    proposal.addVisibility(getVisibilityOfProperty(alternateObjectType, propertyName));
                }
            }
        }
    }
}
Also used : ObjectType(com.google.javascript.rhino.jstype.ObjectType) JSElementInfo(net.vtst.ow.eclipse.js.closure.editor.JSElementInfo)

Example 2 with ObjectType

use of com.google.javascript.rhino.jstype.ObjectType 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 3 with ObjectType

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

the class JSElementInfo method makeFromPropertyOrNull.

/**
   * Creates a new {@code JSElementInfo} from a property.
   * @param run  The compiler run (to be used to retrieve further information).
   * @param type  The type the property may belong to.
   * @param propertyName  The name of the property.  It may exist or not.
   * @return  A new {@code JSElementInfo}, or null.
   */
public static JSElementInfo makeFromPropertyOrNull(CompilerRun run, JSType type, String propertyName) {
    if (type instanceof ObjectType) {
        ObjectType objectType = (ObjectType) type;
        if (objectType.hasProperty(propertyName)) {
            Node propertyNode = objectType.getPropertyNode(propertyName);
            JSType propertyType = objectType.getPropertyType(propertyName);
            if (propertyNode != null && propertyType != null) {
                return new JSElementInfo(run, propertyNode, propertyType, getJSDocInfoOfProperty(objectType, propertyName), true, false);
            }
        }
    }
    return null;
}
Also used : ObjectType(com.google.javascript.rhino.jstype.ObjectType) JSType(com.google.javascript.rhino.jstype.JSType) Node(com.google.javascript.rhino.Node)

Example 4 with ObjectType

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

the class CompilerRun method getTypeOfQualifiedName.

/**
   * Get the type of the object designated by a qualified name in a given scope.
   * @param scope  The scope to look in.
   * @param qualifiedName  The qualified name to look at.
   * @return  The type, or null.
   */
public static JSType getTypeOfQualifiedName(Scope scope, List<String> qualifiedName) {
    ListIterator<String> it = qualifiedName.listIterator();
    if (!it.hasNext())
        return null;
    JSType type = getTypeOfName(scope, it.next());
    while (it.hasNext()) {
        if (!(type instanceof ObjectType))
            return null;
        type = ((ObjectType) type).getPropertyType(it.next());
    }
    return type;
}
Also used : ObjectType(com.google.javascript.rhino.jstype.ObjectType) JSType(com.google.javascript.rhino.jstype.JSType)

Aggregations

ObjectType (com.google.javascript.rhino.jstype.ObjectType)4 JSType (com.google.javascript.rhino.jstype.JSType)3 Node (com.google.javascript.rhino.Node)2 JSDocInfo (com.google.javascript.rhino.JSDocInfo)1 FunctionType (com.google.javascript.rhino.jstype.FunctionType)1 JSElementInfo (net.vtst.ow.eclipse.js.closure.editor.JSElementInfo)1