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

The following examples show how to use com.google.javascript.rhino.jstype.JSType#dereference() . 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: Nopol2017_0051_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Make sure that the access of this property is ok.
 */
private void checkPropertyAccess(JSType childType, String propName,
    NodeTraversal t, Node n) {
  ObjectType objectType = childType.dereference();
  if (objectType != null) {
    JSType propType = getJSType(n);
    if ((!objectType.hasProperty(propName) ||
         objectType.equals(typeRegistry.getNativeType(UNKNOWN_TYPE))) &&
        propType.equals(typeRegistry.getNativeType(UNKNOWN_TYPE))) {
      if (objectType instanceof EnumType) {
        report(t, n, INEXISTENT_ENUM_ELEMENT, propName);
      } else if (!objectType.isEmptyType() &&
          reportMissingProperties && !isPropertyTest(n)) {
        if (!typeRegistry.canPropertyBeDefined(objectType, propName)) {
          report(t, n, INEXISTENT_PROPERTY, propName,
              validator.getReadableJSTypeName(n.getFirstChild(), true));
        }
      }
    }
  } else {
    // TODO(nicksantos): might want to flag the access on a non object when
    // it's impossible to get a property from this type.
  }
}
 
Example 2
Source File: Nopol2017_0051_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Make sure that the access of this property is ok.
 */
private void checkPropertyAccess(JSType childType, String propName,
    NodeTraversal t, Node n) {
  ObjectType objectType = childType.dereference();
  if (objectType != null) {
    JSType propType = getJSType(n);
    if ((!objectType.hasProperty(propName) ||
         objectType.equals(typeRegistry.getNativeType(UNKNOWN_TYPE))) &&
        propType.equals(typeRegistry.getNativeType(UNKNOWN_TYPE))) {
      if (objectType instanceof EnumType) {
        report(t, n, INEXISTENT_ENUM_ELEMENT, propName);
      } else if (!objectType.isEmptyType() &&
          reportMissingProperties && !isPropertyTest(n)) {
        if (!typeRegistry.canPropertyBeDefined(objectType, propName)) {
          report(t, n, INEXISTENT_PROPERTY, propName,
              validator.getReadableJSTypeName(n.getFirstChild(), true));
        }
      }
    }
  } else {
    // TODO(nicksantos): might want to flag the access on a non object when
    // it's impossible to get a property from this type.
  }
}
 
Example 3
Source File: Closure_96_TypeCheck_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Make sure that the access of this property is ok.
 */
private void checkPropertyAccess(JSType childType, String propName,
    NodeTraversal t, Node n) {
  ObjectType objectType = childType.dereference();
  if (objectType != null) {
    JSType propType = getJSType(n);
    if ((!objectType.hasProperty(propName) ||
         objectType.equals(typeRegistry.getNativeType(UNKNOWN_TYPE))) &&
        propType.equals(typeRegistry.getNativeType(UNKNOWN_TYPE))) {
      if (objectType instanceof EnumType) {
        report(t, n, INEXISTENT_ENUM_ELEMENT, propName);
      } else if (!objectType.isEmptyType() &&
          reportMissingProperties && !isPropertyTest(n)) {
        if (!typeRegistry.canPropertyBeDefined(objectType, propName)) {
          report(t, n, INEXISTENT_PROPERTY, propName,
              validator.getReadableJSTypeName(n.getFirstChild(), true));
        }
      }
    }
  } else {
    // TODO(nicksantos): might want to flag the access on a non object when
    // it's impossible to get a property from this type.
  }
}
 
Example 4
Source File: Closure_66_TypeCheck_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Make sure that the access of this property is ok.
 */
private void checkPropertyAccess(JSType childType, String propName,
    NodeTraversal t, Node n) {
  ObjectType objectType = childType.dereference();
  if (objectType != null) {
    JSType propType = getJSType(n);
    if ((!objectType.hasProperty(propName) ||
         objectType.equals(typeRegistry.getNativeType(UNKNOWN_TYPE))) &&
        propType.equals(typeRegistry.getNativeType(UNKNOWN_TYPE))) {
      if (objectType instanceof EnumType) {
        report(t, n, INEXISTENT_ENUM_ELEMENT, propName);
      } else if (!objectType.isEmptyType() &&
          reportMissingProperties && !isPropertyTest(n)) {
        if (!typeRegistry.canPropertyBeDefined(objectType, propName)) {
          report(t, n, INEXISTENT_PROPERTY, propName,
              validator.getReadableJSTypeName(n.getFirstChild(), true));
        }
      }
    }
  } else {
    // TODO(nicksantos): might want to flag the access on a non object when
    // it's impossible to get a property from this type.
  }
}
 
