Java Code Examples for com.google.javascript.rhino.jstype.JSType#visit()

The following examples show how to use com.google.javascript.rhino.jstype.JSType#visit() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Cardumen_0024_t.java    From coming with MIT License 5 votes vote down vote up
@Override
public JSType caseUnionType(UnionType type) {
  JSType restricted = null;
  for (JSType alternate : type.getAlternates()) {
    JSType restrictedAlternate = alternate.visit(this);
    if (restrictedAlternate != null) {
      if (restricted == null) {
        restricted = restrictedAlternate;
      } else {
        restricted = restrictedAlternate.getLeastSupertype(restricted);
      }
    }
  }
  return restricted;
}
 
Example 2
Source File: Closure_19_ChainableReverseAbstractInterpreter_t.java    From coming with MIT License 5 votes vote down vote up
@Override
public JSType caseUnionType(UnionType type) {
  JSType restricted = null;
  for (JSType alternate : type.getAlternates()) {
    JSType restrictedAlternate = alternate.visit(this);
    if (restrictedAlternate != null) {
      if (restricted == null) {
        restricted = restrictedAlternate;
      } else {
        restricted = restrictedAlternate.getLeastSupertype(restricted);
      }
    }
  }
  return restricted;
}
 
Example 3
Source File: Cardumen_0024_s.java    From coming with MIT License 5 votes vote down vote up
@Override
public JSType caseUnionType(UnionType type) {
  JSType restricted = null;
  for (JSType alternate : type.getAlternates()) {
    JSType restrictedAlternate = alternate.visit(this);
    if (restrictedAlternate != null) {
      if (restricted == null) {
        restricted = restrictedAlternate;
      } else {
        restricted = restrictedAlternate.getLeastSupertype(restricted);
      }
    }
  }
  return restricted;
}
 
Example 4
Source File: TypeExpressionParser.java    From js-dossier with Apache License 2.0 5 votes vote down vote up
private void caseUnionType(UnionType type, boolean filterVoid) {
  com.github.jsdossier.proto.UnionType.Builder unionType =
      currentExpression().getUnionTypeBuilder();

  Set<TypeExpression> alternateTypes = new LinkedHashSet<>();
  for (JSType alternate : type.getAlternates()) {
    TypeExpression.Builder alternateType = TypeExpression.newBuilder();

    expressions.addLast(alternateType);
    alternate.visit(this);
    expressions.removeLast();

    if (TypeExpression.NodeTypeCase.UNION_TYPE.equals(alternateType.getNodeTypeCase())) {
      alternateTypes.addAll(alternateType.getUnionType().getTypeList());
    } else {
      alternateTypes.add(alternateType.build());
    }
  }

  if (filterVoid) {
    alternateTypes.remove(VOID_TYPE);
  }

  unionType.addAllType(UNION_ORDERING.sortedCopy(alternateTypes));
  if (unionType.getTypeCount() == 1) {
    currentExpression().clearUnionType();
    currentExpression().mergeFrom(unionType.getType(0));
  }
}
 
Example 5
Source File: TypeInspector.java    From js-dossier with Apache License 2.0 5 votes vote down vote up
private JSType evaluate(JSTypeExpression expression) {
  if (!resolvedExpressions.contains(expression)) {
    resolveNames(expression.getRoot());
    resolvedExpressions.add(expression);
  }

  JSType type = Types.evaluate(expression, globalScope, jsRegistry);
  return type.visit(typeMapReplacer);
}
 
Example 6
Source File: Closure_117_TypeValidator_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Expect that the property in an interface that this type implements is
 * implemented and correctly typed.
 */
