Search in sources :

Example 1 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.
 * @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 2 with StaticSourceFile

use of com.google.javascript.rhino.StaticSourceFile 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)

Example 3 with StaticSourceFile

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

the class SourceMap method addMapping.

public void addMapping(Node node, FilePosition outputStartPosition, FilePosition outputEndPosition) {
    // If the node does not have an associated source file or
    // its line number is -1, then the node does not have sufficient
    // information for a mapping to be useful.
    StaticSourceFile sourceFile = node.getStaticSourceFile();
    if (sourceFile == null || node.getLineno() < 0) {
        return;
    }
    String sourceFileName = sourceFile.getName();
    int lineNo = node.getLineno();
    int charNo = node.getCharno();
    String originalName = SourceMap.getOriginalName(node);
    if (mapping != null) {
        OriginalMapping sourceMapping = mapping.getSourceMapping(sourceFileName, lineNo, charNo);
        if (sourceMapping != null) {
            sourceFileName = sourceMapping.getOriginalFile();
            lineNo = sourceMapping.getLineNumber();
            charNo = sourceMapping.getColumnPosition();
            String identifier = sourceMapping.getIdentifier();
            if (sourceMapping.hasIdentifier() && !identifier.isEmpty()) {
                originalName = identifier;
            }
        }
    }
    sourceFileName = fixupSourceLocation(sourceFileName);
    // Rhino source lines are one based but for v3 source maps, we make
    // them zero based.
    int lineBaseOffset = 1;
    generator.addMapping(sourceFileName, originalName, new FilePosition(lineNo - lineBaseOffset, charNo), outputStartPosition, outputEndPosition);
}
Also used : OriginalMapping(com.google.debugging.sourcemap.proto.Mapping.OriginalMapping) StaticSourceFile(com.google.javascript.rhino.StaticSourceFile) FilePosition(com.google.debugging.sourcemap.FilePosition)

Example 4 with StaticSourceFile

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

the class GoogleCodingConventionTest method assertPackageName.

private void assertPackageName(String filename, String expectedPackageName) {
    StaticSourceFile sourceFile = SourceFile.fromCode(filename, "");
    assertThat(conv.getPackageName(sourceFile)).isEqualTo(expectedPackageName);
}
Also used : StaticSourceFile(com.google.javascript.rhino.StaticSourceFile)

Example 5 with StaticSourceFile

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

the class ParserTest method doParse.

private ParserRunner.ParseResult doParse(String string, String... warnings) {
    TestErrorReporter testErrorReporter = new TestErrorReporter().expectAllWarnings(warnings);
    StaticSourceFile file = new SimpleSourceFile("input", SourceKind.STRONG);
    ParserRunner.ParseResult result = ParserRunner.parse(file, string, createConfig(), testErrorReporter);
    // check expected features if specified
    assertFS(result.features).contains(expectedFeatures);
    // verifying that all warnings were seen
    testErrorReporter.verifyHasEncounteredAllWarningsAndErrors();
    assertSourceInfoPresent(result.ast);
    return result;
}
Also used : TestErrorReporter(com.google.javascript.rhino.testing.TestErrorReporter) SimpleSourceFile(com.google.javascript.rhino.SimpleSourceFile) StaticSourceFile(com.google.javascript.rhino.StaticSourceFile) ParseResult(com.google.javascript.jscomp.parsing.ParserRunner.ParseResult)

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