use of com.google.javascript.rhino.TypeI in project closure-compiler by google.
the class ReplaceStrings method findMatchingClass.
/**
* @return The Config object for the class match the specified type or null
* if no match was found.
*/
private Config findMatchingClass(TypeI callClassType, Collection<String> declarationNames) {
if (!callClassType.isBottom() && !callClassType.isSomeUnknownType()) {
for (String declarationName : declarationNames) {
String className = getClassFromDeclarationName(declarationName);
TypeI methodClassType = registry.getGlobalType(className);
if (methodClassType != null && callClassType.isSubtypeOf(methodClassType)) {
return functions.get(declarationName);
}
}
}
return null;
}
use of com.google.javascript.rhino.TypeI in project closure-compiler by google.
the class FunctionType method acceptsArguments.
@Override
public boolean acceptsArguments(List<? extends TypeI> argumentTypes) {
// NOTE(aravindpg): This code is essentially lifted from TypeCheck::visitParameterList,
// but what small differences there are make it very painful to refactor out the shared code.
Iterator<? extends TypeI> arguments = argumentTypes.iterator();
Iterator<Node> parameters = this.getParameters().iterator();
Node parameter = null;
TypeI argument = null;
while (arguments.hasNext() && (parameters.hasNext() || parameter != null && parameter.isVarArgs())) {
// above implies that this must be a var_args function.
if (parameters.hasNext()) {
parameter = parameters.next();
}
argument = arguments.next();
if (!argument.isSubtypeOf(parameter.getTypeI())) {
return false;
}
}
int numArgs = argumentTypes.size();
return this.getMinArity() <= numArgs && numArgs <= this.getMaxArity();
}
use of com.google.javascript.rhino.TypeI in project closure-compiler by google.
the class CheckAccessControls method checkFinalClassOverrides.
/**
* Checks if a constructor is trying to override a final class.
*/
private void checkFinalClassOverrides(NodeTraversal t, Node fn, Node parent) {
checkState(fn.isFunction(), fn);
TypeI type = fn.getTypeI().toMaybeFunctionType();
if (type != null && type.isConstructor()) {
TypeI finalParentClass = getSuperClassInstanceIfFinal(getClassOfMethod(fn, parent));
if (finalParentClass != null) {
compiler.report(t.makeError(fn, EXTEND_FINAL_CLASS, type.getDisplayName(), finalParentClass.getDisplayName()));
}
}
}
use of com.google.javascript.rhino.TypeI in project closure-compiler by google.
the class CollapseProperties method flattenNameRef.
/**
* Replaces a GETPROP a.b.c with a NAME a$b$c.
*
* @param alias A flattened prefix name (e.g. "a$b")
* @param n The GETPROP node corresponding to the original name (e.g. "a.b")
* @param parent {@code n}'s parent
* @param originalName String version of the property name.
*/
private void flattenNameRef(String alias, Node n, Node parent, String originalName) {
Preconditions.checkArgument(n.isGetProp(), "Expected GETPROP, found %s. Node: %s", n.getToken(), n);
// BEFORE:
// getprop
// getprop
// name a
// string b
// string c
// AFTER:
// name a$b$c
Node ref = NodeUtil.newName(compiler, alias, n, originalName);
NodeUtil.copyNameAnnotations(n.getLastChild(), ref);
if (parent.isCall() && n == parent.getFirstChild()) {
// The node was a call target, we are deliberately flatten these as
// we node the "this" isn't provided by the namespace. Mark it as such:
parent.putBooleanProp(Node.FREE_CALL, true);
}
TypeI type = n.getTypeI();
if (type != null) {
ref.setTypeI(type);
}
parent.replaceChild(n, ref);
compiler.reportChangeToEnclosingScope(ref);
}
use of com.google.javascript.rhino.TypeI in project closure-compiler by google.
the class EsNextToEs8Converter method visitObjectWithSpread.
/*
* Convert '{first: b, c, ...spread, d: e, last}' to:
*
* Object.assign({}, {first:b, c}, spread, {d:e, last});
*/
private void visitObjectWithSpread(Node obj) {
checkArgument(obj.isObjectLit());
TypeI simpleObjectType = createType(addTypes, compiler.getTypeIRegistry(), JSTypeNative.EMPTY_OBJECT_LITERAL_TYPE);
TypeI resultType = simpleObjectType;
Node result = withType(IR.call(NodeUtil.newQName(compiler, "Object.assign")), resultType);
// Add an empty target object literal so changes made by Object.assign will not affect any other
// variables.
result.addChildToBack(withType(IR.objectlit(), simpleObjectType));
// An indicator whether the current last thing in the param list is an object literal to which
// properties may be added. Initialized to null since nothing should be added to the empty
// object literal in first position of the param list.
Node trailingObjectLiteral = null;
for (Node child : obj.children()) {
if (child.isSpread()) {
// Add the object directly to the param list.
Node spreaded = child.removeFirstChild();
result.addChildToBack(spreaded);
// Properties should not be added to the trailing object.
trailingObjectLiteral = null;
} else {
if (trailingObjectLiteral == null) {
// Add a new object to which properties may be added.
trailingObjectLiteral = withType(IR.objectlit(), simpleObjectType);
result.addChildToBack(trailingObjectLiteral);
}
// Add the property to the object literal.
trailingObjectLiteral.addChildToBack(child.detach());
}
}
result.useSourceInfoIfMissingFromForTree(obj);
obj.replaceWith(result);
compiler.reportChangeToEnclosingScope(result);
}
Aggregations