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

The following examples show how to use com.google.javascript.rhino.jstype.JSType#restrictByNotNullOrUndefined() . 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: Closure_117_TypeValidator_t.java    From coming with MIT License 6 votes vote down vote up
private void registerMismatch(JSType found, JSType required, JSError error) {
  // Don't register a mismatch for differences in null or undefined or if the
  // code didn't downcast.
  found = found.restrictByNotNullOrUndefined();
  required = required.restrictByNotNullOrUndefined();
  if (found.isSubtype(required) || required.isSubtype(found)) {
    return;
  }

  mismatches.add(new TypeMismatch(found, required, error));
  if (found.isFunctionType() &&
      required.isFunctionType()) {
    FunctionType fnTypeA = found.toMaybeFunctionType();
    FunctionType fnTypeB = required.toMaybeFunctionType();
    Iterator<Node> paramItA = fnTypeA.getParameters().iterator();
    Iterator<Node> paramItB = fnTypeB.getParameters().iterator();
    while (paramItA.hasNext() && paramItB.hasNext()) {
      registerIfMismatch(paramItA.next().getJSType(),
          paramItB.next().getJSType(), error);
    }

    registerIfMismatch(
        fnTypeA.getReturnType(), fnTypeB.getReturnType(), error);
  }
}
 
Example 2
Source File: Closure_6_TypeValidator_t.java    From coming with MIT License 6 votes vote down vote up
private void registerMismatch(JSType found, JSType required, JSError error) {
  // Don't register a mismatch for differences in null or undefined or if the
  // code didn't downcast.
  found = found.restrictByNotNullOrUndefined();
  required = required.restrictByNotNullOrUndefined();
  if (found.canAssignTo(required) || required.canAssignTo(found)) {
    return;
  }

  mismatches.add(new TypeMismatch(found, required, error));
  if (found.isFunctionType() &&
      required.isFunctionType()) {
    FunctionType fnTypeA = found.toMaybeFunctionType();
    FunctionType fnTypeB = required.toMaybeFunctionType();
    Iterator<Node> paramItA = fnTypeA.getParameters().iterator();
    Iterator<Node> paramItB = fnTypeB.getParameters().iterator();
    while (paramItA.hasNext() && paramItB.hasNext()) {
      registerIfMismatch(paramItA.next().getJSType(),
          paramItB.next().getJSType(), error);
    }

    registerIfMismatch(
        fnTypeA.getReturnType(), fnTypeB.getReturnType(), error);
  }
}
 
Example 3
Source File: Closure_117_TypeValidator_s.java    From coming with MIT License 6 votes vote down vote up
private void registerMismatch(JSType found, JSType required, JSError error) {
  // Don't register a mismatch for differences in null or undefined or if the
  // code didn't downcast.
  found = found.restrictByNotNullOrUndefined();
  required = required.restrictByNotNullOrUndefined();
  if (found.isSubtype(required) || required.isSubtype(found)) {
    return;
  }

  mismatches.add(new TypeMismatch(found, required, error));
  if (found.isFunctionType() &&
      required.isFunctionType()) {
    FunctionType fnTypeA = found.toMaybeFunctionType();
    FunctionType fnTypeB = required.toMaybeFunctionType();
    Iterator<Node> paramItA = fnTypeA.getParameters().iterator();
    Iterator<Node> paramItB = fnTypeB.getParameters().iterator();
    while (paramItA.hasNext() && paramItB.hasNext()) {
      registerIfMismatch(paramItA.next().getJSType(),
          paramItB.next().getJSType(), error);
    }

    registerIfMismatch(
        fnTypeA.getReturnType(), fnTypeB.getReturnType(), error);
  }
}
 
