Search in sources :

Example 1 with Visibility

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

the class JsDocInfoParser method parseHelperLoop.

private boolean parseHelperLoop(JsDocToken token, List<ExtendedTypeInfo> extendedTypes) {
    while (true) {
        switch(token) {
            case ANNOTATION:
                if (state == State.SEARCHING_ANNOTATION) {
                    state = State.SEARCHING_NEWLINE;
                    token = parseAnnotation(token, extendedTypes);
                } else {
                    token = next();
                }
                break;
            case EOC:
                boolean success = true;
                // TODO(johnlenz): It should be a parse error to have an @extends
                // or similiar annotations in a file overview block.
                checkExtendedTypes(extendedTypes);
                if (hasParsedFileOverviewDocInfo()) {
                    fileOverviewJSDocInfo = retrieveAndResetParsedJSDocInfo();
                    Visibility visibility = fileOverviewJSDocInfo.getVisibility();
                    switch(visibility) {
                        // fallthrough
                        case PRIVATE:
                        case PROTECTED:
                            // PRIVATE and PROTECTED are not allowed in @fileoverview JsDoc.
                            addParserWarning("msg.bad.fileoverview.visibility.annotation", Ascii.toLowerCase(visibility.toString()));
                            success = false;
                            break;
                        default:
                            // in @fileoverview JsDoc.
                            break;
                    }
                }
                return success;
            case EOF:
                // discard any accumulated information
                jsdocBuilder.build();
                addParserWarning("msg.unexpected.eof");
                checkExtendedTypes(extendedTypes);
                return false;
            case EOL:
                if (state == State.SEARCHING_NEWLINE) {
                    state = State.SEARCHING_ANNOTATION;
                }
                token = next();
                break;
            default:
                if (token == JsDocToken.STAR && state == State.SEARCHING_ANNOTATION) {
                    token = next();
                } else {
                    state = State.SEARCHING_NEWLINE;
                    token = eatTokensUntilEOL();
                }
                break;
        }
    }
}
Also used : Visibility(com.google.javascript.rhino.JSDocInfo.Visibility)

Example 2 with Visibility

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

the class CheckAccessControls method checkPropertyVisibility.

/**
 * Reports an error if the given property is not visible in the current
 * context.
 * @param t The current traversal.
 * @param getprop The getprop node.
 */
private void checkPropertyVisibility(NodeTraversal t, Node getprop, Node parent) {
    JSDocInfo jsdoc = NodeUtil.getBestJSDocInfo(getprop);
    if (jsdoc != null && jsdoc.getSuppressions().contains("visibility")) {
        return;
    }
    ObjectTypeI referenceType = castToObject(dereference(getprop.getFirstChild().getTypeI()));
    String propertyName = getprop.getLastChild().getString();
    boolean isPrivateByConvention = isPrivateByConvention(propertyName);
    if (isPrivateByConvention && propertyIsDeclaredButNotPrivate(getprop, parent)) {
        compiler.report(t.makeError(getprop, CONVENTION_MISMATCH));
        return;
    }
    StaticSourceFile definingSource = AccessControlUtils.getDefiningSource(getprop, referenceType, propertyName);
    boolean isClassType = false;
    // Is this a normal property access, or are we trying to override
    // an existing property?
    boolean isOverride = jsdoc != null && (parent.isExprResult() || (parent.isAssign() && parent.getFirstChild() == getprop));
    ObjectTypeI objectType = AccessControlUtils.getObjectType(referenceType, isOverride, propertyName);
    Visibility fileOverviewVisibility = defaultVisibilityForFiles.get(definingSource);
    Visibility visibility = AccessControlUtils.getEffectivePropertyVisibility(getprop, referenceType, defaultVisibilityForFiles, enforceCodingConventions ? compiler.getCodingConvention() : null);
    if (isOverride) {
        Visibility overriding = getOverridingPropertyVisibility(parent);
        if (overriding != null) {
            checkOverriddenPropertyVisibilityMismatch(overriding, visibility, fileOverviewVisibility, t, getprop);
        }
    }
    if (objectType != null) {
        Node node = objectType.getOwnPropertyDefSite(propertyName);
        if (node == null) {
            // Assume the property is public.
            return;
        }
        definingSource = node.getStaticSourceFile();
        isClassType = objectType.getOwnPropertyJSDocInfo(propertyName).isConstructor();
    } else if (isPrivateByConvention) {
        // We can only check visibility references if we know what file
        // it was defined in.
        objectType = referenceType;
    } else if (fileOverviewVisibility == null) {
        // Otherwise just assume the property is public.
        return;
    }
    StaticSourceFile referenceSource = getprop.getStaticSourceFile();
    if (isOverride) {
        boolean sameInput = referenceSource != null && referenceSource.getName().equals(definingSource.getName());
        checkOverriddenPropertyVisibility(t, getprop, parent, visibility, fileOverviewVisibility, objectType, sameInput);
    } else {
        checkNonOverriddenPropertyVisibility(t, getprop, parent, visibility, isClassType, objectType, referenceSource, definingSource);
    }
}
Also used : ObjectTypeI(com.google.javascript.rhino.ObjectTypeI) Node(com.google.javascript.rhino.Node) Visibility(com.google.javascript.rhino.JSDocInfo.Visibility) JSDocInfo(com.google.javascript.rhino.JSDocInfo) StaticSourceFile(com.google.javascript.rhino.StaticSourceFile)

