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