Skip to content

Commit ab0b15c

Browse files
committed
Update deprecated check to consider new annotations
1 parent 4df6dbe commit ab0b15c

File tree

1 file changed

+37
-11
lines changed

1 file changed

+37
-11
lines changed

lib/src/model/model_element.dart

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -634,27 +634,26 @@ abstract class ModelElement
634634
bool get isConst => false;
635635

636636
bool get isDeprecated {
637-
// If element.metadata is empty, it might be because this is a property
638-
// where the metadata belongs to the individual getter/setter
639-
if (element.annotations.isEmpty && element is PropertyInducingElement) {
640-
var pie = element as PropertyInducingElement;
641-
642-
// The getter or the setter might be null – so the stored value may be
643-
// `true`, `false`, or `null`
644-
var getterDeprecated = pie.getter?.metadata.hasDeprecated;
645-
var setterDeprecated = pie.setter?.metadata.hasDeprecated;
637+
// If `element.annotations` is empty, it might be because this is a property
638+
// where the annotations belongs to the individual getter/setter.
639+
if (element case PropertyInducingElement element
640+
when element.annotations.isEmpty) {
641+
// The getter or the setter might be `null` – so the stored value may be
642+
// `true`, `false`, or `null`.
643+
var getterDeprecated = element.getter?.isDeprecatedWithKind('use');
644+
var setterDeprecated = element.setter?.isDeprecatedWithKind('use');
646645

647646
var deprecatedValues = [getterDeprecated, setterDeprecated].nonNulls;
648647

649-
// At least one of these should be non-null. Otherwise things are weird
648+
// At least one of these should be non-null. Otherwise things are weird.
650649
assert(deprecatedValues.isNotEmpty);
651650

652651
// If there are both a setter and getter, only show the property as
653652
// deprecated if both are deprecated.
654653
return deprecatedValues.every((d) => d);
655654
}
656655

657-
return element.metadata.hasDeprecated;
656+
return element.isDeprecatedWithKind('use');
658657
}
659658

660659
@override
@@ -831,3 +830,30 @@ extension on Element {
831830
return metadata.annotations;
832831
}
833832
}
833+
834+
// Copied from analyzer's `lib/src/dart/element/extensions.dart`. Re-use that
835+
// extension if it becomes public.
836+
extension on Element {
837+
/// Whether this Element is annotated with a `Deprecated` annotation with a
838+
/// `_DeprecationKind` of [kind].
839+
bool isDeprecatedWithKind(String kind) => metadata.annotations
840+
.where((e) => e.isDeprecated)
841+
.any((e) => e.deprecationKind == kind);
842+
}
843+
844+
// Copied from analyzer's `lib/src/dart/element/extensions.dart`. Re-use that
845+
// extension if it becomes public.
846+
extension ElementAnnotationExtension on ElementAnnotation {
847+
/// The kind of deprecation, if this annotation is a `Deprecated` annotation.
848+
///
849+
/// `null` is returned if this is not a `Deprecated` annotation.
850+
String? get deprecationKind {
851+
if (!isDeprecated) return null;
852+
return computeConstantValue()
853+
?.getField('_kind')
854+
?.getField('_name')
855+
?.toStringValue() ??
856+
// For SDKs where the `Deprecated` class does not have a deprecation kind.
857+
'use';
858+
}
859+
}

0 commit comments

Comments
 (0)