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

The following examples show how to use com.google.javascript.rhino.jstype.ObjectType#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: InlineProperties.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** Returns true if properties on this type should not be renamed. */
private boolean isInvalidatingType(JSType type) {
  if (type.isUnionType()) {
    type = type.restrictByNotNullOrUndefined();
    if (type.isUnionType()) {
      for (JSType alt : type.toMaybeUnionType().getAlternates()) {
        if (isInvalidatingType(alt)) {
          return true;
        }
      }
      return false;
    }
  }
  ObjectType objType = ObjectType.cast(type);
  return objType == null
      || invalidatingTypes.contains(objType)
      || !objType.hasReferenceName()
      || objType.isUnknownType()
      || objType.isEmptyType() /* unresolved types */
      || objType.isEnumType()
      || objType.autoboxesTo() != null;
}
 
Example 2
Source File: InferJSDocInfo.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Handle cases #1 and #3 in the class doc.
 */
private void attachJSDocInfoToNominalTypeOrShape(
    ObjectType objType, JSDocInfo docInfo, @Nullable String qName) {
  if (objType.isConstructor() ||
      objType.isEnumType() ||
      objType.isInterface()) {
    // Named types.
    if (objType.hasReferenceName() &&
        objType.getReferenceName().equals(qName)) {
      objType.setJSDocInfo(docInfo);

      if (objType.isConstructor() || objType.isInterface()) {
        JSType.toMaybeFunctionType(objType).getInstanceType().setJSDocInfo(
            docInfo);
      } else if (objType instanceof EnumType) {
        ((EnumType) objType).getElementsType().setJSDocInfo(docInfo);
      }
    }
  } else if (!objType.isNativeObjectType() &&
      objType.isFunctionType()) {
    // Structural functions.
    objType.setJSDocInfo(docInfo);
  }
}
 
Example 3
Source File: AmbiguateProperties.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** Returns true if properties on this type should not be renamed. */
private boolean isInvalidatingType(JSType type) {
  if (type.isUnionType()) {
    type = type.restrictByNotNullOrUndefined();
    if (type.isUnionType()) {
      for (JSType alt : type.toMaybeUnionType().getAlternates()) {
        if (isInvalidatingType(alt)) {
          return true;
        }
      }
      return false;
    }
  }
  ObjectType objType = ObjectType.cast(type);
  return objType == null
      || invalidatingTypes.contains(objType)
      || !objType.hasReferenceName()
      || objType.isUnknownType()
      || objType.isEmptyType() /* unresolved types */
      || objType.isEnumType()
      || objType.autoboxesTo() != null;
}
 
Example 4
Source File: Nopol2017_0027_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Process an object literal and all the types on it.
 * @param objLit The OBJECTLIT node.
 * @param objLitType The type of the OBJECTLIT node. This might be a named
 *     type, because of the lends annotation.
 * @param declareOnOwner If true, declare properties on the objLitType as
 *     well. If false, the caller should take care of this.
 */
void processObjectLitProperties(
    Node objLit, ObjectType objLitType,
    boolean declareOnOwner) {
  for (Node keyNode = objLit.getFirstChild(); keyNode != null;
       keyNode = keyNode.getNext()) {
    Node value = keyNode.getFirstChild();
    String memberName = NodeUtil.getObjectLitKeyName(keyNode);
    JSDocInfo info = keyNode.getJSDocInfo();
    JSType valueType =
        getDeclaredType(keyNode.getSourceFileName(), info, keyNode, value);
    JSType keyType =  objLitType.isEnumType() ?
        objLitType.toMaybeEnumType().getElementsType() :
        NodeUtil.getObjectLitKeyTypeFromValueType(keyNode, valueType);

    // Try to declare this property in the current scope if it
    // has an authoritative name.
    String qualifiedName = NodeUtil.getBestLValueName(keyNode);
    if (qualifiedName != null) {
      boolean inferred = keyType == null;
      defineSlot(keyNode, objLit, qualifiedName, keyType, inferred);
    } else if (keyType != null) {
      setDeferredType(keyNode, keyType);
    }

    if (keyType != null && objLitType != null && declareOnOwner) {
      // Declare this property on its object literal.
      boolean isExtern = keyNode.isFromExterns();
      objLitType.defineDeclaredProperty(memberName, keyType, keyNode);
    }
  }
}
 
Example 5
Source File: Nopol2017_0027_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Process an object literal and all the types on it.
 * @param objLit The OBJECTLIT node.
 * @param objLitType The type of the OBJECTLIT node. This might be a named
 *     type, because of the lends annotation.
 * @param declareOnOwner If true, declare properties on the objLitType as
 *     well. If false, the caller should take care of this.
 */
