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

The following examples show how to use com.google.javascript.rhino.jstype.JSType#isVoidType() . 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 visitFunctionDeclaration(FunctionType ftype, List<String> skipTemplateParams) {
  visitFunctionParameters(ftype, true, skipTemplateParams);
  JSType type = ftype.getReturnType();
  final JSType typeOfThis = ftype.getTypeOfThis();

  if (type == null) return;
  emit(":");
  // Closure conflates 'undefined' and 'void', and in general visitType always emits `undefined`
  // for that type.
  // In idiomatic TypeScript, `void` is used for function return types, and the "void",
  // "undefined" types are not the same.
  if (type.isVoidType()) {
    emit("void");
  } else if (typeOfThis != null && typeOfThis.isTemplateType() && typeOfThis.equals(type)) {
    // Special case: prefer polymorphic `this` type to templatized `this` param
    emit("this");
  } else {
    visitType(type);
  }
}
 
Example 2
Source File: TypedCodeGenerator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private String getTypeAnnotation(Node node) {
  // Only add annotations for things with JSDoc, or function literals.
  JSDocInfo jsdoc = NodeUtil.getBestJSDocInfo(node);
  if (jsdoc == null && !node.isFunction()) {
    return "";
  }

  JSType type = node.getJSType();
  if (type == null) {
    return "";
  } else if (type.isFunctionType()) {
    return getFunctionAnnotation(node);
  } else if (type.isEnumType()) {
    return "/** @enum {" +
        type.toMaybeEnumType().getElementsType().toAnnotationString() +
        "} */\n";
  } else if (!type.isUnknownType()
      && !type.isEmptyType()
      && !type.isVoidType()
      && !type.isFunctionPrototypeType()) {
    return "/** @type {" + node.getJSType().toAnnotationString() + "} */\n";
  } else {
    return "";
  }
}
 
Example 3
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 4
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 5
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 6
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 7
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;
}
 
Example 8
Source File: TypeInspector.java    From js-dossier with Apache License 2.0 4 votes vote down vote up
@Nullable
private TypeExpression getReturnType(
    PropertyDocs docs, Iterable<InstanceProperty> overrides, FunctionType function) {
  PropertyDocs returnDocs =
      findPropertyDocs(
          docs,
          overrides,
          input -> input != null && input.getReturnClause().getType().isPresent());

  JSType returnType = function.getReturnType();

  // Try to compensate for loss of information (how does this happen?). If the type compiler
  // says it is a templatized type, but does not have template types, or if it is an instance
  // type with a non-empty template type map, resort to JSDoc to figure out what the user wanted.
  boolean isUnknownType = returnType.isUnknownType() && !returnType.isTemplateType();
  boolean isEmptyTemplatizedType =
      returnType.isTemplatizedType() && returnType.getTemplateTypeMap().isEmpty();
  boolean isUnresolvedTemplateInstance =
      returnType.isInstanceType() && !returnType.getTemplateTypeMap().isEmpty();

  if (isUnknownType || isEmptyTemplatizedType || isUnresolvedTemplateInstance) {
    if (returnDocs != null && isKnownType(returnDocs.getJsDoc().getReturnClause())) {
      @SuppressWarnings("OptionalGetWithoutIsPresent") // Implied by isKnownType check above.
      JSTypeExpression expression = returnDocs.getJsDoc().getReturnClause().getType().get();
      returnType = evaluate(expression);
    } else {
      for (InstanceProperty property : overrides) {
        if (property.getType() != null && property.getType().isFunctionType()) {
          FunctionType fn = (FunctionType) property.getType();
          if (fn.getReturnType() != null && !fn.getReturnType().isUnknownType()) {
            returnType = fn.getReturnType();
            break;
          }
        }
      }
    }
  }

  if (returnType.isVoidType() || (returnType.isUnknownType() && !returnType.isTemplateType())) {
    return null;
  }

  NominalType context;
  if (returnDocs != null) {
    context = returnDocs.getContextType();
  } else {
    context = docs.getContextType();
  }

  TypeExpressionParser parser =
      expressionParserFactory.create(linkFactory.withTypeContext(context));
  return parser.parse(returnType);
}