Search in sources :

Example 1 with ObjectTypeI

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

the class TypedCodeGenerator method appendConstructorAnnotations.

// TODO(dimvar): it's awkward that we print @constructor after the extends/implements;
// we should print it first, like users write it. Same for @interface and @record.
private void appendConstructorAnnotations(StringBuilder sb, FunctionTypeI funType) {
    FunctionTypeI superConstructor = funType.getInstanceType().getSuperClassConstructor();
    if (superConstructor != null) {
        ObjectTypeI superInstance = superConstructor.getInstanceType();
        if (!superInstance.toString().equals("Object")) {
            sb.append(" * ");
            appendAnnotation(sb, "extends", superInstance.toAnnotationString(Nullability.IMPLICIT));
            sb.append("\n");
        }
    }
    // Avoid duplicates, add implemented type to a set first
    Set<String> interfaces = new TreeSet<>();
    for (ObjectTypeI interfaze : funType.getAncestorInterfaces()) {
        interfaces.add(interfaze.toAnnotationString(Nullability.IMPLICIT));
    }
    for (String interfaze : interfaces) {
        sb.append(" * ");
        appendAnnotation(sb, "implements", interfaze);
        sb.append("\n");
    }
    sb.append(" * @constructor\n");
}
Also used : ObjectTypeI(com.google.javascript.rhino.ObjectTypeI) TreeSet(java.util.TreeSet) FunctionTypeI(com.google.javascript.rhino.FunctionTypeI)

Example 2 with ObjectTypeI

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

the class CheckAccessControls method getTypeDeprecationInfo.

/**
 * Returns the deprecation reason for the type if it is marked
 * as being deprecated. Returns empty string if the type is deprecated
 * but no reason was given. Returns null if the type is not deprecated.
 */
private static String getTypeDeprecationInfo(TypeI type) {
    if (type == null) {
        return null;
    }
    String depReason = getDeprecationReason(type.getJSDocInfo());
    if (depReason != null) {
        return depReason;
    }
    ObjectTypeI objType = castToObject(type);
    if (objType != null) {
        ObjectTypeI implicitProto = objType.getPrototypeObject();
        if (implicitProto != null) {
            return getTypeDeprecationInfo(implicitProto);
        }
    }
    return null;
}
Also used : ObjectTypeI(com.google.javascript.rhino.ObjectTypeI)

Example 3 with ObjectTypeI

use of com.google.javascript.rhino.ObjectTypeI 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 4 with ObjectTypeI

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

the class CheckAccessControls method checkPropertyDeprecation.

/**
 * Checks the given GETPROP node to ensure that access restrictions are
 * obeyed.
 */
private void checkPropertyDeprecation(NodeTraversal t, Node n, Node parent) {
    // Don't bother checking constructors.
    if (parent.isNew()) {
        return;
    }
    ObjectTypeI objectType = castToObject(dereference(n.getFirstChild().getTypeI()));
    String propertyName = n.getLastChild().getString();
    if (objectType != null) {
        String deprecationInfo = getPropertyDeprecationInfo(objectType, propertyName);
        if (deprecationInfo != null && shouldEmitDeprecationWarning(t, n, parent)) {
            if (!deprecationInfo.isEmpty()) {
                compiler.report(t.makeError(n, DEPRECATED_PROP_REASON, propertyName, typeRegistry.getReadableTypeName(n.getFirstChild()), deprecationInfo));
            } else {
                compiler.report(t.makeError(n, DEPRECATED_PROP, propertyName, typeRegistry.getReadableTypeName(n.getFirstChild())));
            }
        }
    }
}
Also used : ObjectTypeI(com.google.javascript.rhino.ObjectTypeI)

Example 5 with ObjectTypeI

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

the class AccessControlUtils method getEffectivePropertyVisibility.

/**
 * Returns the effective visibility of the given property. This can differ
 * from the property's declared visibility if the property is inherited from
 * a superclass, or if the file's {@code @fileoverview} JsDoc specifies
 * a default visibility.
 *
 * @param property The property to compute effective visibility for.
 * @param referenceType The JavaScript type of the property.
 * @param fileVisibilityMap A map of {@code @fileoverview} visibility
 *     annotations, used to compute the property's default visibility.
 * @param codingConvention The coding convention in effect (if any),
 *     used to determine whether the property is private by lexical convention
 *     (example: trailing underscore).
 */
static Visibility getEffectivePropertyVisibility(Node property, ObjectTypeI referenceType, ImmutableMap<StaticSourceFile, Visibility> fileVisibilityMap, @Nullable CodingConvention codingConvention) {
    String propertyName = property.getLastChild().getString();
    StaticSourceFile definingSource = getDefiningSource(property, referenceType, propertyName);
    Visibility fileOverviewVisibility = fileVisibilityMap.get(definingSource);
    Node parent = property.getParent();
    boolean isOverride = parent.getJSDocInfo() != null && parent.isAssign() && parent.getFirstChild() == property;
    ObjectTypeI objectType = getObjectType(referenceType, isOverride, propertyName);
    if (isOverride) {
        Visibility overridden = getOverriddenPropertyVisibility(objectType, propertyName);
        return getEffectiveVisibilityForOverriddenProperty(overridden, fileOverviewVisibility, propertyName, codingConvention);
    } else {
        return getEffectiveVisibilityForNonOverriddenProperty(property, objectType, fileOverviewVisibility, codingConvention);
    }
}
Also used : ObjectTypeI(com.google.javascript.rhino.ObjectTypeI) Node(com.google.javascript.rhino.Node) Visibility(com.google.javascript.rhino.JSDocInfo.Visibility) StaticSourceFile(com.google.javascript.rhino.StaticSourceFile)

Aggregations

ObjectTypeI (com.google.javascript.rhino.ObjectTypeI)27 TypeI (com.google.javascript.rhino.TypeI)15 FunctionTypeI (com.google.javascript.rhino.FunctionTypeI)9 Node (com.google.javascript.rhino.Node)7 JSDocInfo (com.google.javascript.rhino.JSDocInfo)4 Visibility (com.google.javascript.rhino.JSDocInfo.Visibility)3 StaticSourceFile (com.google.javascript.rhino.StaticSourceFile)2 LinkedHashMap (java.util.LinkedHashMap)2 TreeSet (java.util.TreeSet)2 Nullable (javax.annotation.Nullable)2 ImmutableList (com.google.common.collect.ImmutableList)1 Token (com.google.javascript.rhino.Token)1 TypeIRegistry (com.google.javascript.rhino.TypeIRegistry)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1