Skip to content

Commit aac3076

Browse files
srawlinsCommit Queue
authored andcommitted
analyzer: Change mock SDK Deprecated to use enum
Change-Id: If3ca71eda399292373e95a26530f8b2c54437779 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/445403 Commit-Queue: Samuel Rawlins <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent c1a26df commit aac3076

File tree

4 files changed

+50
-61
lines changed

4 files changed

+50
-61
lines changed

pkg/analyzer/lib/src/dart/element/extensions.dart

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@ extension Element2Extension on Element {
8686
var annotations = metadata.annotations.where((e) => e.isDeprecated);
8787
return annotations.any((annotation) {
8888
var value = annotation.computeConstantValue();
89-
return value?.getField('_isUse')?.toBoolValue() ?? true;
89+
var kindValue = value?.getField('_kind');
90+
if (kindValue == null) return true;
91+
var kind = kindValue.getField('_name')?.toStringValue();
92+
return kind == 'use';
9093
});
9194
}
9295

@@ -102,6 +105,12 @@ extension Element2Extension on Element {
102105
this is! SuperFormalParameterElement)) &&
103106
library.hasWildcardVariablesFeatureEnabled;
104107
}
108+
109+
/// Whether this Element is annotated with a `Deprecated` annotation with a
110+
/// `_DeprecationKind` of [kind].
111+
bool isDeprecatedWithKind(String kind) => metadata.annotations
112+
.where((e) => e.isDeprecated)
113+
.any((e) => e.deprecationKind == kind);
105114
}
106115

107116
extension Element2OrNullExtension on Element? {
@@ -111,11 +120,24 @@ extension Element2OrNullExtension on Element? {
111120
}
112121
}
113122

