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;
}
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);
}
}
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);
}
}
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());
}
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;
}
Aggregations