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

The following examples show how to use com.google.javascript.rhino.jstype.JSType#isEnumType() . 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: AbstractClosureVisitor.java    From jsinterop-generator with Apache License 2.0 6 votes vote down vote up
private void accept(StaticTypedSlot var, boolean isStatic) {
  JSType type = var.getType();

  if ((type.isInterface() || type.isConstructor())
      && !type.isInstanceType()
      && toFunctionType(type).getSource() != null
      && !isTypeAlias(var)) {
    acceptClassOrInterface(toFunctionType(type));
  } else if (isModule(type)) {
    acceptModule(var);
  } else if (isTypedef(var)) {
    acceptTypedef(var);
  } else if (type.isEnumType()) {
    acceptEnumType(toEnumType(type));
  } else if (isTypeAlias(var)) {
    // We don't process type alias.
    //   /** @constructor */ function Foo() {};
    //   /** @const */ var Bar = Foo;
  } else {
    acceptMember(var, isStatic);
  }
}
 
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: DeclarationGenerator.java    From clutz with MIT License 6 votes vote down vote up
private boolean isPrivate(JSType type) {
  // Due to https://github.com/google/closure-compiler/issues/1975 we cannot obtain the JSDoc
  // for a typedef. Assume non-private as it is more common.
  // Closure creates a NamedType when the typedef is used in an union, eg: T | null.
  // Dereference the named type before checking if it is a typedef.
  NamedType nType = type.toMaybeNamedType();
  if (typedefs.containsKey(type)
      || (nType != null && typedefs.containsKey(nType.getReferencedType()))) {
    return false;
  }

  // For unknown reasons, enum types do not keep their defining jsdoc info.
  if (type.isEnumType() || type.isEnumElementType()) {
    return isPrivate(type.getDisplayName());
  } else {
    return isPrivate(type.getJSDocInfo());
  }
}
 
