use of com.google.javascript.rhino.FunctionTypeI in project closure-compiler by google.
the class TypedCodeGenerator method findMethodOwner.
// TODO(sdh): This whole method could be deleted if we don't mind adding
// additional @this annotations where they're not actually necessary.
/**
* Given a method definition node, returns the {@link ObjectTypeI} corresponding
* to the class the method is defined on, or null if it is not a prototype method.
*/
private ObjectTypeI findMethodOwner(Node n) {
if (n == null) {
return null;
}
Node parent = n.getParent();
FunctionTypeI ctor = null;
if (parent.isAssign()) {
Node target = parent.getFirstChild();
if (NodeUtil.isPrototypeProperty(target)) {
// TODO(johnlenz): handle non-global types
TypeI type = registry.getGlobalType(target.getFirstFirstChild().getQualifiedName());
ctor = type != null ? ((ObjectTypeI) type).getConstructor() : null;
}
} else if (parent.isClass()) {
// TODO(sdh): test this case once the type checker understands ES6 classes
ctor = parent.getTypeI().toMaybeFunctionType();
}
return ctor != null ? ctor.getInstanceType() : null;
}
use of com.google.javascript.rhino.FunctionTypeI in project closure-compiler by google.
the class DevirtualizePrototypeMethodsTest method checkTypeOfRewrittenMethods.
private void checkTypeOfRewrittenMethods() {
TypeI thisType = getTypeAtPosition(0).toMaybeFunctionType().getInstanceType();
FunctionTypeI fooType = getTypeAtPosition(1, 0, 0).toMaybeFunctionType();
FunctionTypeI barType = getTypeAtPosition(2, 0, 0).toMaybeFunctionType();
FunctionTypeI bazType = getTypeAtPosition(3, 0, 0).toMaybeFunctionType();
TypeI fooResultType = getTypeAtPosition(5, 0);
TypeI barResultType = getTypeAtPosition(6, 0);
TypeI bazResultType = getTypeAtPosition(7, 0);
TypeI number = fooResultType;
TypeI receiver = fooType.getTypeOfThis();
assertTrue("Expected number: " + number, number.isNumberValueType());
// NOTE: OTI has the receiver as unknown, NTI has it as null.
assertTrue("Expected null or unknown: " + receiver, receiver == null || receiver.isUnknownType());
assertThat(barResultType).isEqualTo(number);
// Check that foo's type is {function(A): number}
assertThat(fooType.getParameterTypes()).containsExactly(thisType);
assertThat(fooType.getReturnType()).isEqualTo(number);
assertThat(fooType.getTypeOfThis()).isEqualTo(receiver);
// Check that bar's type is {function(A, number): number}
assertThat(barType.getParameterTypes()).containsExactly(thisType, number).inOrder();
assertThat(barType.getReturnType()).isEqualTo(number);
assertThat(barType.getTypeOfThis()).isEqualTo(receiver);
// Check that baz's type is {function(A): undefined} in OTI and {function(A): ?} in NTI
assertThat(bazType.getParameterTypes()).containsExactly(thisType);
assertThat(bazType.getTypeOfThis()).isEqualTo(receiver);
// TODO(sdh): NTI currently fails to infer the result of the baz() call (b/37351897)
// so we handle it more carefully. When methods are deferred, this should be changed
// to check that it's exactly unknown.
assertTrue("Expected undefined or unknown: " + bazResultType, bazResultType.isVoidType() || bazResultType.isUnknownType());
assertTrue("Expected undefined: " + bazType.getReturnType(), bazType.getReturnType().isVoidType());
}
use of com.google.javascript.rhino.FunctionTypeI 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.FunctionTypeI in project closure-compiler by google.
the class ClosureCodingConvention method applySubclassRelationship.
/**
* Closure's goog.inherits adds a {@code superClass_} property to the
* subclass, and a {@code constructor} property.
*/
@Override
public void applySubclassRelationship(final NominalTypeBuilder parent, final NominalTypeBuilder child, SubclassType type) {
super.applySubclassRelationship(parent, child, type);
if (type == SubclassType.INHERITS) {
final FunctionTypeI childCtor = child.constructor();
child.declareConstructorProperty("superClass_", parent.prototypeOrInstance(), childCtor.getSource());
// Notice that constructor functions do not need to be covariant on the superclass.
// So if G extends F, new G() and new F() can accept completely different argument
// types, but G.prototype.constructor needs to be covariant on F.prototype.constructor.
// To get around this, we just turn off type-checking on arguments and return types
// of G.prototype.constructor.
FunctionTypeI qmarkCtor = childCtor.toBuilder().withUnknownReturnType().withNoParameters().build();
child.declarePrototypeProperty("constructor", qmarkCtor, childCtor.getSource());
}
}
use of com.google.javascript.rhino.FunctionTypeI in project closure-compiler by google.
the class FunctionTypeBuilderTest method testExternSubTypes.
public void testExternSubTypes() throws Exception {
testSame(externs(ALL_NATIVE_EXTERN_TYPES), srcs(""));
List<FunctionTypeI> subtypes = ImmutableList.copyOf(((ObjectType) getLastCompiler().getTypeRegistry().getGlobalType("Error")).getConstructor().getDirectSubTypes());
for (FunctionTypeI type : subtypes) {
String typeName = type.getInstanceType().toString();
FunctionType typeInRegistry = ((ObjectType) getLastCompiler().getTypeRegistry().getGlobalType(typeName)).getConstructor();
assertSame(type, typeInRegistry);
}
}
Aggregations