use of com.google.javascript.rhino.ObjectTypeI in project closure-compiler by google.
the class TypeTransformation method joinRecordTypes.
/**
* Merges a list of record types.
* Example
* {r:{s:string, n:number}} and {a:boolean}
* is transformed into {r:{s:string, n:number}, a:boolean}
*/
private TypeI joinRecordTypes(ImmutableList<ObjectTypeI> recTypes) {
Map<String, TypeI> props = new LinkedHashMap<>();
for (ObjectTypeI recType : recTypes) {
for (String newPropName : recType.getOwnPropertyNames()) {
TypeI newPropValue = recType.getPropertyType(newPropName);
// Put the new property depending if it already exists in the map
putNewPropInPropertyMap(props, newPropName, newPropValue);
}
}
return createRecordType(ImmutableMap.copyOf(props));
}
use of com.google.javascript.rhino.ObjectTypeI in project closure-compiler by google.
the class TypeMismatch method removeNullUndefinedAndTemplates.
private static TypeI removeNullUndefinedAndTemplates(TypeI t) {
TypeI result = t.restrictByNotNullOrUndefined();
ObjectTypeI obj = result.toMaybeObjectType();
if (obj != null && obj.isGenericObjectType()) {
return obj.instantiateGenericsWithUnknown();
}
return result;
}
use of com.google.javascript.rhino.ObjectTypeI in project closure-compiler by google.
the class CheckAccessControls method getSuperClassInstanceIfFinal.
/**
* If the superclass is final, this method returns an instance of the superclass.
*/
@Nullable
private static ObjectTypeI getSuperClassInstanceIfFinal(@Nullable TypeI type) {
if (type != null) {
ObjectTypeI obj = castToObject(type);
FunctionTypeI ctor = obj == null ? null : obj.getSuperClassConstructor();
JSDocInfo doc = ctor == null ? null : ctor.getJSDocInfo();
if (doc != null && doc.isFinal()) {
return ctor.getInstanceType();
}
}
return null;
}
use of com.google.javascript.rhino.ObjectTypeI in project closure-compiler by google.
the class CheckAccessControls method getPropertyDeprecationInfo.
/**
* Returns the deprecation reason for the property if it is marked
* as being deprecated. Returns empty string if the property is deprecated
* but no reason was given. Returns null if the property is not deprecated.
*/
@Nullable
private static String getPropertyDeprecationInfo(ObjectTypeI type, String prop) {
String depReason = getDeprecationReason(type.getOwnPropertyJSDocInfo(prop));
if (depReason != null) {
return depReason;
}
ObjectTypeI implicitProto = type.getPrototypeObject();
if (implicitProto != null) {
return getPropertyDeprecationInfo(implicitProto, prop);
}
return null;
}
use of com.google.javascript.rhino.ObjectTypeI in project closure-compiler by google.
the class Es6ForOfConverter method visitForOf.
private void visitForOf(NodeTraversal t, Node node, Node parent) {
Node variable = node.removeFirstChild();
Node iterable = node.removeFirstChild();
Node body = node.removeFirstChild();
TypeI typeParam = unknownType;
if (addTypes) {
// TODO(sdh): This is going to be null if the iterable is nullable or unknown. We might want
// to consider some way of unifying rather than simply looking at the nominal type.
ObjectTypeI iterableType = iterable.getTypeI().autobox().toMaybeObjectType();
if (iterableType != null) {
TypeIRegistry registry = compiler.getTypeIRegistry();
TypeI iterableBaseType = registry.getNativeType(JSTypeNative.ITERABLE_TYPE);
typeParam = iterableType.getInstantiatedTypeArgument(iterableBaseType);
}
}
TypeI iteratorType = createGenericType(JSTypeNative.ITERATOR_TYPE, typeParam);
TypeI iIterableResultType = createGenericType(JSTypeNative.I_ITERABLE_RESULT_TYPE, typeParam);
TypeI iteratorNextType = addTypes ? iteratorType.toMaybeObjectType().getPropertyType("next") : null;
JSDocInfo varJSDocInfo = variable.getJSDocInfo();
Node iterName = withType(IR.name(ITER_BASE + compiler.getUniqueNameIdSupplier().get()), iteratorType);
iterName.makeNonIndexable();
Node getNext = withType(IR.call(withType(IR.getprop(iterName.cloneTree(), withStringType(IR.string("next"))), iteratorNextType)), iIterableResultType);
String variableName;
Token declType;
if (variable.isName()) {
declType = Token.NAME;
variableName = variable.getQualifiedName();
} else {
Preconditions.checkState(NodeUtil.isNameDeclaration(variable), "Expected var, let, or const. Got %s", variable);
declType = variable.getToken();
variableName = variable.getFirstChild().getQualifiedName();
}
Node iterResult = withType(IR.name(ITER_RESULT + variableName), iIterableResultType);
iterResult.makeNonIndexable();
Node call = Es6ToEs3Util.makeIterator(compiler, iterable);
if (addTypes) {
TypeI jscompType = t.getScope().getVar("$jscomp").getNode().getTypeI();
TypeI makeIteratorType = jscompType.toMaybeObjectType().getPropertyType("makeIterator");
call.getFirstChild().setTypeI(makeIteratorType);
call.getFirstFirstChild().setTypeI(jscompType);
}
Node init = IR.var(iterName.cloneTree(), withType(call, iteratorType));
Node initIterResult = iterResult.cloneTree();
initIterResult.addChildToFront(getNext.cloneTree());
init.addChildToBack(initIterResult);
Node cond = withBooleanType(IR.not(withBooleanType(IR.getprop(iterResult.cloneTree(), withStringType(IR.string("done"))))));
Node incr = withType(IR.assign(iterResult.cloneTree(), getNext.cloneTree()), iIterableResultType);
Node declarationOrAssign;
if (declType == Token.NAME) {
declarationOrAssign = withType(IR.assign(withType(IR.name(variableName).useSourceInfoFrom(variable), typeParam), withType(IR.getprop(iterResult.cloneTree(), withStringType(IR.string("value"))), typeParam)), typeParam);
declarationOrAssign.setJSDocInfo(varJSDocInfo);
declarationOrAssign = IR.exprResult(declarationOrAssign);
} else {
declarationOrAssign = new Node(declType, withType(IR.name(variableName).useSourceInfoFrom(variable.getFirstChild()), typeParam));
declarationOrAssign.getFirstChild().addChildToBack(withType(IR.getprop(iterResult.cloneTree(), withStringType(IR.string("value"))), typeParam));
declarationOrAssign.setJSDocInfo(varJSDocInfo);
}
Node newBody = IR.block(declarationOrAssign, body).useSourceInfoFrom(body);
Node newFor = IR.forNode(init, cond, incr, newBody);
newFor.useSourceInfoIfMissingFromForTree(node);
parent.replaceChild(node, newFor);
compiler.reportChangeToEnclosingScope(newFor);
}
Aggregations