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

The following examples show how to use com.google.javascript.rhino.jstype.ObjectType#hasReferenceName() . 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: 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 2
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 3
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 4
Source File: Closure_103_DisambiguateProperties_s.java    From coming with MIT License 5 votes vote down vote up
@Override public boolean isInvalidatingType(JSType type) {
  if (type == null || invalidatingTypes.contains(type) ||
      (type.isNamedType() && type.isUnknownType())) {
    return true;
  }

  ObjectType objType = ObjectType.cast(type);
  return objType != null && !objType.hasReferenceName();
}
 
Example 5
Source File: Closure_103_DisambiguateProperties_t.java    From coming with MIT License 5 votes vote down vote up
@Override public boolean isInvalidatingType(JSType type) {
  if (type == null || invalidatingTypes.contains(type) ||
      (type.isNamedType() && type.isUnknownType())) {
    return true;
  }

  ObjectType objType = ObjectType.cast(type);
  return objType != null && !objType.hasReferenceName();
}
 
Example 6
Source File: DisambiguateProperties.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override public boolean isInvalidatingType(JSType type) {
  if (type == null || invalidatingTypes.contains(type) ||
      type.isUnknownType() /* unresolved types */) {
    return true;
  }

  ObjectType objType = ObjectType.cast(type);
  return objType != null && !objType.hasReferenceName();
}
 
Example 7
Source File: Closure_118_DisambiguateProperties_t.java    From coming with MIT License 5 votes vote down vote up
@Override public boolean isInvalidatingType(JSType type) {
  if (type == null || invalidatingTypes.contains(type) ||
      type.isUnknownType() /* unresolved types */) {
    return true;
  }

  ObjectType objType = ObjectType.cast(type);
  return objType != null && !objType.hasReferenceName();
}
 
Example 8
Source File: Closure_118_DisambiguateProperties_s.java    From coming with MIT License 5 votes vote down vote up
@Override public boolean isInvalidatingType(JSType type) {
  if (type == null || invalidatingTypes.contains(type) ||
      type.isUnknownType() /* unresolved types */) {
    return true;
  }

  ObjectType objType = ObjectType.cast(type);
  return objType != null && !objType.hasReferenceName();
}
 
Example 9
Source File: CheckAccessControls.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns if a property is declared constant.
 */
private static boolean isPropertyDeclaredConstant(
    ObjectType objectType, String prop) {
  for (;
       // Only objects with reference names can have constant properties.
       objectType != null && objectType.hasReferenceName();

       objectType = objectType.getImplicitPrototype()) {
    JSDocInfo docInfo = objectType.getOwnPropertyJSDocInfo(prop);
    if (docInfo != null && docInfo.isConstant()) {
      return true;
    }
  }
  return false;
}
 
Example 10
Source File: Closure_71_CheckAccessControls_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Determines whether the given property with @const tag got reassigned
 * @param t The current traversal.
 * @param getprop The getprop node.
 */
private void checkConstantProperty(NodeTraversal t,
    Node getprop) {
  // Check whether the property is modified
  Node parent = getprop.getParent();
  if (!(NodeUtil.isAssignmentOp(parent) && parent.getFirstChild() == getprop)
      && (parent.getType() != Token.INC) && (parent.getType() != Token.DEC)) {
    return;
  }

  ObjectType objectType =
    ObjectType.cast(dereference(getprop.getFirstChild().getJSType()));
  String propertyName = getprop.getLastChild().getString();

  // Check whether constant properties are reassigned
  if (objectType != null) {
    ObjectType oType = objectType;
    while (oType != null) {
      if (oType.hasReferenceName()) {
        if (initializedConstantProperties.containsEntry(
                oType.getReferenceName(), propertyName)) {
          compiler.report(
              t.makeError(getprop, CONST_PROPERTY_REASSIGNED_VALUE,
                  propertyName));
          break;
        }
      }
      oType = oType.getImplicitPrototype();
    }

    JSDocInfo info = objectType.getOwnPropertyJSDocInfo(propertyName);
    if (info != null && info.isConstant()
        && objectType.hasReferenceName()) {
      initializedConstantProperties.put(objectType.getReferenceName(),
          propertyName);
    }

    // Add the prototype when we're looking at an instance object
    if (objectType.isInstanceType()) {
      ObjectType prototype = objectType.getImplicitPrototype();
      if (prototype != null) {
        JSDocInfo prototypeInfo
          = prototype.getOwnPropertyJSDocInfo(propertyName);
        if (prototypeInfo != null && prototypeInfo.isConstant()
            && prototype.hasReferenceName()) {
          initializedConstantProperties.put(prototype.getReferenceName(),
              propertyName);
        }
      }
    }
  }
}
 