void processObjectLitProperties(
    Node objLit, ObjectType objLitType,
    boolean declareOnOwner) {
  for (Node keyNode = objLit.getFirstChild(); keyNode != null;
       keyNode = keyNode.getNext()) {
    Node value = keyNode.getFirstChild();
    String memberName = NodeUtil.getObjectLitKeyName(keyNode);
    JSDocInfo info = keyNode.getJSDocInfo();
    JSType valueType =
        getDeclaredType(keyNode.getSourceFileName(), info, keyNode, value);
    JSType keyType =  objLitType.isEnumType() ?
        objLitType.toMaybeEnumType().getElementsType() :
        NodeUtil.getObjectLitKeyTypeFromValueType(keyNode, valueType);

    // Try to declare this property in the current scope if it
    // has an authoritative name.
    String qualifiedName = NodeUtil.getBestLValueName(keyNode);
    if (qualifiedName != null) {
      boolean inferred = keyType == null;
      defineSlot(keyNode, objLit, qualifiedName, keyType, inferred);
    } else if (keyType != null) {
      setDeferredType(keyNode, keyType);
    }

    if (keyType != null && objLitType != null && declareOnOwner) {
      // Declare this property on its object literal.
      boolean isExtern = keyNode.isFromExterns();
      objLitType.defineDeclaredProperty(memberName, keyType, keyNode);
    }
  }
}
 
Example 6
Source File: Closure_17_TypedScopeCreator_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Process an object literal and all the types on it.
 * @param objLit The OBJECTLIT node.
 * @param objLitType The type of the OBJECTLIT node. This might be a named
 *     type, because of the lends annotation.
 * @param declareOnOwner If true, declare properties on the objLitType as
 *     well. If false, the caller should take care of this.
 */
void processObjectLitProperties(
    Node objLit, ObjectType objLitType,
    boolean declareOnOwner) {
  for (Node keyNode = objLit.getFirstChild(); keyNode != null;
       keyNode = keyNode.getNext()) {
    Node value = keyNode.getFirstChild();
    String memberName = NodeUtil.getObjectLitKeyName(keyNode);
    JSDocInfo info = keyNode.getJSDocInfo();
    JSType valueType =
        getDeclaredType(keyNode.getSourceFileName(), info, keyNode, value);
    JSType keyType =  objLitType.isEnumType() ?
        objLitType.toMaybeEnumType().getElementsType() :
        NodeUtil.getObjectLitKeyTypeFromValueType(keyNode, valueType);

    // Try to declare this property in the current scope if it
    // has an authoritative name.
    String qualifiedName = NodeUtil.getBestLValueName(keyNode);
    if (qualifiedName != null) {
      boolean inferred = keyType == null;
      defineSlot(keyNode, objLit, qualifiedName, keyType, inferred);
    } else if (keyType != null) {
      setDeferredType(keyNode, keyType);
    }

    if (keyType != null && objLitType != null && declareOnOwner) {
      // Declare this property on its object literal.
      boolean isExtern = keyNode.isFromExterns();
      objLitType.defineDeclaredProperty(memberName, keyType, keyNode);
    }
  }
}
 
Example 7
Source File: Closure_17_TypedScopeCreator_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Process an object literal and all the types on it.
 * @param objLit The OBJECTLIT node.
 * @param objLitType The type of the OBJECTLIT node. This might be a named
 *     type, because of the lends annotation.
 * @param declareOnOwner If true, declare properties on the objLitType as
 *     well. If false, the caller should take care of this.
 */
void processObjectLitProperties(
    Node objLit, ObjectType objLitType,
    boolean declareOnOwner) {
  for (Node keyNode = objLit.getFirstChild(); keyNode != null;
       keyNode = keyNode.getNext()) {
    Node value = keyNode.getFirstChild();
    String memberName = NodeUtil.getObjectLitKeyName(keyNode);
    JSDocInfo info = keyNode.getJSDocInfo();
    JSType valueType =
        getDeclaredType(keyNode.getSourceFileName(), info, keyNode, value);
    JSType keyType =  objLitType.isEnumType() ?
        objLitType.toMaybeEnumType().getElementsType() :
        NodeUtil.getObjectLitKeyTypeFromValueType(keyNode, valueType);

    // Try to declare this property in the current scope if it
    // has an authoritative name.
    String qualifiedName = NodeUtil.getBestLValueName(keyNode);
    if (qualifiedName != null) {
      boolean inferred = keyType == null;
      defineSlot(keyNode, objLit, qualifiedName, keyType, inferred);
    } else if (keyType != null) {
      setDeferredType(keyNode, keyType);
    }

    if (keyType != null && objLitType != null && declareOnOwner) {
      // Declare this property on its object literal.
      boolean isExtern = keyNode.isFromExterns();
      objLitType.defineDeclaredProperty(memberName, keyType, keyNode);
    }
  }
}
 
