use of com.google.javascript.rhino.TypeI in project closure-compiler by google.
the class DisambiguateProperties method getTypeAlternatives.
/**
* Returns the alternatives if this is a type that represents multiple
* types, and null if not. Union and interface types can correspond to
* multiple other types.
*/
private Iterable<? extends TypeI> getTypeAlternatives(TypeI type) {
if (type.isUnionType()) {
return type.getUnionMembers();
} else {
ObjectTypeI objType = type.toMaybeObjectType();
FunctionTypeI constructor = objType != null ? objType.getConstructor() : null;
if (constructor != null && constructor.isInterface()) {
List<TypeI> list = new ArrayList<>();
for (FunctionTypeI impl : constructor.getDirectSubTypes()) {
list.add(impl.getInstanceType());
}
return list.isEmpty() ? null : list;
} else {
return null;
}
}
}
use of com.google.javascript.rhino.TypeI in project closure-compiler by google.
the class Es6TemplateLiterals method visitTemplateLiteral.
/**
* Converts `${a} b ${c} d ${e}` to (a + " b " + c + " d " + e)
*
* @param n A TEMPLATELIT node that is not prefixed with a tag
*/
static void visitTemplateLiteral(NodeTraversal t, Node n, boolean addTypes) {
TypeIRegistry registry = t.getCompiler().getTypeIRegistry();
TypeI stringType = createType(addTypes, registry, JSTypeNative.STRING_TYPE);
int length = n.getChildCount();
if (length == 0) {
n.replaceWith(withType(IR.string("\"\""), stringType));
} else {
Node first = n.removeFirstChild();
checkState(first.isString());
if (length == 1) {
n.replaceWith(first);
} else {
// Add the first string with the first substitution expression
Node add = withType(IR.add(first, n.removeFirstChild().removeFirstChild()), n.getTypeI());
// Process the rest of the template literal
for (int i = 2; i < length; i++) {
Node child = n.removeFirstChild();
if (child.isString()) {
if (child.getString().isEmpty()) {
continue;
} else if (i == 2 && first.getString().isEmpty()) {
// So that `${hello} world` gets translated into (hello + " world")
// instead of ("" + hello + " world").
add = add.getSecondChild().detach();
}
}
add = withType(IR.add(add, child.isString() ? child : child.removeFirstChild()), n.getTypeI());
}
n.replaceWith(add.useSourceInfoIfMissingFromForTree(n));
}
}
t.reportCodeChange();
}
use of com.google.javascript.rhino.TypeI in project closure-compiler by google.
the class AccessControlUtils method getEffectiveVisibilityForNonOverriddenProperty.
/**
* Returns the effective visibility of the given non-overridden property.
* Non-overridden properties without an explicit visibility annotation
* receive the default visibility declared in the file's {@code @fileoverview}
* block, if one exists.
*/
private static Visibility getEffectiveVisibilityForNonOverriddenProperty(Node getprop, ObjectTypeI objectType, @Nullable Visibility fileOverviewVisibility, @Nullable CodingConvention codingConvention) {
String propertyName = getprop.getLastChild().getString();
if (codingConvention != null && codingConvention.isPrivate(propertyName)) {
return Visibility.PRIVATE;
}
Visibility raw = Visibility.INHERITED;
if (objectType != null) {
raw = objectType.getOwnPropertyJSDocInfo(propertyName).getVisibility();
}
TypeI type = getprop.getTypeI();
boolean createdFromGoogProvide = (type != null && type.isLiteralObject());
// every a.b.* namespace effectively package-private.
return (raw != Visibility.INHERITED || fileOverviewVisibility == null || createdFromGoogProvide) ? raw : fileOverviewVisibility;
}
use of com.google.javascript.rhino.TypeI in project closure-compiler by google.
the class DisambiguateProperties method getTypeWithProperty.
/**
* Returns the type in the chain from the given type that contains the given
* field or null if it is not found anywhere.
* Can return a subtype of the input type.
*/
private ObjectTypeI getTypeWithProperty(String field, TypeI type) {
if (type == null) {
return null;
}
ObjectTypeI foundType = gtwpCacheGet(field, type);
if (foundType != null) {
return foundType.equals(BOTTOM_OBJECT) ? null : foundType;
}
if (type.isEnumElement()) {
foundType = getTypeWithProperty(field, type.getEnumeratedTypeOfEnumElement());
gtwpCachePut(field, type, foundType == null ? BOTTOM_OBJECT : foundType);
return foundType;
}
if (!type.isObjectType()) {
if (type.isBoxableScalar()) {
foundType = getTypeWithProperty(field, type.autobox());
gtwpCachePut(field, type, foundType == null ? BOTTOM_OBJECT : foundType);
return foundType;
} else {
gtwpCachePut(field, type, BOTTOM_OBJECT);
return null;
}
}
// Ignore the prototype itself at all times.
if ("prototype".equals(field)) {
gtwpCachePut(field, type, BOTTOM_OBJECT);
return null;
}
// We look up the prototype chain to find the highest place (if any) that
// this appears. This will make references to overridden properties look
// like references to the initial property, so they are renamed alike.
ObjectTypeI objType = type.toMaybeObjectType();
if (objType != null && objType.getConstructor() != null && objType.getConstructor().isInterface()) {
ObjectTypeI topInterface = objType.getTopDefiningInterface(field);
if (topInterface != null && topInterface.getConstructor() != null) {
foundType = topInterface.getPrototypeObject();
}
} else {
while (objType != null && !Objects.equals(objType.getPrototypeObject(), objType)) {
if (objType.hasOwnProperty(field)) {
foundType = objType;
}
objType = objType.getPrototypeObject();
}
}
// type is an object type, see if any subtype has the property.
if (foundType == null) {
TypeI subtypeWithProp = type.getGreatestSubtypeWithProperty(field);
ObjectTypeI maybeType = subtypeWithProp == null ? null : subtypeWithProp.toMaybeObjectType();
// so we have to double check.
if (maybeType != null && maybeType.hasOwnProperty(field)) {
foundType = maybeType;
}
}
// Unwrap templatized types, they are not unique at runtime.
if (foundType != null && foundType.isGenericObjectType()) {
foundType = foundType.getRawType();
}
// so that the returned type has the correct name.
if (foundType != null && foundType.isLegacyNamedType()) {
foundType = foundType.getLegacyResolvedType().toMaybeObjectType();
}
gtwpCachePut(field, type, foundType == null ? BOTTOM_OBJECT : foundType);
return foundType;
}
use of com.google.javascript.rhino.TypeI in project closure-compiler by google.
the class DisambiguateProperties method getTypesToSkipForTypeNonUnion.
private Set<TypeI> getTypesToSkipForTypeNonUnion(TypeI type) {
Set<TypeI> types = new HashSet<>();
TypeI skipType = type;
while (skipType != null) {
types.add(skipType);
ObjectTypeI objSkipType = skipType.toMaybeObjectType();
if (objSkipType != null) {
skipType = objSkipType.getPrototypeObject();
} else {
break;
}
}
return types;
}
Aggregations