compareTo method
Compares this constructor with newConstructor and returns a list of
ApiChanges that have been detected between the two constructors.
Implementation
List<ApiChange> compareTo(
DocConstructor newConstructor, {
required DocComponent component,
}) {
final changes = <ApiChange>[];
for (final annotation in annotations) {
if (!newConstructor.annotations.contains(annotation)) {
changes.add(ConstructorApiChange(
component: component,
constructor: this,
operation: ApiChangeOperation.annotationRemoved,
annotation: annotation,
));
}
}
for (final annotation in newConstructor.annotations) {
if (!annotations.contains(annotation)) {
changes.add(ConstructorApiChange(
component: component,
constructor: this,
operation: ApiChangeOperation.annotationAdded,
annotation: annotation,
));
}
}
_addChange(DocParameter parameter, ApiChangeOperation operation, {String? annotation}) {
changes.add(ConstructorParameterApiChange(
component: component,
constructor: this,
operation: operation,
parameter: parameter,
annotation: annotation,
));
}
for (var i = 0; i < signature.length; i++) {
final oldParam = signature[i];
final newParam = newConstructor.signature.firstWhereOrNull((element) => element.name == oldParam.name);
if (newParam == null) {
_addChange(oldParam, ApiChangeOperation.removed);
continue;
}
if (oldParam.required != newParam.required) {
_addChange(
oldParam,
oldParam.required ? ApiChangeOperation.becameOptional : ApiChangeOperation.becameRequired,
);
}
if (oldParam.named != newParam.named) {
_addChange(
oldParam,
oldParam.named ? ApiChangeOperation.becamePositional : ApiChangeOperation.becameNamed,
);
}
if (oldParam.type != newParam.type) {
_addChange(oldParam, ApiChangeOperation.typeChanged);
}
for (final annotation in oldParam.annotations) {
if (!newParam.annotations.contains(annotation)) {
_addChange(oldParam, ApiChangeOperation.annotationRemoved, annotation: annotation);
}
}
for (final annotation in newParam.annotations) {
if (!oldParam.annotations.contains(annotation)) {
_addChange(oldParam, ApiChangeOperation.annotationAdded, annotation: annotation);
}
}
}
for (var i = 0; i < newConstructor.signature.length; i++) {
final newParam = newConstructor.signature[i];
final oldParam = signature.firstWhereOrNull((element) => element.name == newParam.name);
if (oldParam == null) {
_addChange(newParam, ApiChangeOperation.added);
}
}
return changes;
}