Search in sources :

Example 6 with StaticSourceFile

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

the class CheckAccessControls method isPackageAccessAllowed.

private boolean isPackageAccessAllowed(Var var, Node name) {
    StaticSourceFile varSrc = var.getSourceFile();
    StaticSourceFile refSrc = name.getStaticSourceFile();
    if (varSrc == null && refSrc == null) {
        // file is unknown. I didn't change it in order not to break existing code.
        return false;
    }
    CodingConvention codingConvention = compiler.getCodingConvention();
    String srcPackage = codingConvention.getPackageName(varSrc);
    String refPackage = codingConvention.getPackageName(refSrc);
    return srcPackage != null && refPackage != null && Objects.equals(srcPackage, refPackage);
}
Also used : StaticSourceFile(com.google.javascript.rhino.StaticSourceFile)

Example 7 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, ObjectType referenceType, ImmutableMap<StaticSourceFile, Visibility> fileVisibilityMap) {
    String propertyName = property.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;
    ObjectType objectType = getObjectType(referenceType, isOverride, propertyName);
    if (isOverride) {
        Visibility overridden = getOverriddenPropertyVisibility(objectType, propertyName);
        return getEffectiveVisibilityForOverriddenProperty(overridden, fileOverviewVisibility, propertyName);
    } else {
        return getEffectiveVisibilityForNonOverriddenProperty(property, objectType, fileOverviewVisibility);
    }
}
Also used : ObjectType(com.google.javascript.rhino.jstype.ObjectType) Node(com.google.javascript.rhino.Node) Visibility(com.google.javascript.rhino.JSDocInfo.Visibility) StaticSourceFile(com.google.javascript.rhino.StaticSourceFile)

Example 8 with StaticSourceFile

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

the class ParserRunner method parse.

public static ParseResult parse(StaticSourceFile sourceFile, String sourceString, Config config, ErrorReporter errorReporter) {
    // TODO(johnlenz): unify "SourceFile", "Es6ErrorReporter" and "Config"
    String sourceName = sourceFile.getName();
    try {
        SourceFile file = new SourceFile(sourceName, sourceString);
        boolean keepGoing = config.runMode() == Config.RunMode.KEEP_GOING;
        Es6ErrorReporter es6ErrorReporter = new Es6ErrorReporter(errorReporter, keepGoing);
        com.google.javascript.jscomp.parsing.parser.Parser.Config es6config = newParserConfig(config);
        Parser p = new Parser(es6config, es6ErrorReporter, file);
        ProgramTree tree = p.parseProgram();
        Node root = null;
        List<Comment> comments = ImmutableList.of();
        FeatureSet features = p.getFeatures();
        if (tree != null && (!es6ErrorReporter.hadError() || keepGoing)) {
            IRFactory factory = IRFactory.transformTree(tree, sourceFile, config, errorReporter);
            root = factory.getResultNode();
            features = features.union(factory.getFeatures());
            root.putProp(Node.FEATURE_SET, features);
            if (config.jsDocParsingMode().shouldParseDescriptions()) {
                comments = p.getComments();
            }
        }
        return new ParseResult(root, comments, features, p.getSourceMapURL());
    } catch (Throwable t) {
        throw new RuntimeException("Exception parsing \"" + sourceName + "\"", t);
    }
}
Also used : Comment(com.google.javascript.jscomp.parsing.parser.trees.Comment) ProgramTree(com.google.javascript.jscomp.parsing.parser.trees.ProgramTree) Node(com.google.javascript.rhino.Node) Parser(com.google.javascript.jscomp.parsing.parser.Parser) FeatureSet(com.google.javascript.jscomp.parsing.parser.FeatureSet) StaticSourceFile(com.google.javascript.rhino.StaticSourceFile) SourceFile(com.google.javascript.jscomp.parsing.parser.SourceFile)

Example 9 with StaticSourceFile

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

the class CheckMissingSemicolon method checkSemicolon.

private void checkSemicolon(NodeTraversal t, Node n) {
    StaticSourceFile staticSourceFile = n.getStaticSourceFile();
    if (staticSourceFile instanceof SourceFile) {
        SourceFile sourceFile = (SourceFile) staticSourceFile;
        String code;
        try {
            code = sourceFile.getCode();
        } catch (IOException e) {
            // We can't read the original source file. Just skip this check.
            return;
        }
        int length = n.getLength();
        if (length == 0) {
            // that information, so just skip the check.
            return;
        }
        int position = n.getSourceOffset() + length - 1;
        boolean endsWithSemicolon = code.charAt(position) == ';';
        if (!endsWithSemicolon) {
            t.report(n, MISSING_SEMICOLON);
        }
    }
}
Also used : StaticSourceFile(com.google.javascript.rhino.StaticSourceFile) SourceFile(com.google.javascript.jscomp.SourceFile) IOException(java.io.IOException) StaticSourceFile(com.google.javascript.rhino.StaticSourceFile)

Example 10 with StaticSourceFile

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

the class DefaultCodingConventionTest 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)

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