checkOptionalRecursive method

bool checkOptionalRecursive(
  1. ParameterElement parameter
)

Implementation

bool checkOptionalRecursive(ParameterElement parameter) {
  final constructor = parameter.enclosingElement as ConstructorElement;
  if (constructor.redirectedConstructor != null) {
    final redirectedConstructor = constructor.redirectedConstructor!;
    final redirectedParameter = redirectedConstructor.parameters.firstWhere(
      (element) => element.name == parameter.name,
    );
    return checkOptionalRecursive(redirectedParameter);
  }
  // TODO: remove when `analyzer: 2.0.0` dependency used.
  // Because of occasionaly missing defaultValueCode for parameter
  // we can't use `parameter.isOptional` to determine optionality.
  // Analyzer marks parameter optional regardless of missing defaultValueCode.
  // Therefore parameter can't be optional if we not have default value for it
  // or it not nullable.
  if (parameter.isOptional && !parameter.type.toString().endsWith('?')) {
    return getDefaultValueCode(parameter)?.isNotEmpty == true;
  }
  return parameter.isOptional;
}