Example 4
Source File: Nopol2017_0029_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * <p>Checks enum aliases.
 *
 * <p>We verify that the enum element type of the enum used
 * for initialization is a subtype of the enum element type of
 * the enum the value is being copied in.</p>
 *
 * <p>Example:</p>
 * <pre>var myEnum = myOtherEnum;</pre>
 *
 * <p>Enum aliases are irregular, so we need special code for this :(</p>
 *
 * @param value the value used for initialization of the enum
 */
private void checkEnumAlias(
    NodeTraversal t, JSDocInfo declInfo, Node value) {
  if (declInfo == null || !declInfo.hasEnumParameterType()) {
    return;
  }

  JSType valueType = getJSType(value);
  if (!valueType.isEnumType()) {
    return;
  }

  EnumType valueEnumType = valueType.toMaybeEnumType();
  JSType valueEnumPrimitiveType =
      valueEnumType.getElementsType().getPrimitiveType();
  validator.expectCanAssignTo(t, value, valueEnumPrimitiveType,
      declInfo.getEnumParameterType().evaluate(t.getScope(), typeRegistry),
      "incompatible enum element types");
}
 
Example 5
Source File: TypeCheck.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * <p>Checks enum aliases.
 *
 * <p>We verify that the enum element type of the enum used
 * for initialization is a subtype of the enum element type of
 * the enum the value is being copied in.</p>
 *
 * <p>Example:</p>
 * <pre>var myEnum = myOtherEnum;</pre>
 *
 * <p>Enum aliases are irregular, so we need special code for this :(</p>
 *
 * @param value the value used for initialization of the enum
 */
private void checkEnumAlias(
    NodeTraversal t, JSDocInfo declInfo, Node value) {
  if (declInfo == null || !declInfo.hasEnumParameterType()) {
    return;
  }

  JSType valueType = getJSType(value);
  if (!valueType.isEnumType()) {
    return;
  }

  EnumType valueEnumType = valueType.toMaybeEnumType();
  JSType valueEnumPrimitiveType =
      valueEnumType.getElementsType().getPrimitiveType();
  validator.expectCanAssignTo(t, value, valueEnumPrimitiveType,
      declInfo.getEnumParameterType().evaluate(t.getScope(), typeRegistry),
      "incompatible enum element types");
}
 
Example 6
Source File: Closure_125_TypeCheck_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * <p>Checks enum aliases.
 *
 * <p>We verify that the enum element type of the enum used
 * for initialization is a subtype of the enum element type of
 * the enum the value is being copied in.</p>
 *
 * <p>Example:</p>
 * <pre>var myEnum = myOtherEnum;</pre>
 *
 * <p>Enum aliases are irregular, so we need special code for this :(</p>
 *
 * @param value the value used for initialization of the enum
 */
private void checkEnumAlias(
    NodeTraversal t, JSDocInfo declInfo, Node value) {
  if (declInfo == null || !declInfo.hasEnumParameterType()) {
    return;
  }

  JSType valueType = getJSType(value);
  if (!valueType.isEnumType()) {
    return;
  }

  EnumType valueEnumType = valueType.toMaybeEnumType();
  JSType valueEnumPrimitiveType =
      valueEnumType.getElementsType().getPrimitiveType();
  validator.expectCanAssignTo(t, value, valueEnumPrimitiveType,
      declInfo.getEnumParameterType().evaluate(t.getScope(), typeRegistry),
      "incompatible enum element types");
}
 
Example 7
Source File: Closure_125_TypeCheck_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * <p>Checks enum aliases.
 *
 * <p>We verify that the enum element type of the enum used
 * for initialization is a subtype of the enum element type of
 * the enum the value is being copied in.</p>
 *
 * <p>Example:</p>
 * <pre>var myEnum = myOtherEnum;</pre>
 *
 * <p>Enum aliases are irregular, so we need special code for this :(</p>
 *
 * @param value the value used for initialization of the enum
 */
private void checkEnumAlias(
    NodeTraversal t, JSDocInfo declInfo, Node value) {
  if (declInfo == null || !declInfo.hasEnumParameterType()) {
    return;
  }

  JSType valueType = getJSType(value);
  if (!valueType.isEnumType()) {
    return;
  }

  EnumType valueEnumType = valueType.toMaybeEnumType();
  JSType valueEnumPrimitiveType =
      valueEnumType.getElementsType().getPrimitiveType();
  validator.expectCanAssignTo(t, value, valueEnumPrimitiveType,
      declInfo.getEnumParameterType().evaluate(t.getScope(), typeRegistry),
      "incompatible enum element types");
}
 
Example 8
Source File: Closure_11_TypeCheck_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * <p>Checks enum aliases.
 *
 * <p>We verify that the enum element type of the enum used
 * for initialization is a subtype of the enum element type of
 * the enum the value is being copied in.</p>
 *
 * <p>Example:</p>
 * <pre>var myEnum = myOtherEnum;</pre>
 *
 * <p>Enum aliases are irregular, so we need special code for this :(</p>
 *
 * @param value the value used for initialization of the enum
 */
private void checkEnumAlias(
    NodeTraversal t, JSDocInfo declInfo, Node value) {
  if (declInfo == null || !declInfo.hasEnumParameterType()) {
    return;
  }

  JSType valueType = getJSType(value);
  if (!valueType.isEnumType()) {
    return;
  }

  EnumType valueEnumType = valueType.toMaybeEnumType();
  JSType valueEnumPrimitiveType =
      valueEnumType.getElementsType().getPrimitiveType();
  validator.expectCanAssignTo(t, value, valueEnumPrimitiveType,
      declInfo.getEnumParameterType().evaluate(t.getScope(), typeRegistry),
      "incompatible enum element types");
}
 
Example 9
Source File: Closure_11_TypeCheck_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * <p>Checks enum aliases.
 *
 * <p>We verify that the enum element type of the enum used
 * for initialization is a subtype of the enum element type of
 * the enum the value is being copied in.</p>
 *
 * <p>Example:</p>
 * <pre>var myEnum = myOtherEnum;</pre>
 *
 * <p>Enum aliases are irregular, so we need special code for this :(</p>
 *
 * @param value the value used for initialization of the enum
 */
private void checkEnumAlias(
    NodeTraversal t, JSDocInfo declInfo, Node value) {
  if (declInfo == null || !declInfo.hasEnumParameterType()) {
    return;
  }

  JSType valueType = getJSType(value);
  if (!valueType.isEnumType()) {
    return;
  }

  EnumType valueEnumType = valueType.toMaybeEnumType();
  JSType valueEnumPrimitiveType =
      valueEnumType.getElementsType().getPrimitiveType();
  validator.expectCanAssignTo(t, value, valueEnumPrimitiveType,
      declInfo.getEnumParameterType().evaluate(t.getScope(), typeRegistry),
      "incompatible enum element types");
}
 
Example 10
Source File: Closure_2_TypeCheck_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * <p>Checks enum aliases.
 *
 * <p>We verify that the enum element type of the enum used
 * for initialization is a subtype of the enum element type of
 * the enum the value is being copied in.</p>
 *
 * <p>Example:</p>
 * <pre>var myEnum = myOtherEnum;</pre>
 *
 * <p>Enum aliases are irregular, so we need special code for this :(</p>
 *
 * @param value the value used for initialization of the enum
 */
private void checkEnumAlias(
    NodeTraversal t, JSDocInfo declInfo, Node value) {
  if (declInfo == null || !declInfo.hasEnumParameterType()) {
    return;
  }

  JSType valueType = getJSType(value);
  if (!valueType.isEnumType()) {
    return;
  }

  EnumType valueEnumType = valueType.toMaybeEnumType();
  JSType valueEnumPrimitiveType =
      valueEnumType.getElementsType().getPrimitiveType();
  validator.expectCanAssignTo(t, value, valueEnumPrimitiveType,
      declInfo.getEnumParameterType().evaluate(t.getScope(), typeRegistry),
      "incompatible enum element types");
}
 
Example 11
Source File: Closure_2_TypeCheck_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * <p>Checks enum aliases.
 *
 * <p>We verify that the enum element type of the enum used
 * for initialization is a subtype of the enum element type of
 * the enum the value is being copied in.</p>
 *
 * <p>Example:</p>
 * <pre>var myEnum = myOtherEnum;</pre>
 *
 * <p>Enum aliases are irregular, so we need special code for this :(</p>
 *
 * @param value the value used for initialization of the enum
 */
private void checkEnumAlias(
    NodeTraversal t, JSDocInfo declInfo, Node value) {
  if (declInfo == null || !declInfo.hasEnumParameterType()) {
    return;
  }

  JSType valueType = getJSType(value);
  if (!valueType.isEnumType()) {
    return;
  }

  EnumType valueEnumType = valueType.toMaybeEnumType();
  JSType valueEnumPrimitiveType =
      valueEnumType.getElementsType().getPrimitiveType();
  validator.expectCanAssignTo(t, value, valueEnumPrimitiveType,
      declInfo.getEnumParameterType().evaluate(t.getScope(), typeRegistry),
      "incompatible enum element types");
}
 
Example 12
Source File: DeclarationGenerator.java    From clutz with MIT License 5 votes vote down vote up
/**
 * A type representing an object literal that is inferred through goog.provide(...) or @const {}
 * and assigning properties to it.
 *
 * <p>We start with object types and narrow away known non-namespace types.
 *
 * <p>The terminology "namespace" type is non-standard.
 */
private static boolean isNamespaceType(JSType type) {
  if (!type.isObject()) return false;
  return !type.isInterface()
      && !type.isInstanceType()
      && !type.isEnumType()
      && !type.isFunctionType()
      && !isTypedef(type);
}
 
Example 13
Source File: Closure_6_TypeValidator_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Expect that the first type can be assigned to a symbol of the second
 * type.
 *
 * @param t The node traversal.
 * @param n The node to issue warnings on.
 * @param rightType The type on the RHS of the assign.
 * @param leftType The type of the symbol on the LHS of the assign.
 * @param owner The owner of the property being assigned to.
 * @param propName The name of the property being assigned to.
 * @return True if the types matched, false otherwise.
 */
boolean expectCanAssignToPropertyOf(NodeTraversal t, Node n, JSType rightType,
    JSType leftType, Node owner, String propName) {
  // The NoType check is a hack to make typedefs work OK.
  if (!leftType.isNoType() && !rightType.canAssignTo(leftType)) {
    if ((leftType.isConstructor() || leftType.isEnumType()) && (rightType.isConstructor() || rightType.isEnumType())) {
      registerMismatch(rightType, leftType, null);
    } else {
    // Do not type-check interface methods, because we expect that
    // they will have dummy implementations that do not match the type
    // annotations.
    JSType ownerType = getJSType(owner);
    if (ownerType.isFunctionPrototypeType()) {
      FunctionType ownerFn = ownerType.toObjectType().getOwnerFunction();
      if (ownerFn.isInterface() &&
          rightType.isFunctionType() && leftType.isFunctionType()) {
        return true;
      }
    }

    mismatch(t, n,
        "assignment to property " + propName + " of " +
        getReadableJSTypeName(owner, true),
        rightType, leftType);
    }
    return false;
  }
  return true;
}
 
Example 14
Source File: TypeCollectionPass.java    From js-dossier with Apache License 2.0 5 votes vote down vote up
public void crawl(JSType type) {
  type.visit(this);
  if ((type.isNominalType() && !type.isInstanceType())
      || type.isNominalConstructor()
      || type.isEnumType()) {
    externs.add(type);
  }
}
 
Example 15
Source File: DeclarationGenerator.java    From clutz with MIT License 5 votes vote down vote up
/**
 * Returns true if {@code propType} is creating a new type in the TypeScript sense - i.e. it's a
 * constructor function (class or interface), enum, or typedef.
 */
private boolean isDefiningType(JSType propType) {
  return isClassLike(propType)
      || propType.isEnumType()
      || propType.isInterface()
      || isTypedef(propType);
}
 
Example 16
Source File: DisambiguateProperties.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Override public boolean isTypeToSkip(JSType type) {
  return type.isEnumType() || (type.autoboxesTo() != null);
}
 
Example 17
Source File: Closure_118_DisambiguateProperties_t.java    From coming with MIT License 4 votes vote down vote up
@Override public boolean isTypeToSkip(JSType type) {
  return type.isEnumType() || (type.autoboxesTo() != null);
}
 
Example 18
Source File: TypeCollectionPass.java    From js-dossier with Apache License 2.0 4 votes vote down vote up
private void recordType(NominalType type) {
  if (externTypes.contains(type.getType()) && !type.getJsDoc().isTypedef()) {
    logfmt("Skipping extern alias: %s", type.getName());
    return;
  }

  if (type.getName().contains("$$")) {
    int index = type.getName().indexOf("$$");
    String id = type.getName().substring(index + 2);
    if (typeRegistry.isModule(id)) {
      Module module = typeRegistry.getModule(id);
      if (module.isEs6()) {
        for (AliasRegion region : typeRegistry.getAliasRegions(type.getSourceFile())) {
          if (region.getRange().contains(type.getSourcePosition())) {
            String alias = type.getName().substring(0, index);
            region.addAlias(alias, type.getName());
          }
        }
      }
      logfmt("Skipping module alias: %s", type.getName());
      return;
    }
  }

  JSType jsType = type.getType();
  if (!type.getModule().isPresent()
      && !jsType.isConstructor()
      && !jsType.isInterface()
      && !jsType.isEnumType()
      && !type.getJsDoc().isTypedef()
      && !type.getJsDoc().isDefine()
      && !typeRegistry.isProvided(type.getName())
      && !typeRegistry.isImplicitNamespace(type.getName())
      && symbolTable.getSlot(type.getName()) == null) {
    logfmt("Ignoring undeclared namespace %s", type.getName());
    return;
  }

  if (!typeRegistry.getTypes(type.getType()).isEmpty()) {
    logfmt(
        "Found type alias: %s = %s",
        type.getName(), typeRegistry.getTypes(type.getType()).iterator().next().getName());
  }
  typeRegistry.addType(type);
}
 
Example 19
Source File: Closure_103_DisambiguateProperties_t.java    From coming with MIT License 4 votes vote down vote up
@Override public boolean isTypeToSkip(JSType type) {
  return type.isEnumType() || (type.autoboxesTo() != null);
}
 
Example 20
Source File: Closure_103_DisambiguateProperties_s.java    From coming with MIT License 4 votes vote down vote up
@Override public boolean isTypeToSkip(JSType type) {
  return type.isEnumType() || (type.autoboxesTo() != null);
}