Example 8
Source File: Closure_48_TypedScopeCreator_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Process an object literal and all the types on it.
 * @param objLit The OBJECTLIT node.
 * @param objLitType The type of the OBJECTLIT node. This might be a named
 *     type, because of the lends annotation.
 * @param declareOnOwner If true, declare properties on the objLitType as
 *     well. If false, the caller should take crae of this.
 */
void processObjectLitProperties(
    NodeTraversal t, Node objLit, ObjectType objLitType,
    boolean declareOnOwner) {
  for (Node keyNode = objLit.getFirstChild(); keyNode != null;
       keyNode = keyNode.getNext()) {
    Node value = keyNode.getFirstChild();
    String memberName = NodeUtil.getObjectLitKeyName(keyNode);
    JSDocInfo info = keyNode.getJSDocInfo();
    JSType valueType =
        getDeclaredType(t.getSourceName(), info, keyNode, value);
    JSType keyType =  objLitType.isEnumType() ?
        objLitType.toMaybeEnumType().getElementsType() :
        NodeUtil.getObjectLitKeyTypeFromValueType(keyNode, valueType);
    if (keyType != null) {
      // Try to declare this property in the current scope if it
      // has an authoritative name.
      String qualifiedName = NodeUtil.getBestLValueName(keyNode);
      if (qualifiedName != null) {
        defineSlot(keyNode, objLit, qualifiedName, keyType, false);
      } else {
        setDeferredType(keyNode, keyType);
      }

      if (objLitType != null && declareOnOwner) {
        // Declare this property on its object literal.
        boolean isExtern = t.getInput() != null && t.getInput().isExtern();
        objLitType.defineDeclaredProperty(memberName, keyType, keyNode);
      }
    }
  }
}
 
Example 9
Source File: Closure_48_TypedScopeCreator_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Process an object literal and all the types on it.
 * @param objLit The OBJECTLIT node.
 * @param objLitType The type of the OBJECTLIT node. This might be a named
 *     type, because of the lends annotation.
 * @param declareOnOwner If true, declare properties on the objLitType as
 *     well. If false, the caller should take crae of this.
 */
void processObjectLitProperties(
    NodeTraversal t, Node objLit, ObjectType objLitType,
    boolean declareOnOwner) {
  for (Node keyNode = objLit.getFirstChild(); keyNode != null;
       keyNode = keyNode.getNext()) {
    Node value = keyNode.getFirstChild();
    String memberName = NodeUtil.getObjectLitKeyName(keyNode);
    JSDocInfo info = keyNode.getJSDocInfo();
    JSType valueType =
        getDeclaredType(t.getSourceName(), info, keyNode, value);
    JSType keyType =  objLitType.isEnumType() ?
        objLitType.toMaybeEnumType().getElementsType() :
        NodeUtil.getObjectLitKeyTypeFromValueType(keyNode, valueType);
    if (keyType != null) {
      // Try to declare this property in the current scope if it
      // has an authoritative name.
      String qualifiedName = NodeUtil.getBestLValueName(keyNode);
      if (qualifiedName != null) {
        defineSlot(keyNode, objLit, qualifiedName, keyType, false);
      } else {
        setDeferredType(keyNode, keyType);
      }

      if (objLitType != null && declareOnOwner) {
        // Declare this property on its object literal.
        boolean isExtern = t.getInput() != null && t.getInput().isExtern();
        objLitType.defineDeclaredProperty(memberName, keyType, keyNode);
      }
    }
  }
}
 
Example 10
Source File: Closure_43_TypedScopeCreator_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Process an object literal and all the types on it.
 * @param objLit The OBJECTLIT node.
 * @param objLitType The type of the OBJECTLIT node. This might be a named
 *     type, because of the lends annotation.
 * @param declareOnOwner If true, declare properties on the objLitType as
 *     well. If false, the caller should take crae of this.
 */