Example 5
Source File: Closure_66_TypeCheck_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Make sure that the access of this property is ok.
 */
private void checkPropertyAccess(JSType childType, String propName,
    NodeTraversal t, Node n) {
  ObjectType objectType = childType.dereference();
  if (objectType != null) {
    JSType propType = getJSType(n);
    if ((!objectType.hasProperty(propName) ||
         objectType.equals(typeRegistry.getNativeType(UNKNOWN_TYPE))) &&
        propType.equals(typeRegistry.getNativeType(UNKNOWN_TYPE))) {
      if (objectType instanceof EnumType) {
        report(t, n, INEXISTENT_ENUM_ELEMENT, propName);
      } else if (!objectType.isEmptyType() &&
          reportMissingProperties && !isPropertyTest(n)) {
        if (!typeRegistry.canPropertyBeDefined(objectType, propName)) {
          report(t, n, INEXISTENT_PROPERTY, propName,
              validator.getReadableJSTypeName(n.getFirstChild(), true));
        }
      }
    }
  } else {
    // TODO(nicksantos): might want to flag the access on a non object when
    // it's impossible to get a property from this type.
  }
}
 
Example 6
Source File: Closure_117_TypeValidator_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Expect that the first type can be addressed with GETELEM syntax,
 * and that the second type is the right type for an index into the
 * first type.
 *
 * @param t The node traversal.
 * @param n The GETELEM node to issue warnings on.
 * @param objType The type of the left side of the GETELEM.
 * @param indexType The type inside the brackets of the GETELEM.
 */
void expectIndexMatch(NodeTraversal t, Node n, JSType objType,
                      JSType indexType) {
  Preconditions.checkState(n.isGetElem());
  Node indexNode = n.getLastChild();
  if (objType.isStruct()) {
    report(JSError.make(t.getSourceName(), indexNode,
                        ILLEGAL_PROPERTY_ACCESS, "'[]'", "struct"));
  }
  if (objType.isUnknownType()) {
    expectStringOrNumber(t, indexNode, indexType, "property access");
  } else {
    ObjectType dereferenced = objType.dereference();
    if (dereferenced != null && dereferenced
        .getTemplateTypeMap()
        .hasTemplateKey(typeRegistry.getObjectIndexKey())) {
      expectCanAssignTo(t, indexNode, indexType, dereferenced
          .getTemplateTypeMap().getTemplateType(typeRegistry.getObjectIndexKey()),
          "restricted index type");
    } else if (dereferenced != null && dereferenced.isArrayType()) {
      expectNumber(t, indexNode, indexType, "array access");
    } else if (objType.matchesObjectContext()) {
      expectString(t, indexNode, indexType, "property access");
    } else {
      mismatch(t, n, "only arrays or objects can be accessed",
          objType,
          typeRegistry.createUnionType(ARRAY_TYPE, OBJECT_TYPE));
    }
  }
}
 
Example 7
Source File: Closure_54_TypedScopeCreator_t.java    From coming with MIT License 4 votes vote down vote up
private FunctionType getFunctionType(@Nullable Var v) {
  JSType t = v == null ? null : v.getType();
  ObjectType o = t == null ? null : t.dereference();
  return JSType.toMaybeFunctionType(o);
}
 
Example 8
Source File: Closure_54_TypedScopeCreator_s.java    From coming with MIT License 4 votes vote down vote up
private FunctionType getFunctionType(@Nullable Var v) {
  JSType t = v == null ? null : v.getType();
  ObjectType o = t == null ? null : t.dereference();
  return JSType.toMaybeFunctionType(o);
}
 
Example 9
Source File: Closure_117_TypeValidator_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Given a node, get a human-readable name for the type of that node so
 * that will be easy for the programmer to find the original declaration.
 *
 * For example, if SubFoo's property "bar" might have the human-readable
 * name "Foo.prototype.bar".
 *
 * @param n The node.
 * @param dereference If true, the type of the node will be dereferenced
 *     to an Object type, if possible.
 */