private void expectInterfaceProperty(NodeTraversal t, Node n,
    ObjectType instance, ObjectType implementedInterface, String prop) {
  StaticSlot<JSType> propSlot = instance.getSlot(prop);
  if (propSlot == null) {
    // Not implemented
    String sourceName = n.getSourceFileName();
    sourceName = sourceName == null ? "" : sourceName;
    registerMismatch(instance, implementedInterface,
        report(JSError.make(sourceName, n,
        INTERFACE_METHOD_NOT_IMPLEMENTED,
        prop, implementedInterface.toString(), instance.toString())));
  } else {
    Node propNode = propSlot.getDeclaration() == null ?
        null : propSlot.getDeclaration().getNode();

    // Fall back on the constructor node if we can't find a node for the
    // property.
    propNode = propNode == null ? n : propNode;

    JSType found = propSlot.getType();
    found = found.restrictByNotNullOrUndefined();

    JSType required
        = implementedInterface.getImplicitPrototype().getPropertyType(prop);
    TemplateTypeMap typeMap = implementedInterface.getTemplateTypeMap();
    if (!typeMap.isEmpty()) {
      TemplateTypeMapReplacer replacer = new TemplateTypeMapReplacer(
          typeRegistry, typeMap);
      required = required.visit(replacer);
    }
    required = required.restrictByNotNullOrUndefined();

    if (!found.isSubtype(required)) {
      // Implemented, but not correctly typed
      FunctionType constructor =
          implementedInterface.toObjectType().getConstructor();
      registerMismatch(found, required, report(t.makeError(propNode,
          HIDDEN_INTERFACE_PROPERTY_MISMATCH, prop,
          constructor.getTopMostDefiningType(prop).toString(),
          required.toString(), found.toString())));
    }
  }
}
 
Example 7
Source File: TypeInspector.java    From js-dossier with Apache License 2.0 4 votes vote down vote up
/**
 * Extracts information on the members (both functions and properties) of the given type.
 *
 * <p>The returned report will include information on all properties on the type, regardless of
 * whether the property is defined directly on the nominal type or one of its super
 * types/interfaces.
 */
public Report inspectInstanceType() {
  if (!inspectedType.getType().isConstructor() && !inspectedType.getType().isInterface()) {
    return Report.empty();
  }

  Report.Builder report = Report.builder();
  Multimap<String, InstanceProperty> properties =
      MultimapBuilder.treeKeys().linkedHashSetValues().build();

  for (JSType assignableType : getAssignableTypes(inspectedType.getType())) {
    for (Map.Entry<String, InstanceProperty> entry :
        getInstanceProperties(assignableType).entrySet()) {
      properties.put(entry.getKey(), entry.getValue());
    }
  }

  final JSType currentType = ((FunctionType) inspectedType.getType()).getInstanceType();
  final TemplateTypeReplacer replacer =
      TemplateTypeReplacer.forPartialReplacement(jsRegistry, currentType.getTemplateTypeMap());

  for (String key : properties.keySet()) {
    Deque<InstanceProperty> definitions = new ArrayDeque<>(properties.get(key));
    JSType propertyType = findPropertyType(definitions);
    InstanceProperty property = definitions.removeFirst();

    if (property.getJsDoc() != null
        && property.getJsDoc().getVisibility() == JSDocInfo.Visibility.PRIVATE) {
      continue;
    }

    NominalType ownerType = property.getOwnerType().orElse(inspectedType);
    DefinedByType definedBy = getDefinedByComment(linkFactory, ownerType, currentType, property);

    if (!currentType.getTemplateTypeMap().isEmpty()) {
      propertyType = propertyType.visit(replacer);
    }

    if (propertyType.isFunctionType()) {
      report
          .functionsBuilder()
          .add(
              getFunctionData(
                  property.getName(),
                  propertyType.toMaybeFunctionType(),
                  property.getNode(),
                  PropertyDocs.create(ownerType, property.getJsDoc()),
                  definedBy,
                  definitions));
    } else {
      report
          .propertiesBuilder()
          .add(
              getPropertyData(
                  property.getName(),
                  propertyType,
                  property.getNode(),
                  PropertyDocs.create(ownerType, property.getJsDoc()),
                  definedBy,
                  definitions));
    }
  }

  return report.build();
}
 
Example 8
Source File: Closure_117_TypeValidator_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Expect that the property in an interface that this type implements is
 * implemented and correctly typed.
 */
