Search in sources :

Example 16 with StaticSourceFile

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

the class NodeUtil method getSourceFile.

/**
 * @param n The node.
 * @return The source name property on the node or its ancestors.
 */
public static StaticSourceFile getSourceFile(Node n) {
    StaticSourceFile sourceName = null;
    while (sourceName == null && n != null) {
        sourceName = n.getStaticSourceFile();
        n = n.getParent();
    }
    return sourceName;
}
Also used : StaticSourceFile(com.google.javascript.rhino.StaticSourceFile)

Example 17 with StaticSourceFile

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

the class CheckAccessControls method isPrivateAccessAllowed.

private static boolean isPrivateAccessAllowed(Var var, Node name) {
    StaticSourceFile varSrc = var.getSourceFile();
    StaticSourceFile refSrc = name.getStaticSourceFile();
    return varSrc == null || refSrc == null || Objects.equals(varSrc.getName(), refSrc.getName());
}
Also used : StaticSourceFile(com.google.javascript.rhino.StaticSourceFile)

Example 18 with StaticSourceFile

use of com.google.javascript.rhino.StaticSourceFile 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.
 *
 * <p>This method covers both:
 *
 * <ul>
 *   <li>accesses to properties during execution
 *   <li>overrides of properties during declaration
 * </ul>
 *
 * TODO(nickreid): Things would probably be a lot simpler, though a bit duplicated, if these two
 * concepts were separated. Much of the underlying logic could stop checking various inconsistent
 * definitions of "is this an override".
 */
private void checkPropertyVisibility(PropertyReference propRef) {
    if (NodeUtil.isEs6ConstructorMemberFunctionDef(propRef.getSourceNode())) {
        // because it ignores cases where there's no overridden definition.
        return;
    }
    JSType rawReferenceType = typeOrUnknown(propRef.getReceiverType()).autobox();
    ObjectType referenceType = castToObject(rawReferenceType);
    String propertyName = propRef.getName();
    StaticSourceFile definingSource = AccessControlUtils.getDefiningSource(propRef.getSourceNode(), referenceType, propertyName);
    // Is this a normal property access, or are we trying to override
    // an existing property?
    boolean isOverride = propRef.isDocumentedDeclaration() || propRef.isOverride();
    ObjectType objectType = AccessControlUtils.getObjectType(referenceType, isOverride, propertyName);
    Visibility fileOverviewVisibility = defaultVisibilityForFiles.get(definingSource);
    Visibility visibility = getEffectivePropertyVisibility(propRef, referenceType, defaultVisibilityForFiles);
    if (isOverride) {
        Visibility overriding = getOverridingPropertyVisibility(propRef);
        if (overriding != null) {
            checkPropertyOverrideVisibilityIsSame(overriding, visibility, fileOverviewVisibility, propRef);
        }
    }
    JSType reportType = rawReferenceType;
    if (objectType != null) {
        Node node = objectType.getOwnPropertyDefSite(propertyName);
        if (node == null) {
            // Assume the property is public.
            return;
        }
        reportType = objectType;
        definingSource = node.getStaticSourceFile();
    } else if (fileOverviewVisibility == null) {
        // Otherwise just assume the property is public.
        return;
    }
    StaticSourceFile referenceSource = propRef.getSourceNode().getStaticSourceFile();
    if (isOverride) {
        boolean sameInput = referenceSource != null && referenceSource.getName().equals(definingSource.getName());
        checkPropertyOverrideVisibility(propRef, visibility, fileOverviewVisibility, reportType, sameInput);
    } else {
        checkPropertyAccessVisibility(propRef, visibility, reportType, referenceSource, definingSource);
    }
}
Also used : ObjectType(com.google.javascript.rhino.jstype.ObjectType) JSType(com.google.javascript.rhino.jstype.JSType) Node(com.google.javascript.rhino.Node) Visibility(com.google.javascript.rhino.JSDocInfo.Visibility) StaticSourceFile(com.google.javascript.rhino.StaticSourceFile)

Aggregations

StaticSourceFile (com.google.javascript.rhino.StaticSourceFile)18 Node (com.google.javascript.rhino.Node)7 Visibility (com.google.javascript.rhino.JSDocInfo.Visibility)6 ObjectType (com.google.javascript.rhino.jstype.ObjectType)4 SimpleSourceFile (com.google.javascript.rhino.SimpleSourceFile)3 Parser (com.google.javascript.jscomp.parsing.parser.Parser)2 SourceFile (com.google.javascript.jscomp.parsing.parser.SourceFile)2 ProgramTree (com.google.javascript.jscomp.parsing.parser.trees.ProgramTree)2 JSDocInfo (com.google.javascript.rhino.JSDocInfo)2 ObjectTypeI (com.google.javascript.rhino.ObjectTypeI)2 TestErrorReporter (com.google.javascript.rhino.testing.TestErrorReporter)2 FilePosition (com.google.debugging.sourcemap.FilePosition)1 OriginalMapping (com.google.debugging.sourcemap.proto.Mapping.OriginalMapping)1 NodeUtil (com.google.javascript.jscomp.NodeUtil)1 SourceFile (com.google.javascript.jscomp.SourceFile)1 ParseResult (com.google.javascript.jscomp.parsing.ParserRunner.ParseResult)1 FeatureSet (com.google.javascript.jscomp.parsing.parser.FeatureSet)1 Comment (com.google.javascript.jscomp.parsing.parser.trees.Comment)1 ErrorReporter (com.google.javascript.rhino.ErrorReporter)1 FunctionType (com.google.javascript.rhino.jstype.FunctionType)1