Example 11
Source File: TypeInference.java    From astor with GNU General Public License v2.0 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, or changed with
  // type casts.
  // See CodingConvention#getObjectLiteralCase and goog.object.reflect.
  // Ignore these types of literals.
  // TODO(nicksantos): There should be an "anonymous object" type that
  // we can check for here.
  ObjectType objectType = ObjectType.cast(type);
  if (objectType == null) {
    return scope;
  }

  boolean hasLendsName = n.getJSDocInfo() != null &&
      n.getJSDocInfo().getLendsName() != null;
  if (objectType.hasReferenceName() && !hasLendsName) {
    return scope;
  }

  String qObjName = NodeUtil.getBestLValueName(
      NodeUtil.getBestLValue(n));
  for (Node name = n.getFirstChild(); name != null;
       name = name.getNext()) {
    Node value = name.getFirstChild();
    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 12
Source File: CheckAccessControls.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Determines whether the given property with @const tag got reassigned
 * @param t The current traversal.
 * @param getprop The getprop node.
 */
private void checkConstantProperty(NodeTraversal t,
    Node getprop) {
  // Check whether the property is modified
  Node parent = getprop.getParent();
  boolean isDelete = parent.isDelProp();
  if (!(NodeUtil.isAssignmentOp(parent) && parent.getFirstChild() == getprop)
      && !parent.isInc() && !parent.isDec()
      && !isDelete) {
    return;
  }

  ObjectType objectType =
    ObjectType.cast(dereference(getprop.getFirstChild().getJSType()));
  String propertyName = getprop.getLastChild().getString();

  boolean isConstant = isPropertyDeclaredConstant(objectType, propertyName);

  // Check whether constant properties are reassigned
  if (isConstant) {
    if (isDelete) {
      compiler.report(
          t.makeError(getprop, CONST_PROPERTY_DELETED, propertyName));
      return;
    }

    ObjectType oType = objectType;
    while (oType != null) {
      if (oType.hasReferenceName()) {
        if (initializedConstantProperties.containsEntry(
                oType.getReferenceName(), propertyName)) {
          compiler.report(
              t.makeError(getprop, CONST_PROPERTY_REASSIGNED_VALUE,
                  propertyName));
          break;
        }
      }
      oType = oType.getImplicitPrototype();
    }

    Preconditions.checkState(objectType.hasReferenceName());
    initializedConstantProperties.put(objectType.getReferenceName(),
        propertyName);

    // Add the prototype when we're looking at an instance object
    if (objectType.isInstanceType()) {
      ObjectType prototype = objectType.getImplicitPrototype();
      if (prototype != null) {
        if (prototype.hasProperty(propertyName)
            && prototype.hasReferenceName()) {
          initializedConstantProperties.put(prototype.getReferenceName(),
              propertyName);
        }
      }
    }
  }
}
 
Example 13
Source File: Closure_71_CheckAccessControls_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Determines whether the given property with @const tag got reassigned
 * @param t The current traversal.
 * @param getprop The getprop node.
 */
private void checkConstantProperty(NodeTraversal t,
    Node getprop) {
  // Check whether the property is modified
  Node parent = getprop.getParent();
  if (!(NodeUtil.isAssignmentOp(parent) && parent.getFirstChild() == getprop)
      && (parent.getType() != Token.INC) && (parent.getType() != Token.DEC)) {
    return;
  }

  ObjectType objectType =
    ObjectType.cast(dereference(getprop.getFirstChild().getJSType()));
  String propertyName = getprop.getLastChild().getString();

  // Check whether constant properties are reassigned
  if (objectType != null) {
    ObjectType oType = objectType;
    while (oType != null) {
      if (oType.hasReferenceName()) {
        if (initializedConstantProperties.containsEntry(
                oType.getReferenceName(), propertyName)) {
          compiler.report(
              t.makeError(getprop, CONST_PROPERTY_REASSIGNED_VALUE,
                  propertyName));
          break;
        }
      }
      oType = oType.getImplicitPrototype();
    }

    JSDocInfo info = objectType.getOwnPropertyJSDocInfo(propertyName);
    if (info != null && info.isConstant()
        && objectType.hasReferenceName()) {
      initializedConstantProperties.put(objectType.getReferenceName(),
          propertyName);
    }

    // Add the prototype when we're looking at an instance object
    if (objectType.isInstanceType()) {
      ObjectType prototype = objectType.getImplicitPrototype();
      if (prototype != null) {
        JSDocInfo prototypeInfo
          = prototype.getOwnPropertyJSDocInfo(propertyName);
        if (prototypeInfo != null && prototypeInfo.isConstant()
            && prototype.hasReferenceName()) {
          initializedConstantProperties.put(prototype.getReferenceName(),
              propertyName);
        }
      }
    }
  }
}
 
Example 14
Source File: Closure_25_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, or changed with
  // type casts.
  // See CodingConvention#getObjectLiteralCase and goog.object.reflect.
  // Ignore these types of literals.
  // TODO(nicksantos): There should be an "anonymous object" type that
  // we can check for here.
  ObjectType objectType = ObjectType.cast(type);
  if (objectType == null) {
    return scope;
  }

  boolean hasLendsName = n.getJSDocInfo() != null &&
      n.getJSDocInfo().getLendsName() != null;
  if (objectType.hasReferenceName() && !hasLendsName) {
    return scope;
  }

  String qObjName = NodeUtil.getBestLValueName(
      NodeUtil.getBestLValue(n));
  for (Node name = n.getFirstChild(); name != null;
       name = name.getNext()) {
    Node value = name.getFirstChild();
    String memberName = NodeUtil.getObjectLitKeyName(name);
    if (memberName != null) {
      JSType rawValueType =  name.getFirstChild().getJSType();
      JSType valueType = NodeUtil.getObjectLitKeyTypeFromValueType(
          name, rawValueType);
      if (valueType == null) {
        valueType = getNativeType(UNKNOWN_TYPE);
      }
      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 ? getNativeType(UNKNOWN_TYPE) : oldType,
            valueType);
      }
    } else {
      n.setJSType(getNativeType(UNKNOWN_TYPE));
    }
  }
  return scope;
}
 