void processObjectLitProperties(
    Node objLit, ObjectType objLitType,
    boolean declareOnOwner) {
  for (Node keyNode = objLit.getFirstChild(); keyNode != null;
       keyNode = keyNode.getNext()) {
    Node value = keyNode.getFirstChild();
    String memberName = NodeUtil.getObjectLitKeyName(keyNode);
    JSDocInfo info = keyNode.getJSDocInfo();
    JSType valueType =
        getDeclaredType(keyNode.getSourceFileName(), info, keyNode, value);
    JSType keyType =  objLitType.isEnumType() ?
        objLitType.toMaybeEnumType().getElementsType() :
        NodeUtil.getObjectLitKeyTypeFromValueType(keyNode, valueType);

    // Try to declare this property in the current scope if it
    // has an authoritative name.
    String qualifiedName = NodeUtil.getBestLValueName(keyNode);
    if (qualifiedName != null) {
      boolean inferred = keyType == null;
      defineSlot(keyNode, objLit, qualifiedName, keyType, inferred);
    } else if (keyType != null) {
      setDeferredType(keyNode, keyType);
    }

    if (keyType != null && objLitType != null && declareOnOwner) {
      // Declare this property on its object literal.
      boolean isExtern = keyNode.isFromExterns();
      objLitType.defineDeclaredProperty(memberName, keyType, keyNode);
    }
  }
}
 
Example 11
Source File: Closure_43_TypedScopeCreator_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Process an object literal and all the types on it.
 * @param objLit The OBJECTLIT node.
 * @param objLitType The type of the OBJECTLIT node. This might be a named
 *     type, because of the lends annotation.
 * @param declareOnOwner If true, declare properties on the objLitType as
 *     well. If false, the caller should take crae of this.
 */
void processObjectLitProperties(
    Node objLit, ObjectType objLitType,
    boolean declareOnOwner) {
  for (Node keyNode = objLit.getFirstChild(); keyNode != null;
       keyNode = keyNode.getNext()) {
    Node value = keyNode.getFirstChild();
    String memberName = NodeUtil.getObjectLitKeyName(keyNode);
    JSDocInfo info = keyNode.getJSDocInfo();
    JSType valueType =
        getDeclaredType(keyNode.getSourceFileName(), info, keyNode, value);
    JSType keyType =  objLitType.isEnumType() ?
        objLitType.toMaybeEnumType().getElementsType() :
        NodeUtil.getObjectLitKeyTypeFromValueType(keyNode, valueType);

    // Try to declare this property in the current scope if it
    // has an authoritative name.
    String qualifiedName = NodeUtil.getBestLValueName(keyNode);
    if (qualifiedName != null) {
      boolean inferred = keyType == null;
      defineSlot(keyNode, objLit, qualifiedName, keyType, inferred);
    } else if (keyType != null) {
      setDeferredType(keyNode, keyType);
    }

    if (keyType != null && objLitType != null && declareOnOwner) {
      // Declare this property on its object literal.
      boolean isExtern = keyNode.isFromExterns();
      objLitType.defineDeclaredProperty(memberName, keyType, keyNode);
    }
  }
}
 
Example 12
Source File: TypedScopeCreator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Process an object literal and all the types on it.
 * @param objLit The OBJECTLIT node.
 * @param objLitType The type of the OBJECTLIT node. This might be a named
 *     type, because of the lends annotation.
 * @param declareOnOwner If true, declare properties on the objLitType as
 *     well. If false, the caller should take care of this.
 */
void processObjectLitProperties(
    Node objLit, ObjectType objLitType,
    boolean declareOnOwner) {
  for (Node keyNode = objLit.getFirstChild(); keyNode != null;
       keyNode = keyNode.getNext()) {
    Node value = keyNode.getFirstChild();
    String memberName = NodeUtil.getObjectLitKeyName(keyNode);
    JSDocInfo info = keyNode.getJSDocInfo();
    JSType valueType =
        getDeclaredType(keyNode.getSourceFileName(), info, keyNode, value);
    JSType keyType =  objLitType.isEnumType() ?
        objLitType.toMaybeEnumType().getElementsType() :
        NodeUtil.getObjectLitKeyTypeFromValueType(keyNode, valueType);

    // Try to declare this property in the current scope if it
    // has an authoritative name.
    String qualifiedName = NodeUtil.getBestLValueName(keyNode);
    if (qualifiedName != null) {
      boolean inferred = keyType == null;
      defineSlot(keyNode, objLit, qualifiedName, keyType, inferred);
    } else if (keyType != null) {
      setDeferredType(keyNode, keyType);
    }

    if (keyType != null && objLitType != null && declareOnOwner) {
      // Declare this property on its object literal.
      boolean isExtern = keyNode.isFromExterns();
      objLitType.defineDeclaredProperty(memberName, keyType, keyNode);
    }
  }
}
 