private void expectInterfaceProperty(NodeTraversal t, Node n,
    ObjectType instance, ObjectType implementedInterface, String prop) {
  StaticSlot<JSType> propSlot = instance.getSlot(prop);
  if (propSlot == null) {
    // Not implemented
    String sourceName = n.getSourceFileName();
    sourceName = sourceName == null ? "" : sourceName;
    registerMismatch(instance, implementedInterface,
        report(JSError.make(sourceName, n,
        INTERFACE_METHOD_NOT_IMPLEMENTED,
        prop, implementedInterface.toString(), instance.toString())));
  } else {
    Node propNode = propSlot.getDeclaration() == null ?
        null : propSlot.getDeclaration().getNode();

    // Fall back on the constructor node if we can't find a node for the
    // property.
    propNode = propNode == null ? n : propNode;

    JSType found = propSlot.getType();
    found = found.restrictByNotNullOrUndefined();

    JSType required
        = implementedInterface.getImplicitPrototype().getPropertyType(prop);
    TemplateTypeMap typeMap = implementedInterface.getTemplateTypeMap();
    if (!typeMap.isEmpty()) {
      TemplateTypeMapReplacer replacer = new TemplateTypeMapReplacer(
          typeRegistry, typeMap);
      required = required.visit(replacer);
    }
    required = required.restrictByNotNullOrUndefined();

    if (!found.isSubtype(required)) {
      // Implemented, but not correctly typed
      FunctionType constructor =
          implementedInterface.toObjectType().getConstructor();
      registerMismatch(found, required, report(t.makeError(propNode,
          HIDDEN_INTERFACE_PROPERTY_MISMATCH, prop,
          constructor.getTopMostDefiningType(prop).toString(),
          required.toString(), found.toString())));
    }
  }
}
 
Example 9
Source File: Closure_7_ChainableReverseAbstractInterpreter_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Returns a version of type where undefined is not present.
 */
protected final JSType getRestrictedWithoutUndefined(JSType type) {
  return type == null ? null : type.visit(restrictUndefinedVisitor);
}
 
Example 10
Source File: Closure_19_ChainableReverseAbstractInterpreter_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Returns a version of type where undefined is not present.
 */
protected final JSType getRestrictedWithoutUndefined(JSType type) {
  return type == null ? null : type.visit(restrictUndefinedVisitor);
}
 
Example 11
Source File: Closure_7_ChainableReverseAbstractInterpreter_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Returns a version of type where null is not present.
 */
protected final JSType getRestrictedWithoutNull(JSType type) {
  return type == null ? null : type.visit(restrictNullVisitor);
}
 
Example 12
Source File: Closure_7_ChainableReverseAbstractInterpreter_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Returns a version of type where undefined is not present.
 */
protected final JSType getRestrictedWithoutUndefined(JSType type) {
  return type == null ? null : type.visit(restrictUndefinedVisitor);
}
 
Example 13
Source File: Cardumen_0024_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Returns a version of type where null is not present.
 */
protected final JSType getRestrictedWithoutNull(JSType type) {
  return type == null ? null : type.visit(restrictNullVisitor);
}
 
Example 14
Source File: Cardumen_0024_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Returns a version of type where undefined is not present.
 */
protected final JSType getRestrictedWithoutUndefined(JSType type) {
  return type == null ? null : type.visit(restrictUndefinedVisitor);
}
 
Example 15
Source File: ChainableReverseAbstractInterpreter.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns a version of type where null is not present.
 */
protected final JSType getRestrictedWithoutNull(JSType type) {
  return type == null ? null : type.visit(restrictNullVisitor);
}
 
Example 16
Source File: Cardumen_0024_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Returns a version of type where null is not present.
 */
protected final JSType getRestrictedWithoutNull(JSType type) {
  return type == null ? null : type.visit(restrictNullVisitor);
}
 
Example 17
Source File: Closure_19_ChainableReverseAbstractInterpreter_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Returns a version of type where undefined is not present.
 */
protected final JSType getRestrictedWithoutUndefined(JSType type) {
  return type == null ? null : type.visit(restrictUndefinedVisitor);
}
 
Example 18
Source File: Cardumen_0024_s.java    From coming with MIT License 3 votes vote down vote up
/**
 * Returns a version of {@code type} that is restricted by some knowledge
 * about the result of the {@code typeof} operation.
 * <p>
 * The behavior of the {@code typeof} operator can be summarized by the
 * following table:
 * <table>
 * <tr><th>type</th><th>result</th></tr>
 * <tr><td>{@code undefined}</td><td>"undefined"</td></tr>
 * <tr><td>{@code null}</td><td>"object"</td></tr>
 * <tr><td>{@code boolean}</td><td>"boolean"</td></tr>
 * <tr><td>{@code number}</td><td>"number"</td></tr>
 * <tr><td>{@code string}</td><td>"string"</td></tr>
 * <tr><td>{@code Object} (which doesn't implement [[Call]])</td>
 *     <td>"object"</td></tr>
 * <tr><td>{@code Object} (which implements [[Call]])</td>
 *     <td>"function"</td></tr>
 * </table>
 * @param type the type to restrict
 * @param value A value known to be equal or not equal to the result of the
 *        {@code typeof} operation
 * @param resultEqualsValue {@code true} if the {@code typeOf} result is known
 *        to equal {@code value}; {@code false} if it is known <em>not</em> to
 *        equal {@code value}
 * @return the restricted type or null if no version of the type matches the
 *         restriction
 */