String getReadableJSTypeName(Node n, boolean dereference) {

  // The best type name is the actual type name.

  // If we're analyzing a GETPROP, the property may be inherited by the
  // prototype chain. So climb the prototype chain and find out where
  // the property was originally defined.
  if (n.isGetProp()) {
    ObjectType objectType = getJSType(n.getFirstChild()).dereference();
    if (objectType != null) {
      String propName = n.getLastChild().getString();
      if (objectType.getConstructor() != null &&
          objectType.getConstructor().isInterface()) {
        objectType = FunctionType.getTopDefiningInterface(
            objectType, propName);
      } else {
        // classes
        while (objectType != null && !objectType.hasOwnProperty(propName)) {
          objectType = objectType.getImplicitPrototype();
        }
      }

      // Don't show complex function names or anonymous types.
      // Instead, try to get a human-readable type name.
      if (objectType != null &&
          (objectType.getConstructor() != null ||
           objectType.isFunctionPrototypeType())) {
        return objectType.toString() + "." + propName;
      }
    }
  }

  JSType type = getJSType(n);
  if (dereference) {
    ObjectType dereferenced = type.dereference();
    if (dereferenced != null) {
      type = dereferenced;
    }
  }
  if (type.isFunctionPrototypeType() ||
      (type.toObjectType() != null &&
       type.toObjectType().getConstructor() != null)) {
    return type.toString();
  }
  String qualifiedName = n.getQualifiedName();
  if (qualifiedName != null) {
    return qualifiedName;
  } else if (type.isFunctionType()) {
    // Don't show complex function names.
    return "function";
  } else {
    return type.toString();
  }
}
 
Example 10
Source File: CheckAccessControls.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Dereference a type, autoboxing it and filtering out null.
 */
private static JSType dereference(JSType type) {
  return type == null ? null : type.dereference();
}
 
Example 11
Source File: TypedScopeCreator.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
private FunctionType getFunctionType(@Nullable Var v) {
  JSType t = v == null ? null : v.getType();
  ObjectType o = t == null ? null : t.dereference();
  return JSType.toMaybeFunctionType(o);
}
 
Example 12
Source File: Closure_6_TypeValidator_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Given a node, get a human-readable name for the type of that node so
 * that will be easy for the programmer to find the original declaration.
 *
 * For example, if SubFoo's property "bar" might have the human-readable
 * name "Foo.prototype.bar".
 *
 * @param n The node.
 * @param dereference If true, the type of the node will be dereferenced
 *     to an Object type, if possible.
 */
String getReadableJSTypeName(Node n, boolean dereference) {
  // If we're analyzing a GETPROP, the property may be inherited by the
  // prototype chain. So climb the prototype chain and find out where
  // the property was originally defined.
  if (n.isGetProp()) {
    ObjectType objectType = getJSType(n.getFirstChild()).dereference();
    if (objectType != null) {
      String propName = n.getLastChild().getString();
      if (objectType.getConstructor() != null &&
          objectType.getConstructor().isInterface()) {
        objectType = FunctionType.getTopDefiningInterface(
            objectType, propName);
      } else {
        // classes
        while (objectType != null && !objectType.hasOwnProperty(propName)) {
          objectType = objectType.getImplicitPrototype();
        }
      }

      // Don't show complex function names or anonymous types.
      // Instead, try to get a human-readable type name.
      if (objectType != null &&
          (objectType.getConstructor() != null ||
           objectType.isFunctionPrototypeType())) {
        return objectType.toString() + "." + propName;
      }
    }
  }

  JSType type = getJSType(n);
  if (dereference) {
    ObjectType dereferenced = type.dereference();
    if (dereferenced != null) {
      type = dereferenced;
    }
  }

  String qualifiedName = n.getQualifiedName();
  if (type.isFunctionPrototypeType() ||
      (type.toObjectType() != null &&
       type.toObjectType().getConstructor() != null)) {
    return type.toString();
  } else if (qualifiedName != null) {
    return qualifiedName;
  } else if (type.isFunctionType()) {
    // Don't show complex function names.
    return "function";
  } else {
    return type.toString();
  }
}
 
