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

The following examples show how to use com.google.javascript.rhino.jstype.JSType#isNumberValueType() . 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: DeclarationGenerator.java    From clutz with MIT License 6 votes vote down vote up
private void emitIndexSignature(JSType keyType, JSType returnType, boolean emitBreak) {
  emit("[");
  // TS allows only number or string as index type of an object
  // https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#3.9.4
  if (keyType.isNumberValueType()) {
    emit("key: number");
  } else {
    if (!keyType.isStringValueType()) {
      emit("/* warning: coerced from " + keyType + " */");
    }
    emit("key: string");
  }
  emit("]:");
  visitType(returnType);

  if (emitBreak) {
    emit(";");
    emitBreak();
  }
}
 
Example 2
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 3
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 4
Source File: TypeCollectionPass.java    From js-dossier with Apache License 2.0 5 votes vote down vote up
private static boolean isPrimitive(JSType type) {
  return !type.isEnumElementType()
      && (type.isBooleanValueType()
          || type.isBooleanObjectType()
          || type.isNumber()
          || type.isNumberValueType()
          || type.isNumberObjectType()
          || type.isString()
          || type.isStringObjectType()
          || type.isStringValueType()
          || type.isVoidType()
          || type.isArrayType());
}
 
Example 5
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;
}