Example 3 with Visibility

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

the class CheckAccessControls method checkKeyVisibilityConvention.

/**
 * Determines whether the given OBJECTLIT property visibility
 * violates the coding convention.
 * @param t The current traversal.
 * @param key The objectlit key node (STRING_KEY, GETTER_DEF, SETTER_DEF).
 */
private void checkKeyVisibilityConvention(NodeTraversal t, Node key, Node parent) {
    JSDocInfo info = key.getJSDocInfo();
    if (info == null) {
        return;
    }
    if (!isPrivateByConvention(key.getString())) {
        return;
    }
    Node assign = parent.getParent();
    if (assign == null || !assign.isAssign()) {
        return;
    }
    Node left = assign.getFirstChild();
    if (!left.isGetProp() || !left.getLastChild().getString().equals("prototype")) {
        return;
    }
    Visibility declaredVisibility = info.getVisibility();
    // Visibility is declared to be something other than private.
    if (declaredVisibility != Visibility.INHERITED && declaredVisibility != Visibility.PRIVATE) {
        compiler.report(t.makeError(key, CONVENTION_MISMATCH));
    }
}
Also used : Node(com.google.javascript.rhino.Node) Visibility(com.google.javascript.rhino.JSDocInfo.Visibility) JSDocInfo(com.google.javascript.rhino.JSDocInfo)

Example 4 with Visibility

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

the class CheckAccessControls method checkNameVisibility.

/**
 * Reports an error if the given name is not visible in the current context.
 * @param t The current traversal.
 * @param name The name node.
 */
private void checkNameVisibility(NodeTraversal t, Node name, Node parent) {
    Var var = t.getScope().getVar(name.getString());
    if (var == null) {
        return;
    }
    Visibility v = checkPrivateNameConvention(AccessControlUtils.getEffectiveNameVisibility(name, var, defaultVisibilityForFiles), name);
    switch(v) {
        case PACKAGE:
            if (!isPackageAccessAllowed(var, name)) {
                compiler.report(t.makeError(name, BAD_PACKAGE_PROPERTY_ACCESS, name.getString(), var.getSourceFile().getName()));
            }
            break;
        case PRIVATE:
            if (!isPrivateAccessAllowed(var, name, parent)) {
                compiler.report(t.makeError(name, BAD_PRIVATE_GLOBAL_ACCESS, name.getString(), var.getSourceFile().getName()));
            }
            break;
        default:
            // (which is irrelevant for names).
            break;
    }
}
Also used : Visibility(com.google.javascript.rhino.JSDocInfo.Visibility)

Example 5 with Visibility

use of com.google.javascript.rhino.JSDocInfo.Visibility 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

Visibility (com.google.javascript.rhino.JSDocInfo.Visibility)12 JSDocInfo (com.google.javascript.rhino.JSDocInfo)7 Node (com.google.javascript.rhino.Node)4 ObjectTypeI (com.google.javascript.rhino.ObjectTypeI)4 JSDocInfoBuilder (com.google.javascript.rhino.JSDocInfoBuilder)2 JSTypeExpression (com.google.javascript.rhino.JSTypeExpression)2 StaticSourceFile (com.google.javascript.rhino.StaticSourceFile)2 TypeI (com.google.javascript.rhino.TypeI)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1