use of com.google.javascript.rhino.jstype.JSType in project closure-compiler by google.
the class JSTypeReconserializer method reconcileObjectTypes.
private TypeProto reconcileObjectTypes(SeenTypeRecord seen) {
TreeSet<String> debugTypenames = new TreeSet<>();
LinkedHashSet<Integer> instancePointers = new LinkedHashSet<>();
LinkedHashSet<Integer> prototypePointers = new LinkedHashSet<>();
LinkedHashSet<Integer> ownProperties = new LinkedHashSet<>();
boolean isClosureAssert = false;
boolean isConstructor = false;
boolean isInvalidating = false;
boolean propertiesKeepOriginalName = false;
for (JSType type : seen.jstypes) {
ObjectType objType = checkNotNull(type.toMaybeObjectType(), type);
if (this.serializationMode.includeDebugInfo()) {
debugTypenames.add(debugNameOf(type));
}
if (objType.isFunctionType()) {
FunctionType fnType = objType.toMaybeFunctionType();
// appear on the AST, optimizations need to know that at runtime these types may be present.
if (fnType.hasInstanceType() && fnType.getInstanceType() != null) {
instancePointers.add(this.serializeType(fnType.getInstanceType()));
prototypePointers.add(this.serializeType(fnType.getPrototype()));
isConstructor |= fnType.isConstructor();
}
isClosureAssert |= isClosureAssert(fnType.getClosurePrimitive());
}
for (String ownProperty : objType.getOwnPropertyNames()) {
// save space.
if (shouldPropagatePropertyName.test(ownProperty)) {
ownProperties.add(this.stringPoolBuilder.put(ownProperty));
}
}
isInvalidating |= this.invalidatingTypes.isInvalidating(objType);
/**
* To support legacy code, property disambiguation never renames properties of enums (e.g. 'A'
* in '/** @enum * / const E = {A: 0}`). In theory this would be safe to remove if we clean up
* code depending on the lack of renaming
*/
propertiesKeepOriginalName |= objType.isEnumType();
}
ObjectTypeProto.Builder objectProto = ObjectTypeProto.newBuilder().addAllInstanceType(instancePointers).addAllOwnProperty(ownProperties).addAllPrototype(prototypePointers).setClosureAssert(isClosureAssert).setIsInvalidating(isInvalidating).setMarkedConstructor(isConstructor).setPropertiesKeepOriginalName(propertiesKeepOriginalName).setUuid(seen.colorId.asByteString());
if (!debugTypenames.isEmpty()) {
objectProto.getDebugInfoBuilder().addAllTypename(debugTypenames);
}
return TypeProto.newBuilder().setObject(objectProto).build();
}
use of com.google.javascript.rhino.jstype.JSType in project closure-compiler by google.
the class SemanticReverseAbstractInterpreter method caseInstanceOf.
@CheckReturnValue
private FlowScope caseInstanceOf(Node left, Node right, FlowScope blindScope, Outcome outcome) {
JSType leftType = getTypeIfRefinable(left, blindScope);
if (leftType == null) {
return blindScope;
}
JSType rightType = right.getJSType();
ObjectType targetType = typeRegistry.getNativeObjectType(JSTypeNative.UNKNOWN_TYPE);
if (rightType != null && rightType.isFunctionType()) {
targetType = rightType.toMaybeFunctionType();
}
Visitor<JSType> visitor;
if (outcome.isTruthy()) {
visitor = new RestrictByTrueInstanceOfResultVisitor(targetType);
} else {
visitor = new RestrictByFalseInstanceOfResultVisitor(targetType);
}
return maybeRestrictName(blindScope, left, leftType, leftType.visit(visitor));
}
use of com.google.javascript.rhino.jstype.JSType in project closure-compiler by google.
the class SemanticReverseAbstractInterpreter method caseEquality.
@CheckReturnValue
private FlowScope caseEquality(Node left, Node right, FlowScope blindScope, Function<TypePair, TypePair> merging) {
// left type
JSType leftType = getTypeIfRefinable(left, blindScope);
boolean leftIsRefineable;
if (leftType != null) {
leftIsRefineable = true;
} else {
leftIsRefineable = false;
leftType = left.getJSType();
}
// right type
JSType rightType = getTypeIfRefinable(right, blindScope);
boolean rightIsRefineable;
if (rightType != null) {
rightIsRefineable = true;
} else {
rightIsRefineable = false;
rightType = right.getJSType();
}
// merged types
TypePair merged = merging.apply(new TypePair(leftType, rightType));
// creating new scope
if (merged != null) {
return maybeRestrictTwoNames(blindScope, left, leftType, leftIsRefineable ? merged.typeA : null, right, rightType, rightIsRefineable ? merged.typeB : null);
}
return blindScope;
}
use of com.google.javascript.rhino.jstype.JSType in project closure-compiler by google.
the class InferJSDocInfoTest method testJSDocIsPropagatedToFunctionTypesFromMethodAssigments.
@Test
public void testJSDocIsPropagatedToFunctionTypesFromMethodAssigments() {
// Given
testSame(srcs(lines("/** @constructor */", "function Foo() {};", "", "/**", " * I'm a method.", " * @return {number} a", " */", "Foo.prototype.method = function() { };", "", // Just a hook to access type "Foo::method".
"var x = Foo.prototype.method;")));
JSType xType = inferredTypeOfName("x");
assertThat(xType.toString()).isEqualTo("function(this:Foo): number");
// Then
assertThat(xType.getJSDocInfo().getBlockDescription()).isEqualTo("I'm a method.");
}
use of com.google.javascript.rhino.jstype.JSType in project closure-compiler by google.
the class InferJSDocInfoTest method testJSDocIsPropagatedToScopedTypes.
@Test
public void testJSDocIsPropagatedToScopedTypes() {
// Given
testSame(srcs(lines("(() => {", " /**", " * I'm a scoped user class.", " * @constructor", " */", " var Foo = function() {};", "", // Just a hook to access type "Foo".
" var x = new Foo();", "})();")));
JSType xType = inferredTypeOfName("x");
assertThat(xType.toString()).isEqualTo("Foo");
// Then
assertThat(xType.getJSDocInfo().getBlockDescription()).isEqualTo("I'm a scoped user class.");
}
Aggregations