114-
extension ElementAnnotationExtensions on ElementAnnotation {
123+
extension ElementAnnotationExtension on ElementAnnotation {
115124
static final Map<String, TargetKind> _targetKindsByName = {
116125
for (var kind in TargetKind.values) kind.name: kind,
117126
};
118127

128+
/// The kind of deprecation, if this annotation is a `Deprecated` annotation.
129+
///
130+
/// `null` is returned if this is not a `Deprecated` annotation.
131+
String? get deprecationKind {
132+
if (!isDeprecated) return null;
133+
return computeConstantValue()
134+
?.getField('_kind')
135+
?.getField('_name')
136+
?.toStringValue() ??
137+
// For SDKs where the `Deprecated` class does not have a deprecation kind.
138+
'use';
139+
}
140+
119141
/// Return the target kinds defined for this [ElementAnnotation].
120142
Set<TargetKind> get targetKinds {
121143
var element = this.element;

pkg/analyzer/lib/src/error/annotation_verifier.dart

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -117,34 +117,31 @@ class AnnotationVerifier {
117117

118118
void _checkDeprecated(Annotation node) {
119119
var element = node.elementAnnotation;
120-
var value = element?.computeConstantValue();
121-
if (value == null) return;
120+
if (element == null) return;
121+
assert(element.isDeprecated);
122+
var kind = element.deprecationKind;
123+
if (kind == null) return;
122124

123125
// The vast majority of deprecated annotations use the default constructor.
124126
// Check this case first.
125-
if (value.getField('_isUse')?.toBoolValue() ?? true) return;
127+
if (kind == 'use') return;
126128

127-
var isExtend = value.getField('_isExtend')?.toBoolValue() ?? false;
128-
if (isExtend) {
129+
if (kind == 'extend') {
129130
_checkDeprecatedExtend(node, node.parent);
130131
return;
131132
}
132133

133-
var isImplement = value.getField('_isImplement')?.toBoolValue() ?? false;
134-
if (isImplement) {
134+
if (kind == 'implement') {
135135
_checkDeprecatedImplement(node, node.parent);
136136
return;
137137
}
138138

139-
var isSubclass = value.getField('_isSubclass')?.toBoolValue() ?? false;
140-
if (isSubclass) {
139+
if (kind == 'subclass') {
141140
_checkDeprecatedSubclass(node, node.parent);
142141
return;
143142
}
144143

145-
var isInstantiate =
146-
value.getField('_isInstantiate')?.toBoolValue() ?? false;
147-
if (isInstantiate) {
144+
if (kind == 'instantiate') {
148145
_checkDeprecatedInstantiate(node, node.parent);
149146
return;
150147
}

pkg/analyzer/lib/src/error/deprecated_functionality_verifier.dart

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'package:analyzer/dart/element/element.dart';
66
import 'package:analyzer/error/listener.dart';
77
import 'package:analyzer/src/dart/ast/ast.dart';
8+
import 'package:analyzer/src/dart/element/extensions.dart';
89
import 'package:analyzer/src/error/codes.dart';
910

1011
class DeprecatedFunctionalityVerifier {
@@ -31,7 +32,7 @@ class DeprecatedFunctionalityVerifier {
3132
void constructorName(ConstructorName node) {
3233
var classElement = node.type.element;
3334
if (classElement == null) return;
34-
if (classElement.hasDeprecatedWithField('_isInstantiate')) {
35+
if (classElement.isDeprecatedWithKind('instantiate')) {
3536
_diagnosticReporter.atNode(
3637
node,
3738
WarningCode.deprecatedInstantiate,
@@ -57,13 +58,13 @@ class DeprecatedFunctionalityVerifier {
5758
if (element == null) return;
5859
if (node.type?.element is InterfaceElement) {
5960
if (element.library == _currentLibrary) return;
60-
if (element.hasDeprecatedWithField('_isExtend')) {
61+
if (element.isDeprecatedWithKind('extend')) {
6162
_diagnosticReporter.atNode(
6263
node,
6364
WarningCode.deprecatedExtend,
6465
arguments: [element.name!],
6566
);
66-
} else if (element.hasDeprecatedWithField('_isSubclass')) {
67+
} else if (element.isDeprecatedWithKind('subclass')) {
6768
_diagnosticReporter.atNode(
6869
node,
6970
WarningCode.deprecatedSubclass,
@@ -80,13 +81,13 @@ class DeprecatedFunctionalityVerifier {
8081
if (element == null) continue;
8182
if (element.library == _currentLibrary) continue;
8283
if (namedType.type?.element is InterfaceElement) {
83-
if (element.hasDeprecatedWithField('_isImplement')) {
84+
if (element.isDeprecatedWithKind('implement')) {
8485
_diagnosticReporter.atNode(
8586
namedType,
8687
WarningCode.deprecatedImplement,
8788
arguments: [element.name!],
8889
);
89-
} else if (element.hasDeprecatedWithField('_isSubclass')) {
90+
} else if (element.isDeprecatedWithKind('subclass')) {
9091
_diagnosticReporter.atNode(
9192
namedType,
9293
WarningCode.deprecatedSubclass,
@@ -104,7 +105,7 @@ class DeprecatedFunctionalityVerifier {
104105
if (element == null) continue;
105106
if (element.library == _currentLibrary) continue;
106107
if (namedType.type?.element is InterfaceElement) {
107-
if (element.hasDeprecatedWithField('_isSubclass')) {
108+
if (element.isDeprecatedWithKind('subclass')) {
108109
_diagnosticReporter.atNode(
109110
namedType,
110111
WarningCode.deprecatedSubclass,
@@ -115,14 +116,3 @@ class DeprecatedFunctionalityVerifier {
115116
}
116117
}
117118
}
118-
119-
extension on Element {
120-
/// Whether this Element is annotated with a `Deprecated` annotation with a
121-
/// `true` value for [fieldName].
122-
bool hasDeprecatedWithField(String fieldName) {
123-
return metadata.annotations.where((e) => e.isDeprecated).any((annotation) {
124-
var value = annotation.computeConstantValue();
125-
return value?.getField(fieldName)?.toBoolValue() ?? false;
126-
});
127-
}
128-
}

pkg/analyzer/lib/src/test_utilities/mock_sdk.dart

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -314,41 +314,21 @@ class DateTime extends Object {
314314
315315
class Deprecated extends Object {
316316
final String message;
317-
final bool _isUse;
318-
final bool _isImplement;
319-
final bool _isExtend;
320-
final bool _isSubclass;
321-
final bool _isInstantiate;
317+
final _DeprecationKind _kind;
322318
const Deprecated(this.message)
323-
: _isUse = true,
324-
_isImplement = false,
325-
_isExtend = false,
326-
_isSubclass = false,
327-
_isInstantiate = false;
319+
: _kind = _DeprecationKind.use;
328320
const Deprecated.implement([this.message = "next release"])
329-
: _isUse = false,
330-
_isImplement = true,
331-
_isExtend = false,
332-
_isSubclass = false,
333-
_isInstantiate = false;
321+
: _kind = _DeprecationKind.implement;
334322
const Deprecated.extend([this.message = "next release"])
335-
: _isUse = false,
336-
_isImplement = false,
337-
_isExtend = true,
338-
_isSubclass = false,
339-
_isInstantiate = false;
323+
: _kind = _DeprecationKind.extend;
340324
const Deprecated.subclass([this.message = "next release"])
341-
: _isUse = false,
342-
_isImplement = false,
343-
_isExtend = false,
344-
_isSubclass = true,
345-
_isInstantiate = false;
325+
: _kind = _DeprecationKind.subclass;
346326
const Deprecated.instantiate([this.message = "next release"])
347-
: _isUse = false,
348-
_isImplement = false,
349-
_isExtend = false,
350-
_isSubclass = false,
351-
_isInstantiate = true;
327+
: _kind = _DeprecationKind.instantiate;
328+
}
329+
330+
enum _DeprecationKind {
331+
use, implement, extend, subclass, instantiate;
352332
}
353333
354334
class pragma {

0 commit comments

Comments
 (0)