Example 13
Source File: Closure_43_TypedScopeCreator_t.java    From coming with MIT License 4 votes vote down vote up
private FunctionType getFunctionType(@Nullable Var v) {
  JSType t = v == null ? null : v.getType();
  ObjectType o = t == null ? null : t.dereference();
  return JSType.toMaybeFunctionType(o);
}
 
Example 14
Source File: Closure_71_CheckAccessControls_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Dereference a type, autoboxing it and filtering out null.
 */
private static JSType dereference(JSType type) {
  return type == null ? null : type.dereference();
}
 
Example 15
Source File: Closure_48_TypedScopeCreator_t.java    From coming with MIT License 4 votes vote down vote up
private FunctionType getFunctionType(@Nullable Var v) {
  JSType t = v == null ? null : v.getType();
  ObjectType o = t == null ? null : t.dereference();
  return JSType.toMaybeFunctionType(o);
}
 
Example 16
Source File: TypeValidator.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Given a node, get a human-readable name for the type of that node so
 * that will be easy for the programmer to find the original declaration.
 *
 * For example, if SubFoo's property "bar" might have the human-readable
 * name "Foo.prototype.bar".
 *
 * @param n The node.
 * @param dereference If true, the type of the node will be dereferenced
 *     to an Object type, if possible.
 */
String getReadableJSTypeName(Node n, boolean dereference) {
  // If we're analyzing a GETPROP, the property may be inherited by the
  // prototype chain. So climb the prototype chain and find out where
  // the property was originally defined.
  if (n.isGetProp()) {
    ObjectType objectType = getJSType(n.getFirstChild()).dereference();
    if (objectType != null) {
      String propName = n.getLastChild().getString();
      if (objectType.getConstructor() != null &&
          objectType.getConstructor().isInterface()) {
        objectType = FunctionType.getTopDefiningInterface(
            objectType, propName);
      } else {
        // classes
        while (objectType != null && !objectType.hasOwnProperty(propName)) {
          objectType = objectType.getImplicitPrototype();
        }
      }

      // Don't show complex function names or anonymous types.
      // Instead, try to get a human-readable type name.
      if (objectType != null &&
          (objectType.getConstructor() != null ||
           objectType.isFunctionPrototypeType())) {
        return objectType.toString() + "." + propName;
      }
    }
  }

  JSType type = getJSType(n);
  if (dereference) {
    ObjectType dereferenced = type.dereference();
    if (dereferenced != null) {
      type = dereferenced;
    }
  }

  String qualifiedName = n.getQualifiedName();
  if (type.isFunctionPrototypeType() ||
      (type.toObjectType() != null &&
       type.toObjectType().getConstructor() != null)) {
    return type.toString();
  } else if (qualifiedName != null) {
    return qualifiedName;
  } else if (type.isFunctionType()) {
    // Don't show complex function names.
    return "function";
  } else {
    return type.toString();
  }
}
 
Example 17
Source File: Closure_17_TypedScopeCreator_t.java    From coming with MIT License 4 votes vote down vote up
private FunctionType getFunctionType(@Nullable Var v) {
  JSType t = v == null ? null : v.getType();
  ObjectType o = t == null ? null : t.dereference();
  return JSType.toMaybeFunctionType(o);
}
 
Example 18
Source File: Closure_17_TypedScopeCreator_s.java    From coming with MIT License 4 votes vote down vote up
private FunctionType getFunctionType(@Nullable Var v) {
  JSType t = v == null ? null : v.getType();
  ObjectType o = t == null ? null : t.dereference();
  return JSType.toMaybeFunctionType(o);
}
 
Example 19
Source File: Nopol2017_0027_t.java    From coming with MIT License 4 votes vote down vote up
private FunctionType getFunctionType(@Nullable Var v) {
  JSType t = v == null ? null : v.getType();
  ObjectType o = t == null ? null : t.dereference();
  return JSType.toMaybeFunctionType(o);
}
 
Example 20
Source File: Nopol2017_0027_s.java    From coming with MIT License 4 votes vote down vote up
private FunctionType getFunctionType(@Nullable Var v) {
  JSType t = v == null ? null : v.getType();
  ObjectType o = t == null ? null : t.dereference();
  return JSType.toMaybeFunctionType(o);
}