Example 4
Source File: TypeValidator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private void registerMismatch(JSType found, JSType required, JSError error) {
  // Don't register a mismatch for differences in null or undefined or if the
  // code didn't downcast.
  found = found.restrictByNotNullOrUndefined();
  required = required.restrictByNotNullOrUndefined();
  if (found.isSubtype(required) || required.isSubtype(found)) {
    return;
  }

  mismatches.add(new TypeMismatch(found, required, error));
  if (found.isFunctionType() &&
      required.isFunctionType()) {
    FunctionType fnTypeA = found.toMaybeFunctionType();
    FunctionType fnTypeB = required.toMaybeFunctionType();
    Iterator<Node> paramItA = fnTypeA.getParameters().iterator();
    Iterator<Node> paramItB = fnTypeB.getParameters().iterator();
    while (paramItA.hasNext() && paramItB.hasNext()) {
      registerIfMismatch(paramItA.next().getJSType(),
          paramItB.next().getJSType(), error);
    }

    registerIfMismatch(
        fnTypeA.getReturnType(), fnTypeB.getReturnType(), error);
  }
}
 
Example 5
Source File: Closure_103_DisambiguateProperties_t.java    From coming with MIT License 6 votes vote down vote up
private ConcreteType maybeAddAutoboxes(
    ConcreteType cType, JSType jsType, String prop) {
  jsType = jsType.restrictByNotNullOrUndefined();
  if (jsType instanceof UnionType) {
    for (JSType alt : ((UnionType) jsType).getAlternates()) {
      return maybeAddAutoboxes(cType, alt, prop);
    }
  }

  if (jsType.autoboxesTo() != null) {
    JSType autoboxed = jsType.autoboxesTo();
    return cType.unionWith(tt.getConcreteInstance((ObjectType) autoboxed));
  } else if (jsType.unboxesTo() != null) {
    return cType.unionWith(tt.getConcreteInstance((ObjectType) jsType));
  }

  return cType;
}
 
Example 6
Source File: Closure_118_DisambiguateProperties_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Invalidates the given type, so that no properties on it will be renamed.
 */
private void addInvalidatingType(JSType type, JSError error) {
  type = type.restrictByNotNullOrUndefined();
  if (type.isUnionType()) {
    for (JSType alt : type.toMaybeUnionType().getAlternates()) {
      addInvalidatingType(alt, error);
    }
  } else if (type.isEnumElementType()) {
    addInvalidatingType(
        type.toMaybeEnumElementType().getPrimitiveType(), error);
  } else {
    typeSystem.addInvalidatingType(type);
    recordInvalidationError(type, error);
    ObjectType objType = ObjectType.cast(type);
    if (objType != null && objType.getImplicitPrototype() != null) {
      typeSystem.addInvalidatingType(objType.getImplicitPrototype());
      recordInvalidationError(objType.getImplicitPrototype(), error);
    }
  }
}
 
Example 7
Source File: Closure_103_DisambiguateProperties_s.java    From coming with MIT License 6 votes vote down vote up
private ConcreteType maybeAddAutoboxes(
    ConcreteType cType, JSType jsType, String prop) {
  jsType = jsType.restrictByNotNullOrUndefined();
  if (jsType instanceof UnionType) {
    for (JSType alt : ((UnionType) jsType).getAlternates()) {
      return maybeAddAutoboxes(cType, alt, prop);
    }
  }

  if (jsType.autoboxesTo() != null) {
    JSType autoboxed = jsType.autoboxesTo();
    return cType.unionWith(tt.getConcreteInstance((ObjectType) autoboxed));
  } else if (jsType.unboxesTo() != null) {
    return cType.unionWith(tt.getConcreteInstance((ObjectType) jsType));
  }

  return cType;
}
 
Example 8
Source File: TypeValidator.java    From astor with GNU General Public License v2.0 5 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();
    JSType required
        = implementedInterface.getImplicitPrototype().getPropertyType(prop);
    found = found.restrictByNotNullOrUndefined();
    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: InlineProperties.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private boolean isMatchingType(Node n, JSType src) {
  src = src.restrictByNotNullOrUndefined();
  JSType dest = getJSType(n).restrictByNotNullOrUndefined();
  if (!isInvalidatingType(dest)
      && dest.isSubtype(src)) {
    return true;
  }
  return false;
}
 
Example 10
Source File: Closure_35_TypeInference_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * If we access a property of a symbol, then that symbol is not
 * null or undefined.
 */
