Search in sources :

Example 6 with JSTypeExpression

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

the class JSTypeCreatorFromJSDoc method fillInReturnType.

private void fillInReturnType(JSDocInfo jsdoc, Node funNode, Node parent, ImmutableList<String> typeParameters, DeclaredTypeRegistry registry, FunctionTypeBuilder builder, boolean ignoreJsdoc) /* for when the jsdoc is malformed */
{
    JSDocInfo inlineRetJsdoc = ignoreJsdoc || !funNode.isFunction() ? null : funNode.getFirstChild().getJSDocInfo();
    JSTypeExpression retTypeExp = jsdoc == null ? null : jsdoc.getReturnType();
    if (parent.isSetterDef() && retTypeExp == null) {
        // inline returns for getters/setters are not parsed
        builder.addRetType(this.commonTypes.UNDEFINED);
    } else if (inlineRetJsdoc != null) {
        builder.addRetType(getDeclaredTypeOfNode(inlineRetJsdoc, registry, typeParameters));
        if (retTypeExp != null) {
            warnings.add(JSError.make(funNode, TWO_JSDOCS, "the return type"));
        }
    } else {
        builder.addRetType(getTypeFromJSTypeExpression(retTypeExp, registry, typeParameters));
    }
}
Also used : JSTypeExpression(com.google.javascript.rhino.JSTypeExpression) JSDocInfo(com.google.javascript.rhino.JSDocInfo)

Example 7 with JSTypeExpression

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

the class JSTypeCreatorFromJSDoc method isRestArg.

// /** @param {...?} var_args */ function f(var_args) { ... }
// var_args shouldn't be used in the body of f
public static boolean isRestArg(JSDocInfo funJsdoc, String formalParamName) {
    if (funJsdoc == null) {
        return false;
    }
    JSTypeExpression texp = funJsdoc.getParameterType(formalParamName);
    Node jsdocNode = texp == null ? null : texp.getRoot();
    return jsdocNode != null && jsdocNode.getToken() == Token.ELLIPSIS;
}
Also used : Node(com.google.javascript.rhino.Node) JSTypeExpression(com.google.javascript.rhino.JSTypeExpression)

Example 8 with JSTypeExpression

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

the class JSTypeCreatorFromJSDoc method resolveEnum.

public void resolveEnum(EnumType e, DeclaredTypeRegistry registry) {
    checkState(e != null, "getEnum should only be called when we know that the enum is defined");
    if (e.isResolved()) {
        return;
    }
    JSTypeExpression texp = e.getTypeExpr();
    JSType enumeratedType;
    if (texp == null) {
        warnings.add(JSError.make(e.getTypeExprForErrorReporting().getRoot(), CIRCULAR_TYPEDEF_ENUM));
        enumeratedType = this.commonTypes.UNKNOWN;
    } else {
        int numTypeVars = howmanyTypeVars;
        enumeratedType = getTypeFromJSTypeExpression(texp, registry, null);
        if (howmanyTypeVars > numTypeVars) {
            warnings.add(JSError.make(texp.getRoot(), ENUM_WITH_TYPEVARS));
            enumeratedType = this.commonTypes.UNKNOWN;
            howmanyTypeVars = numTypeVars;
        } else if (enumeratedType.isTop()) {
            warnings.add(JSError.make(texp.getRoot(), ENUM_IS_TOP));
            enumeratedType = this.commonTypes.UNKNOWN;
        } else if (enumeratedType.isUnion()) {
            warnings.add(JSError.make(texp.getRoot(), ENUM_IS_UNION));
            enumeratedType = this.commonTypes.UNKNOWN;
        }
    }
    e.resolveEnum(enumeratedType);
}
Also used : JSTypeExpression(com.google.javascript.rhino.JSTypeExpression)

Example 9 with JSTypeExpression

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

the class CheckJSDoc method validateDefaultValue.

/**
 * Check that an arrow function is not annotated with {@constructor}.
 */
private void validateDefaultValue(Node n, JSDocInfo info) {
    if (n.isDefaultValue() && n.getParent().isParamList() && info != null) {
        JSTypeExpression typeExpr = info.getType();
        if (typeExpr == null) {
            return;
        }
        Node typeNode = typeExpr.getRoot();
        if (typeNode.getToken() != Token.EQUALS) {
            report(typeNode, DEFAULT_PARAM_MUST_BE_MARKED_OPTIONAL);
        }
    }
}
Also used : Node(com.google.javascript.rhino.Node) JSTypeExpression(com.google.javascript.rhino.JSTypeExpression)

Example 10 with JSTypeExpression

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

the class Es6RewriteClass method updateClassJsDoc.

/**
 * @param ctorInfo the JSDocInfo from the constructor method of the ES6 class.
 * @param newInfo the JSDocInfo that will be added to the constructor function in the ES3 output
 */
private void updateClassJsDoc(@Nullable JSDocInfo ctorInfo, JSDocInfoBuilder newInfo) {
    // Classes are @struct by default.
    if (!newInfo.isUnrestrictedRecorded() && !newInfo.isDictRecorded() && !newInfo.isStructRecorded()) {
        newInfo.recordStruct();
    }
    if (ctorInfo != null) {
        if (!ctorInfo.getSuppressions().isEmpty()) {
            newInfo.recordSuppressions(ctorInfo.getSuppressions());
        }
        for (String param : ctorInfo.getParameterNames()) {
            newInfo.recordParameter(param, ctorInfo.getParameterType(param));
            newInfo.recordParameterDescription(param, ctorInfo.getDescriptionForParameter(param));
        }
        for (JSTypeExpression thrown : ctorInfo.getThrownTypes()) {
            newInfo.recordThrowType(thrown);
            newInfo.recordThrowDescription(thrown, ctorInfo.getThrowsDescriptionForType(thrown));
        }
        JSDocInfo.Visibility visibility = ctorInfo.getVisibility();
        if (visibility != null && visibility != JSDocInfo.Visibility.INHERITED) {
            newInfo.recordVisibility(visibility);
        }
        if (ctorInfo.isDeprecated()) {
            newInfo.recordDeprecated();
        }
        if (ctorInfo.getDeprecationReason() != null && !newInfo.isDeprecationReasonRecorded()) {
            newInfo.recordDeprecationReason(ctorInfo.getDeprecationReason());
        }
        newInfo.mergePropertyBitfieldFrom(ctorInfo);
        for (String templateType : ctorInfo.getTemplateTypeNames()) {
            newInfo.recordTemplateTypeName(templateType);
        }
    }
}
Also used : JSTypeExpression(com.google.javascript.rhino.JSTypeExpression) Visibility(com.google.javascript.rhino.JSDocInfo.Visibility) JSDocInfo(com.google.javascript.rhino.JSDocInfo)

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