Example 13
Source File: SymbolTable.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private boolean needsPropertyScope(Symbol sym) {
  ObjectType type = ObjectType.cast(sym.getType());
  if (type == null) {
    return false;
  }

  // Anonymous objects
  if (type.getReferenceName() == null) {
    return true;
  }

  // Constructors/prototypes
  // Should this check for
  // (type.isNominalConstructor() || type.isFunctionPrototypeType())
  // ?
  if (sym.getName().equals(type.getReferenceName())) {
    return true;
  }

  // Enums
  if (type.isEnumType() &&
      sym.getName().equals(
          type.toMaybeEnumType().getElementsType().getReferenceName())) {
    return true;
  }

  return false;
}
 
Example 14
Source File: Closure_112_TypeInference_t.java    From coming with MIT License 4 votes vote down vote up
private FlowScope traverseObjectLiteral(Node n, FlowScope scope) {
  JSType type = n.getJSType();
  Preconditions.checkNotNull(type);

  for (Node name = n.getFirstChild(); name != null; name = name.getNext()) {
    scope = traverse(name.getFirstChild(), scope);
  }

  // Object literals can be reflected on other types.
  // See CodingConvention#getObjectLiteralCast and goog.reflect.object
  // Ignore these types of literals.
  ObjectType objectType = ObjectType.cast(type);
  if (objectType == null
      || n.getBooleanProp(Node.REFLECTED_OBJECT)
      || objectType.isEnumType()) {
    return scope;
  }

  String qObjName = NodeUtil.getBestLValueName(
      NodeUtil.getBestLValue(n));
  for (Node name = n.getFirstChild(); name != null;
       name = name.getNext()) {
    String memberName = NodeUtil.getObjectLitKeyName(name);
    if (memberName != null) {
      JSType rawValueType =  name.getFirstChild().getJSType();
      JSType valueType = NodeUtil.getObjectLitKeyTypeFromValueType(
          name, rawValueType);
      if (valueType == null) {
        valueType = unknownType;
      }
      objectType.defineInferredProperty(memberName, valueType, name);

      // Do normal flow inference if this is a direct property assignment.
      if (qObjName != null && name.isStringKey()) {
        String qKeyName = qObjName + "." + memberName;
        Var var = syntacticScope.getVar(qKeyName);
        JSType oldType = var == null ? null : var.getType();
        if (var != null && var.isTypeInferred()) {
          var.setType(oldType == null ?
              valueType : oldType.getLeastSupertype(oldType));
        }

        scope.inferQualifiedSlot(name, qKeyName,
            oldType == null ? unknownType : oldType,
            valueType);
      }
    } else {
      n.setJSType(unknownType);
    }
  }
  return scope;
}
 
Example 15
Source File: Closure_112_TypeInference_s.java    From coming with MIT License 4 votes vote down vote up
private FlowScope traverseObjectLiteral(Node n, FlowScope scope) {
  JSType type = n.getJSType();
  Preconditions.checkNotNull(type);

  for (Node name = n.getFirstChild(); name != null; name = name.getNext()) {
    scope = traverse(name.getFirstChild(), scope);
  }

  // Object literals can be reflected on other types.
  // See CodingConvention#getObjectLiteralCast and goog.reflect.object
  // Ignore these types of literals.
  ObjectType objectType = ObjectType.cast(type);
  if (objectType == null
      || n.getBooleanProp(Node.REFLECTED_OBJECT)
      || objectType.isEnumType()) {
    return scope;
  }

  String qObjName = NodeUtil.getBestLValueName(
      NodeUtil.getBestLValue(n));
  for (Node name = n.getFirstChild(); name != null;
       name = name.getNext()) {
    String memberName = NodeUtil.getObjectLitKeyName(name);
    if (memberName != null) {
      JSType rawValueType =  name.getFirstChild().getJSType();
      JSType valueType = NodeUtil.getObjectLitKeyTypeFromValueType(
          name, rawValueType);
      if (valueType == null) {
        valueType = unknownType;
      }
      objectType.defineInferredProperty(memberName, valueType, name);

      // Do normal flow inference if this is a direct property assignment.
      if (qObjName != null && name.isStringKey()) {
        String qKeyName = qObjName + "." + memberName;
        Var var = syntacticScope.getVar(qKeyName);
        JSType oldType = var == null ? null : var.getType();
        if (var != null && var.isTypeInferred()) {
          var.setType(oldType == null ?
              valueType : oldType.getLeastSupertype(oldType));
        }

        scope.inferQualifiedSlot(name, qKeyName,
            oldType == null ? unknownType : oldType,
            valueType);
      }
    } else {
      n.setJSType(unknownType);
    }
  }
  return scope;
}