JSType getRestrictedByTypeOfResult(JSType type, String value,
                                   boolean resultEqualsValue) {
  if (type == null) {
    if (resultEqualsValue) {
      JSType result = getNativeTypeForTypeOf(value);
      return result == null ? getNativeType(CHECKED_UNKNOWN_TYPE) : result;
    } else {
      return null;
    }
  }
  return type.visit(
      new RestrictByOneTypeOfResultVisitor(value, resultEqualsValue));
}
 
Example 19
Source File: Closure_7_ChainableReverseAbstractInterpreter_s.java    From coming with MIT License 3 votes vote down vote up
/**
 * Returns a version of {@code type} that is restricted by some knowledge
 * about the result of the {@code typeof} operation.
 * <p>
 * The behavior of the {@code typeof} operator can be summarized by the
 * following table:
 * <table>
 * <tr><th>type</th><th>result</th></tr>
 * <tr><td>{@code undefined}</td><td>"undefined"</td></tr>
 * <tr><td>{@code null}</td><td>"object"</td></tr>
 * <tr><td>{@code boolean}</td><td>"boolean"</td></tr>
 * <tr><td>{@code number}</td><td>"number"</td></tr>
 * <tr><td>{@code string}</td><td>"string"</td></tr>
 * <tr><td>{@code Object} (which doesn't implement [[Call]])</td>
 *     <td>"object"</td></tr>
 * <tr><td>{@code Object} (which implements [[Call]])</td>
 *     <td>"function"</td></tr>
 * </table>
 * @param type the type to restrict
 * @param value A value known to be equal or not equal to the result of the
 *        {@code typeof} operation
 * @param resultEqualsValue {@code true} if the {@code typeOf} result is known
 *        to equal {@code value}; {@code false} if it is known <em>not</em> to
 *        equal {@code value}
 * @return the restricted type or null if no version of the type matches the
 *         restriction
 */
JSType getRestrictedByTypeOfResult(JSType type, String value,
                                   boolean resultEqualsValue) {
  if (type == null) {
    if (resultEqualsValue) {
      JSType result = getNativeTypeForTypeOf(value);
      return result == null ? getNativeType(CHECKED_UNKNOWN_TYPE) : result;
    } else {
      return null;
    }
  }
  return type.visit(
      new RestrictByOneTypeOfResultVisitor(value, resultEqualsValue));
}
 
Example 20
Source File: ChainableReverseAbstractInterpreter.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns a version of {@code type} that is restricted by some knowledge
 * about the result of the {@code typeof} operation.
 * <p>
 * The behavior of the {@code typeof} operator can be summarized by the
 * following table:
 * <table>
 * <tr><th>type</th><th>result</th></tr>
 * <tr><td>{@code undefined}</td><td>"undefined"</td></tr>
 * <tr><td>{@code null}</td><td>"object"</td></tr>
 * <tr><td>{@code boolean}</td><td>"boolean"</td></tr>
 * <tr><td>{@code number}</td><td>"number"</td></tr>
 * <tr><td>{@code string}</td><td>"string"</td></tr>
 * <tr><td>{@code Object} (which doesn't implement [[Call]])</td>
 *     <td>"object"</td></tr>
 * <tr><td>{@code Object} (which implements [[Call]])</td>
 *     <td>"function"</td></tr>
 * </table>
 * @param type the type to restrict
 * @param value A value known to be equal or not equal to the result of the
 *        {@code typeof} operation
 * @param resultEqualsValue {@code true} if the {@code typeOf} result is known
 *        to equal {@code value}; {@code false} if it is known <em>not</em> to
 *        equal {@code value}
 * @return the restricted type or null if no version of the type matches the
 *         restriction
 */
JSType getRestrictedByTypeOfResult(JSType type, String value,
                                   boolean resultEqualsValue) {
  if (type == null) {
    if (resultEqualsValue) {
      JSType result = getNativeTypeForTypeOf(value);
      return result == null ? getNativeType(CHECKED_UNKNOWN_TYPE) : result;
    } else {
      return null;
    }
  }
  return type.visit(
      new RestrictByOneTypeOfResultVisitor(value, resultEqualsValue));
}