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

The following examples show how to use com.google.javascript.rhino.jstype.JSType#isNullType() . 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: ClosureTypeRegistry.java    From jsinterop-generator with Apache License 2.0 5 votes vote down vote up
private TypeReference resolveTypeReference(JSType type) {
  if (type.isVoidType() || type.isNullType()) {
    return type.visit(this);
  }

  // Nullable type and optional type are represented in closure by an union type with
  // respectively Null and Undefined. We don't use this information (yet), so we remove Null
  // or Undefined before to visit the type.
  return type.restrictByNotNullOrUndefined().visit(this);
}
 
Example 2
Source File: Closure_117_TypeValidator_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Expect the type to be anything but the null or void type. If the
 * expectation is not met, issue a warning at the provided node's
 * source code position. Note that a union type that includes the
 * void type and at least one other type meets the expectation.
 * @return Whether the expectation was met.
 */
boolean expectNotNullOrUndefined(
    NodeTraversal t, Node n, JSType type, String msg, JSType expectedType) {
  if (!type.isNoType() && !type.isUnknownType() &&
      type.isSubtype(nullOrUndefined) &&
      !containsForwardDeclaredUnresolvedName(type)) {

    // There's one edge case right now that we don't handle well, and
    // that we don't want to warn about.
    // if (this.x == null) {
    //   this.initializeX();
    //   this.x.foo();
    // }
    // In this case, we incorrectly type x because of how we
    // infer properties locally. See issue 109.
    // http://code.google.com/p/closure-compiler/issues/detail?id=109
    //
    // We do not do this inference globally.
    if (n.isGetProp() &&
        !t.inGlobalScope() && type.isNullType()) {
      return true;
    }

    mismatch(t, n, msg, type, expectedType);
    return false;
  }
  return true;
}
 
Example 3
Source File: Closure_117_TypeValidator_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Expect the type to be anything but the null or void type. If the
 * expectation is not met, issue a warning at the provided node's
 * source code position. Note that a union type that includes the
 * void type and at least one other type meets the expectation.
 * @return Whether the expectation was met.
 */
boolean expectNotNullOrUndefined(
    NodeTraversal t, Node n, JSType type, String msg, JSType expectedType) {
  if (!type.isNoType() && !type.isUnknownType() &&
      type.isSubtype(nullOrUndefined) &&
      !containsForwardDeclaredUnresolvedName(type)) {

    // There's one edge case right now that we don't handle well, and
    // that we don't want to warn about.
    // if (this.x == null) {
    //   this.initializeX();
    //   this.x.foo();
    // }
    // In this case, we incorrectly type x because of how we
    // infer properties locally. See issue 109.
    // http://code.google.com/p/closure-compiler/issues/detail?id=109
    //
    // We do not do this inference globally.
    if (n.isGetProp() &&
        !t.inGlobalScope() && type.isNullType()) {
      return true;
    }

    mismatch(t, n, msg, type, expectedType);
    return false;
  }
  return true;
}
 
Example 4
Source File: Closure_6_TypeValidator_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Expect the type to be anything but the null or void type. If the
 * expectation is not met, issue a warning at the provided node's
 * source code position. Note that a union type that includes the
 * void type and at least one other type meets the expectation.
 * @return Whether the expectation was met.
 */
boolean expectNotNullOrUndefined(
    NodeTraversal t, Node n, JSType type, String msg, JSType expectedType) {
  if (!type.isNoType() && !type.isUnknownType() &&
      type.isSubtype(nullOrUndefined) &&
      !containsForwardDeclaredUnresolvedName(type)) {

    // There's one edge case right now that we don't handle well, and
    // that we don't want to warn about.
    // if (this.x == null) {
    //   this.initializeX();
    //   this.x.foo();
    // }
    // In this case, we incorrectly type x because of how we
    // infer properties locally. See issue 109.
    // http://code.google.com/p/closure-compiler/issues/detail?id=109
    //
    // We do not do this inference globally.
    if (n.isGetProp() &&
        !t.inGlobalScope() && type.isNullType()) {
      return true;
    }

    mismatch(t, n, msg, type, expectedType);
    return false;
  }
  return true;
}
 
Example 5
Source File: Closure_6_TypeValidator_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Expect the type to be anything but the null or void type. If the
 * expectation is not met, issue a warning at the provided node's
 * source code position. Note that a union type that includes the
 * void type and at least one other type meets the expectation.
 * @return Whether the expectation was met.
 */
boolean expectNotNullOrUndefined(
    NodeTraversal t, Node n, JSType type, String msg, JSType expectedType) {
  if (!type.isNoType() && !type.isUnknownType() &&
      type.isSubtype(nullOrUndefined) &&
      !containsForwardDeclaredUnresolvedName(type)) {

    // There's one edge case right now that we don't handle well, and
    // that we don't want to warn about.
    // if (this.x == null) {
    //   this.initializeX();
    //   this.x.foo();
    // }
    // In this case, we incorrectly type x because of how we
    // infer properties locally. See issue 109.
    // http://code.google.com/p/closure-compiler/issues/detail?id=109
    //
    // We do not do this inference globally.
    if (n.isGetProp() &&
        !t.inGlobalScope() && type.isNullType()) {
      return true;
    }

    mismatch(t, n, msg, type, expectedType);
    return false;
  }
  return true;
}
 
