Search in sources :

Example 11 with StaticSourceFile

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

the class JsDocInfoParserTest method parse.

private JSDocInfo parse(String comment, JsDocParsing parseDocumentation, boolean parseFileOverview, String... warnings) {
    TestErrorReporter errorReporter = new TestErrorReporter().expectAllWarnings(warnings);
    Config config = Config.builder().setExtraAnnotationNames(extraAnnotations).setJsDocParsingMode(parseDocumentation).setSuppressionNames(extraSuppressions).setClosurePrimitiveNames(extraPrimitives).setLanguageMode(LanguageMode.ECMASCRIPT3).setParseInlineSourceMaps(true).setStrictMode(Config.StrictMode.SLOPPY).build();
    StaticSourceFile file = new SimpleSourceFile("testcode", SourceKind.STRONG);
    Node templateNode = IR.script();
    templateNode.setStaticSourceFile(file);
    JsDocInfoParser jsdocParser = new JsDocInfoParser(stream(comment), comment, 0, templateNode, config, errorReporter);
    jsdocParser.parse();
    this.prevLicense = jsdocParser.getLicenseText();
    errorReporter.verifyHasEncounteredAllWarningsAndErrors();
    final JSDocInfo result;
    if (parseFileOverview) {
        result = jsdocParser.getFileOverviewJSDocInfo();
    } else {
        result = jsdocParser.retrieveAndResetParsedJSDocInfo();
    }
    if (result != null) {
        assertThat(result.getTypeNodes()).comparingElementsUsing(transforming(NodeUtil::getSourceName, "has source name that")).doesNotContain(null);
    }
    return result;
}
Also used : TestErrorReporter(com.google.javascript.rhino.testing.TestErrorReporter) Node(com.google.javascript.rhino.Node) NodeSubject.assertNode(com.google.javascript.rhino.testing.NodeSubject.assertNode) SimpleSourceFile(com.google.javascript.rhino.SimpleSourceFile) StaticSourceFile(com.google.javascript.rhino.StaticSourceFile) JSDocInfo(com.google.javascript.rhino.JSDocInfo) NodeUtil(com.google.javascript.jscomp.NodeUtil)

Example 12 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 13 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 14 with StaticSourceFile

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

the class ParserRunner method detectFeatures.

// TODO(sdh): this is less useful if we end up needing the node for library version detection
public static FeatureSet detectFeatures(String sourcePath, String sourceString) {
    SourceFile file = new SourceFile(sourcePath, sourceString);
    ErrorReporter reporter = IRFactory.NULL_REPORTER;
    com.google.javascript.jscomp.parsing.parser.Parser.Config config = newParserConfig(IRFactory.NULL_CONFIG);
    Parser p = new Parser(config, new Es6ErrorReporter(reporter, false), file);
    ProgramTree tree = p.parseProgram();
    StaticSourceFile simpleSourceFile = new SimpleSourceFile(sourcePath, false);
    return IRFactory.detectFeatures(tree, simpleSourceFile, sourceString).union(p.getFeatures());
}
Also used : ErrorReporter(com.google.javascript.rhino.ErrorReporter) ProgramTree(com.google.javascript.jscomp.parsing.parser.trees.ProgramTree) SimpleSourceFile(com.google.javascript.rhino.SimpleSourceFile) SimpleSourceFile(com.google.javascript.rhino.SimpleSourceFile) StaticSourceFile(com.google.javascript.rhino.StaticSourceFile) SourceFile(com.google.javascript.jscomp.parsing.parser.SourceFile) StaticSourceFile(com.google.javascript.rhino.StaticSourceFile) Parser(com.google.javascript.jscomp.parsing.parser.Parser)

Example 15 with StaticSourceFile

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

the class PerformanceTracker method estimateLines.

private int estimateLines(Node n) {
    checkState(n.isScript());
    StaticSourceFile ssf = n.getStaticSourceFile();
    if (ssf instanceof SourceFile) {
        return ((SourceFile) ssf).getNumLines();
    }
    return 0;
}
Also used : StaticSourceFile(com.google.javascript.rhino.StaticSourceFile) 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