use of com.google.javascript.rhino.TypeI in project closure-compiler by google.
the class CheckAccessControls method enterScope.
@Override
public void enterScope(NodeTraversal t) {
Node n = t.getScopeRoot();
if (n.isFunction()) {
Node parent = n.getParent();
if (isDeprecatedFunction(n)) {
deprecatedDepth++;
}
TypeI prevClass = getCurrentClass();
TypeI currentClass = prevClass == null ? getClassOfMethod(n, parent) : prevClass;
// ArrayDeques can't handle nulls, so we reuse the bottom type
// as a null sentinel.
currentClassStack.addFirst(currentClass == null ? noTypeSentinel : currentClass);
}
}
use of com.google.javascript.rhino.TypeI in project closure-compiler by google.
the class CheckAccessControls method checkProtectedPropertyVisibility.
private void checkProtectedPropertyVisibility(NodeTraversal t, Node getprop, TypeI ownerType) {
// There are 3 types of legal accesses of a protected property:
// 1) Accesses in the same file
// 2) Overriding the property in a subclass
// 3) Accessing the property from inside a subclass
// The first two have already been checked for.
TypeI currentClass = getCurrentClass();
if (currentClass == null || !currentClass.isSubtypeOf(ownerType)) {
String propertyName = getprop.getLastChild().getString();
compiler.report(t.makeError(getprop, BAD_PROTECTED_PROPERTY_ACCESS, propertyName, typeRegistry.getReadableTypeName(getprop.getFirstChild())));
}
}
use of com.google.javascript.rhino.TypeI 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);
}
use of com.google.javascript.rhino.TypeI in project closure-compiler by google.
the class GlobalTypeInfo method createUnionType.
@Override
public TypeI createUnionType(List<? extends TypeI> members) {
checkArgument(!members.isEmpty(), "Cannot create union type with no members");
JSType result = commonTypes.BOTTOM;
for (TypeI t : members) {
result = JSType.join(result, (JSType) t);
}
return result;
}
use of com.google.javascript.rhino.TypeI in project closure-compiler by google.
the class NewTypeInference method instantiateCalleeMaybeWithTTL.
/**
* Instantiate the generic function using the appropriate type map.
*
* If the generic function uses TTL, we need to map the TTL variables to types.
* In this method, we find the TTL expressions, call TypeTransformation#eval to evaluate them,
* and update the type map.
*/
private FunctionType instantiateCalleeMaybeWithTTL(FunctionType calleeType, ImmutableMap<String, JSType> typeMap) {
Map<String, Node> typeTransformations = calleeType.getTypeTransformations();
if (typeTransformations.isEmpty()) {
return calleeType.instantiateGenerics(typeMap);
}
ImmutableMap<String, TypeI> mapWithOriginalNames = getTypemapWithOriginalNames(typeMap);
LinkedHashMap<String, JSType> newTypeMap = new LinkedHashMap<>();
newTypeMap.putAll(typeMap);
for (Map.Entry<String, Node> entry : typeTransformations.entrySet()) {
String ttlVar = entry.getKey();
Node transform = entry.getValue();
@SuppressWarnings({ "unchecked", "rawtypes" }) JSType t = (JSType) this.ttlObj.eval(transform, mapWithOriginalNames);
newTypeMap.put(ttlVar, t);
}
return calleeType.instantiateGenerics(ImmutableMap.copyOf(newTypeMap));
}
Aggregations