Example 6
Source File: RuntimeTypeCheck.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private String getName(JSType type) {
  if (type.isInstanceType()) {
    return ((ObjectType) type).getReferenceName();
  } else if (type.isNullType()
      || type.isBooleanValueType()
      || type.isNumberValueType()
      || type.isStringValueType()
      || type.isVoidType()) {
    return type.toString();
  } else {
    // Type unchecked at runtime, so we don't care about the sorting order.
    return "";
  }
}
 
Example 7
Source File: RuntimeTypeCheck.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a node which evaluates to a checker for the given type (which
 * must not be a union). We have checkers for value types, classes and
 * interfaces.
 *
 * @return the checker node or {@code null} if the type is not checked
 */
private Node createCheckerNode(JSType type) {
  if (type.isNullType()) {
    return jsCode("nullChecker");

  } else if (type.isBooleanValueType()
      || type.isNumberValueType()
      || type.isStringValueType()
      || type.isVoidType()) {
    return IR.call(
        jsCode("valueChecker"),
        IR.string(type.toString()));

  } else if (type.isInstanceType()) {
    ObjectType objType = (ObjectType) type;

    String refName = objType.getReferenceName();

    StaticSourceFile sourceFile =
        NodeUtil.getSourceFile(objType.getConstructor().getSource());
    if (sourceFile == null || sourceFile.isExtern()) {
      return IR.call(
              jsCode("externClassChecker"),
              IR.string(refName));
    }

    return IR.call(
            jsCode(objType.getConstructor().isInterface() ?
                    "interfaceChecker" : "classChecker"),
            IR.string(refName));

  } else {
    // We don't check this type (e.g. unknown & all types).
    return null;
  }
}
 
Example 8
Source File: TypeValidator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Expect the type to be anything but the null or void type. If the
 * expectation is not met, issue a warning at the provided node's
 * source code position. Note that a union type that includes the
 * void type and at least one other type meets the expectation.
 * @return Whether the expectation was met.
 */
boolean expectNotNullOrUndefined(
    NodeTraversal t, Node n, JSType type, String msg, JSType expectedType) {
  if (!type.isNoType() && !type.isUnknownType() &&
      type.isSubtype(nullOrUndefined) &&
      !containsForwardDeclaredUnresolvedName(type)) {

    // There's one edge case right now that we don't handle well, and
    // that we don't want to warn about.
    // if (this.x == null) {
    //   this.initializeX();
    //   this.x.foo();
    // }
    // In this case, we incorrectly type x because of how we
    // infer properties locally. See issue 109.
    // http://code.google.com/p/closure-compiler/issues/detail?id=109
    //
    // We do not do this inference globally.
    if (n.isGetProp() &&
        !t.inGlobalScope() && type.isNullType()) {
      return true;
    }

    mismatch(t, n, msg, type, expectedType);
    return false;
  }
  return true;
}
 
Example 9
Source File: PeepholeFoldWithTypes.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Folds "typeof expression" based on the JSType of "expression" if the
 * expression has no side effects.
 *
 * <p>E.g.,
 * <pre>
 * var x = 6;
 * if (typeof(x) == "number") {
 * }
 * </pre>
 * folds to
 * <pre>
 * var x = 6;
 * if ("number" == "number") {
 * }
 * </pre>
 *
 * <p>This method doesn't fold literal values -- we leave that to
 * PeepholeFoldConstants.
 */
private Node tryFoldTypeof(Node typeofNode) {
  Preconditions.checkArgument(typeofNode.isTypeOf());
  Preconditions.checkArgument(typeofNode.getFirstChild() != null);

  Node argumentNode = typeofNode.getFirstChild();

  // We'll let PeepholeFoldConstants handle folding literals
  // and we can't remove arguments with possible side effects.
  if (!NodeUtil.isLiteralValue(argumentNode, true) &&
      !mayHaveSideEffects(argumentNode)) {
    JSType argumentType = argumentNode.getJSType();

    String typeName = null;

    if (argumentType != null) {
      // typeof null is "object" in JavaScript
      if (argumentType.isObject() || argumentType.isNullType()) {
        typeName = "object";
      } else if (argumentType.isStringValueType()) {
        typeName = "string";
      } else if (argumentType.isNumberValueType()) {
        typeName = "number";
      } else if (argumentType.isBooleanValueType()) {
        typeName = "boolean";
      } else if (argumentType.isVoidType()) {
         typeName = "undefined";
      } else if (argumentType.isUnionType()) {
        // TODO(dcc): We don't handle union types, for now,
        // but could support, say, unions of different object types
        // in the future.
        typeName = null;
      }

      if (typeName != null) {
        Node newNode = IR.string(typeName);
        typeofNode.getParent().replaceChild(typeofNode, newNode);
        reportCodeChange();

        return newNode;
      }
    }
  }
  return typeofNode;
}