parseTokenObject method

TokenElement parseTokenObject(
  1. DartObject object, [
  2. ParameterElement? element
])

Returns object parsed into a TokenElement.

Only a DartType or OpaqueToken are currently supported.

Implementation

TokenElement parseTokenObject(DartObject object,
    [ParameterElement? element]) {
  final constant = ConstantReader(object);
  if (constant.isNull) {
    final errorMsg = 'Annotation on element has errors and was unresolvable.';
    if (element != null) {
      throw BuildError.forElement(element, errorMsg);
    }
    throw FormatException(errorMsg);
  }
  if (constant.isType) {
    final typeValue = constant.typeValue;
    // TODO: assertNotFunctionType.
    return TypeTokenElement(linkTypeOf(typeValue));
  }
  if (constant.instanceOf($OpaqueToken)) {
    return _parseOpaqueToken(constant, element);
  }
  final typeStr = object.type!.getDisplayString(withNullability: false);
  final error =
      'Not a valid token for injection: $object. In previous versions of '
      'AngularDart it was valid to try and inject by other token types '
      'expressable in Dart. However, compile-time injection now only '
      'supports either "Type", "OpaqueToken", "MultiToken" or a class '
      'extending "OpaqueToken" or "MultiToken".\n\n'
      'However: $typeStr was passed, which is not supported';
  if (element != null) {
    throw BuildError.forElement(element, error);
  } else {
    throw BuildError.withoutContext(error);
  }
}