/** * Resolve the referenced type within the enclosing scope. */ @Override JSType resolveInternal(ErrorReporter t, StaticTypedScope<JSType> enclosing) { // TODO(user): Investigate whether it is really necessary to keep two // different mechanisms for resolving named types, and if so, which order // makes more sense. Now, resolution via registry is first in order to // avoid triggering the warnings built into the resolution via properties. boolean resolved = resolveViaRegistry(t); if (detectInheritanceCycle()) { handleTypeCycle(t); } if (resolved) { super.resolveInternal(t, enclosing); finishPropertyContinuations(); return registry.isLastGeneration() ? getReferencedType() : this; } resolveViaProperties(t, enclosing); if (detectInheritanceCycle()) { handleTypeCycle(t); } super.resolveInternal(t, enclosing); if (isResolved()) { finishPropertyContinuations(); } return registry.isLastGeneration() ? getReferencedType() : this; }
/** Creates a named type. */ @VisibleForTesting public NamedType createNamedType( StaticTypedScope scope, String reference, String sourceName, int lineno, int charno) { return new NamedType(scope, this, reference, sourceName, lineno, charno); }
private void setReferencedAndResolvedType( JSType type, ErrorReporter reporter) { if (validator != null) { validator.apply(type); } setReferencedType(type); checkEnumElementCycle(reporter); checkProtoCycle(reporter); setResolvedTypeInternal(getReferencedType()); }
private void checkProtoCycle(ErrorReporter reporter) { JSType referencedType = getReferencedType(); if (areIdentical(referencedType, this)) { handleTypeCycle(reporter); } }
private void checkProtoCycle(ErrorReporter t) { JSType referencedType = getReferencedType(); if (referencedType == this) { handleTypeCycle(t); } }
if (!getReferencedType().isUnknownType()) { boolean resolved = resolveViaRegistry(reporter); if (!resolved) { resolveViaProperties(reporter); if (detectInheritanceCycle()) { handleTypeCycle(reporter); finishPropertyContinuations(); JSType result = getReferencedType(); if (isSuccessfullyResolved()) { int numKeys = result.getTemplateTypeMap().numUnfilledTemplateKeys(); if (result.isObjectType() setReferencedType(result);
private void handleUnresolvedType( ErrorReporter reporter, boolean ignoreForwardReferencedTypes) { boolean isForwardDeclared = ignoreForwardReferencedTypes && registry.isForwardDeclaredType(reference); if (!isForwardDeclared) { String msg = "Bad type annotation. Unknown type " + reference; warning(reporter, msg); } else { setReferencedType(new NoResolvedType(registry, getReferenceName(), getTemplateTypes())); if (validator != null) { validator.apply(getReferencedType()); } } setResolvedTypeInternal(getReferencedType()); }
private void handleTypeCycle(ErrorReporter reporter) { setReferencedType( registry.getNativeObjectType(JSTypeNative.UNKNOWN_TYPE)); warning(reporter, "Cycle detected in inheritance chain of type " + reference); setResolvedTypeInternal(getReferencedType()); }
@Override public Void caseNamedType(NamedType type) { JSType refType = type.getReferencedType(); // When we dealing with partial inputs, we often end up with named type, without a // corresponding referencedType. Instead of emitting 'any', we emit literally the // name of the type as originally written. // It appears that when one writes '@type {A<B>}' and both are missing from the // compilation // unit - A ends up as NoType, while B ends up as NamedType. if (opts.partialInput && refType.isUnknownType()) { emitNoResolvedTypeAsumingForwardDeclare(type); return null; } // Handle "typeof expr" constructions, which translate directly to TypeScript. if (type.hasReferenceName() && type.getReferenceName().startsWith("typeof ")) { emit(type.getReferenceName()); return null; } visitType(refType); return null; }
private void addReferenceTypeIndexedByProperty( String propertyName, JSType type) { if (type instanceof ObjectType && ((ObjectType) type).hasReferenceName()) { Map<String, ObjectType> typeSet = eachRefTypeIndexedByProperty.computeIfAbsent(propertyName, k -> new LinkedHashMap<>()); ObjectType objType = (ObjectType) type; typeSet.put(objType.getReferenceName(), objType); } else if (type instanceof NamedType) { addReferenceTypeIndexedByProperty( propertyName, ((NamedType) type).getReferencedType()); } else if (type.isUnionType()) { for (JSType alternate : type.toMaybeUnionType().getAlternates()) { addReferenceTypeIndexedByProperty(propertyName, alternate); } } }
@Override StringBuilder appendTo(StringBuilder sb, boolean forAnnotations) { JSType type = this.getReferencedType(); if (!isResolved() || type.isNoResolvedType()) { return sb.append(this.reference); } else { return type.appendTo(sb, forAnnotations); } }
type = createNamedType(scope, "typeof " + name, sourceName, n.getLineno(), n.getCharno()); ((NamedType) type).setReferencedType(slot.getType()); new NamedType( scope, this,
private void finishPropertyContinuations() { ObjectType referencedObjType = getReferencedObjTypeInternal(); if (referencedObjType != null && !referencedObjType.isUnknownType() && propertyContinuations != null) { for (PropertyContinuation c : propertyContinuations) { c.commit(this); } } propertyContinuations = null; }
/** Returns the type to which this refers (which is unknown if unresolved). */ public JSType getReferencedType() { return getReferencedTypeInternal(); }
if (this.isNamedType() && that.isNamedType()) { return Objects.equals( this.toMaybeNamedType().getReferenceName(), // that.toMaybeNamedType().getReferenceName()); } else { return true;
private void handleTypeCycle(ErrorReporter t) { setReferencedType( registry.getNativeObjectType(JSTypeNative.UNKNOWN_TYPE)); warning(t, "Cycle detected in inheritance chain of type " + reference); setResolvedTypeInternal(getReferencedType()); }
private void checkEnumElementCycle(ErrorReporter reporter) { JSType referencedType = getReferencedType(); if (referencedType instanceof EnumElementType && areIdentical(this, ((EnumElementType) referencedType).getPrimitiveType())) { handleTypeCycle(reporter); } }
private void addReferenceTypeIndexedByProperty( String propertyName, JSType type) { if (type instanceof ObjectType && ((ObjectType) type).hasReferenceName()) { Map<String, ObjectType> typeSet = eachRefTypeIndexedByProperty.get(propertyName); if (typeSet == null) { typeSet = new LinkedHashMap<String, ObjectType>(); eachRefTypeIndexedByProperty.put(propertyName, typeSet); } ObjectType objType = (ObjectType) type; typeSet.put(objType.getReferenceName(), objType); } else if (type instanceof NamedType) { addReferenceTypeIndexedByProperty( propertyName, ((NamedType) type).getReferencedType()); } else if (type.isUnionType()) { for (JSType alternate : type.toMaybeUnionType().getAlternates()) { addReferenceTypeIndexedByProperty(propertyName, alternate); } } }
private void checkEnumElementCycle(ErrorReporter t) { JSType referencedType = getReferencedType(); if (referencedType instanceof EnumElementType && ((EnumElementType) referencedType).getPrimitiveType() == this) { handleTypeCycle(t); } }
private void finishPropertyContinuations() { ObjectType referencedObjType = getReferencedObjTypeInternal(); if (referencedObjType != null && !referencedObjType.isUnknownType() && propertyContinuations != null) { for (PropertyContinuation c : propertyContinuations) { c.commit(this); } } propertyContinuations = null; }