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

The following examples show how to use com.google.javascript.rhino.jstype.JSType#matchesObjectContext() . 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: Closure_117_TypeValidator_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Expect the type to be an object, or a type convertible to object. If the
 * expectation is not met, issue a warning at the provided node's source code
 * position.
 * @return True if there was no warning, false if there was a mismatch.
 */
boolean expectObject(NodeTraversal t, Node n, JSType type, String msg) {
  if (!type.matchesObjectContext()) {
    mismatch(t, n, msg, type, OBJECT_TYPE);
    return false;
  }
  return true;
}
 
Example 2
Source File: Closure_117_TypeValidator_t.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 3
Source File: Closure_117_TypeValidator_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Expect the type to be an object, or a type convertible to object. If the
 * expectation is not met, issue a warning at the provided node's source code
 * position.
 * @return True if there was no warning, false if there was a mismatch.
 */
boolean expectObject(NodeTraversal t, Node n, JSType type, String msg) {
  if (!type.matchesObjectContext()) {
    mismatch(t, n, msg, type, OBJECT_TYPE);
    return false;
  }
  return true;
}
 
Example 4
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 5
Source File: Closure_6_TypeValidator_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Expect the type to be an object, or a type convertible to object. If the
 * expectation is not met, issue a warning at the provided node's source code
 * position.
 * @return True if there was no warning, false if there was a mismatch.
 */
boolean expectObject(NodeTraversal t, Node n, JSType type, String msg) {
  if (!type.matchesObjectContext()) {
    mismatch(t, n, msg, type, OBJECT_TYPE);
    return false;
  }
  return true;
}
 
Example 6
Source File: Closure_6_TypeValidator_t.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.getIndexType() != null) {
      expectCanAssignTo(t, indexNode, indexType, dereferenced.getIndexType(),
          "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_6_TypeValidator_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Expect the type to be an object, or a type convertible to object. If the
 * expectation is not met, issue a warning at the provided node's source code
 * position.
 * @return True if there was no warning, false if there was a mismatch.
 */
boolean expectObject(NodeTraversal t, Node n, JSType type, String msg) {
  if (!type.matchesObjectContext()) {
    mismatch(t, n, msg, type, OBJECT_TYPE);
    return false;
  }
  return true;
}
 
Example 8
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 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.getIndexType() != null) {
      expectCanAssignTo(t, indexNode, indexType, dereferenced.getIndexType(),
          "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 9
Source File: TypeValidator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Expect the type to be an object, or a type convertible to object. If the
 * expectation is not met, issue a warning at the provided node's source code
 * position.
 * @return True if there was no warning, false if there was a mismatch.
 */
boolean expectObject(NodeTraversal t, Node n, JSType type, String msg) {
  if (!type.matchesObjectContext()) {
    mismatch(t, n, msg, type, OBJECT_TYPE);
    return false;
  }
  return true;
}
 
Example 10
Source File: TypeValidator.java    From astor with GNU General Public License v2.0 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.getIndexType() != null) {
      expectCanAssignTo(t, indexNode, indexType, dereferenced.getIndexType(),
          "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));
    }
  }
}