Example 15
Source File: Closure_35_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, or changed with
  // type casts.
  // See CodingConvention#getObjectLiteralCase and goog.object.reflect.
  // Ignore these types of literals.
  // TODO(nicksantos): There should be an "anonymous object" type that
  // we can check for here.
  ObjectType objectType = ObjectType.cast(type);
  if (objectType == null) {
    return scope;
  }

  boolean hasLendsName = n.getJSDocInfo() != null &&
      n.getJSDocInfo().getLendsName() != null;
  if (objectType.hasReferenceName() && !hasLendsName) {
    return scope;
  }

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

      // Do normal flow inference if this is a direct property assignment.
      if (qObjName != null && name.isString()) {
        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 ? getNativeType(UNKNOWN_TYPE) : oldType,
            valueType);
      }
    } else {
      n.setJSType(getNativeType(UNKNOWN_TYPE));
    }
  }
  return scope;
}
 
Example 16
Source File: Closure_35_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, or changed with
  // type casts.
  // See CodingConvention#getObjectLiteralCase and goog.object.reflect.
  // Ignore these types of literals.
  // TODO(nicksantos): There should be an "anonymous object" type that
  // we can check for here.
  ObjectType objectType = ObjectType.cast(type);
  if (objectType == null) {
    return scope;
  }

  boolean hasLendsName = n.getJSDocInfo() != null &&
      n.getJSDocInfo().getLendsName() != null;
  if (objectType.hasReferenceName() && !hasLendsName) {
    return scope;
  }

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

      // Do normal flow inference if this is a direct property assignment.
      if (qObjName != null && name.isString()) {
        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 ? getNativeType(UNKNOWN_TYPE) : oldType,
            valueType);
      }
    } else {
      n.setJSType(getNativeType(UNKNOWN_TYPE));
    }
  }
  return scope;
}
 
Example 17
Source File: Closure_25_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, or changed with
  // type casts.
  // See CodingConvention#getObjectLiteralCase and goog.object.reflect.
  // Ignore these types of literals.
  // TODO(nicksantos): There should be an "anonymous object" type that
  // we can check for here.
  ObjectType objectType = ObjectType.cast(type);
  if (objectType == null) {
    return scope;
  }

  boolean hasLendsName = n.getJSDocInfo() != null &&
      n.getJSDocInfo().getLendsName() != null;
  if (objectType.hasReferenceName() && !hasLendsName) {
    return scope;
  }

  String qObjName = NodeUtil.getBestLValueName(
      NodeUtil.getBestLValue(n));
  for (Node name = n.getFirstChild(); name != null;
       name = name.getNext()) {
    Node value = name.getFirstChild();
    String memberName = NodeUtil.getObjectLitKeyName(name);
    if (memberName != null) {
      JSType rawValueType =  name.getFirstChild().getJSType();
      JSType valueType = NodeUtil.getObjectLitKeyTypeFromValueType(
          name, rawValueType);
      if (valueType == null) {
        valueType = getNativeType(UNKNOWN_TYPE);
      }
      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 ? getNativeType(UNKNOWN_TYPE) : oldType,
            valueType);
      }
    } else {
      n.setJSType(getNativeType(UNKNOWN_TYPE));
    }
  }
  return scope;
}