private FlowScope dereferencePointer(Node n, FlowScope scope) {
  if (n.isQualifiedName()) {
    JSType type = getJSType(n);
    JSType narrowed = type.restrictByNotNullOrUndefined();
    if (type != narrowed) {
      scope = narrowScope(scope, n, narrowed);
    }
  }
  return scope;
}
 
Example 11
Source File: Closure_6_TypeValidator_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Expect that the first type can be cast to the second type. The first type
 * should be either a subtype or supertype of the second.
 *
 * @param t The node traversal.
 * @param n The node where warnings should point.
 * @param type The type being cast from.
 * @param castType The type being cast to.
 */
void expectCanCast(NodeTraversal t, Node n, JSType type, JSType castType) {
  castType = castType.restrictByNotNullOrUndefined();
  type = type.restrictByNotNullOrUndefined();

  if (!type.canAssignTo(castType) && !castType.canAssignTo(type)) {
    registerMismatch(type, castType, report(t.makeError(n, INVALID_CAST,
        castType.toString(), type.toString())));
  }
}
 
Example 12
Source File: Closure_6_TypeValidator_t.java    From coming with MIT License 5 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();
    JSType required
        = implementedInterface.getImplicitPrototype().getPropertyType(prop);
    found = found.restrictByNotNullOrUndefined();
    required = required.restrictByNotNullOrUndefined();
    if (!found.canAssignTo(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 13
Source File: InlineProperties.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Invalidates the given type, so that no properties on it will be renamed.
 */
private void addInvalidatingType(JSType type) {
  type = type.restrictByNotNullOrUndefined();
  if (type.isUnionType()) {
    for (JSType alt : type.toMaybeUnionType().getAlternates()) {
      addInvalidatingType(alt);
    }
  }

  invalidatingTypes.add(type);
  ObjectType objType = ObjectType.cast(type);
  if (objType != null && objType.isInstanceType()) {
    invalidatingTypes.add(objType.getImplicitPrototype());
  }
}
 
Example 14
Source File: Closure_112_TypeInference_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * If we access a property of a symbol, then that symbol is not
 * null or undefined.
 */
private FlowScope dereferencePointer(Node n, FlowScope scope) {
  if (n.isQualifiedName()) {
    JSType type = getJSType(n);
    JSType narrowed = type.restrictByNotNullOrUndefined();
    if (type != narrowed) {
      scope = narrowScope(scope, n, narrowed);
    }
  }
  return scope;
}
 
Example 15
Source File: Closure_25_TypeInference_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * If we access a property of a symbol, then that symbol is not
 * null or undefined.
 */
private FlowScope dereferencePointer(Node n, FlowScope scope) {
  if (n.isQualifiedName()) {
    JSType type = getJSType(n);
    JSType narrowed = type.restrictByNotNullOrUndefined();
    if (type != narrowed) {
      scope = narrowScope(scope, n, narrowed);
    }
  }
  return scope;
}
 
Example 16
Source File: NameReferenceGraphConstruction.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * A helper to retrieve the type of a node.
 */
private JSType getType(Node n) {
  JSType type = n.getJSType();
  if (type == null) {
    if (CONSERVATIVE) {
      throw new RuntimeException("Type system failed us :(");
    } else {
      return compiler.getTypeRegistry().getNativeType(
          JSTypeNative.UNKNOWN_TYPE);
    }
  }
  // Null-ability does not affect the name graph's result.
  return type.restrictByNotNullOrUndefined();
}
 
Example 17
Source File: Closure_103_DisambiguateProperties_s.java    From coming with MIT License 4 votes vote down vote up
@Override public JSType restrictByNotNullOrUndefined(JSType type) {
  return type.restrictByNotNullOrUndefined();
}
 
Example 18
Source File: Closure_103_DisambiguateProperties_t.java    From coming with MIT License 4 votes vote down vote up
@Override public JSType restrictByNotNullOrUndefined(JSType type) {
  return type.restrictByNotNullOrUndefined();
}
 
Example 19
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 20
Source File: DisambiguateProperties.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Override public JSType restrictByNotNullOrUndefined(JSType type) {
  return type.restrictByNotNullOrUndefined();
}