Skip to content

Commit 813ca7f

Browse files
srawlinsCommit Queue
authored and
Commit Queue
committed
Revert "[analyzer][meta] Refactor TargetKind to be a class, add a value in TargetKind to represent type parameter"
This reverts commit 82143e6. Reason for revert: This commit appears to have negatively affected the analyze benchmark: * https://golem.corp.goog/Revision?repository=flutter-analyze&team=dartanalyzer&revision=114193 We need to revert and analyze it. Original change's description: > [analyzer][meta] Refactor TargetKind to be a class, add a value in TargetKind to represent type parameter > > Bug: #49796 > Change-Id: Ide144ceb57bae94a71b9d1a7ec841d03363fc121 > Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/258200 > Commit-Queue: Brian Wilkerson <[email protected]> > Reviewed-by: Brian Wilkerson <[email protected]> # Not skipping CQ checks because original CL landed > 1 day ago. Bug: #49796 Change-Id: I6ec04ffe8d85c417d138626ffa4f1a7ac7dafe2b Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/268380 Reviewed-by: Samuel Rawlins <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent fa6840a commit 813ca7f

File tree

5 files changed

+129
-173
lines changed

5 files changed

+129
-173
lines changed

pkg/analysis_server/test/mock_packages/meta/lib/meta_meta.dart

Lines changed: 53 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -29,103 +29,95 @@ class Target {
2929

3030
/// An enumeration of the kinds of targets to which an annotation can be
3131
/// applied.
32-
///
33-
/// More values will be added in the future, as the Dart language evolves.
34-
class TargetKind {
35-
// This class is not meant to be instantiated or extended; this constructor
36-
// prevents instantiation and extension.
37-
const TargetKind._(this.displayString, this.name);
38-
39-
/// A numeric identifier for the enumerated value.
40-
int get index => values.indexOf(this);
41-
42-
/// A user visible string used to describe this target kind.
43-
final String displayString;
44-
45-
/// The name of the [TargetKind] value.
46-
///
47-
/// The name is a string containing the source identifier used to declare the [TargetKind] value.
48-
/// For example, the result of `TargetKind.classType.name` is the string "classType".
49-
final String name;
50-
32+
enum TargetKind {
5133
/// Indicates that an annotation is valid on any class declaration.
52-
static const classType = TargetKind._('classes', 'classType');
34+
classType,
5335

5436
/// Indicates that an annotation is valid on any enum declaration.
55-
static const enumType = TargetKind._('enums', 'enumType');
37+
enumType,
5638

5739
/// Indicates that an annotation is valid on any extension declaration.
58-
static const extension = TargetKind._('extensions', 'extension');
40+
extension,
5941

6042
/// Indicates that an annotation is valid on any field declaration, both
6143
/// instance and static fields, whether it's in a class, mixin or extension.
62-
static const field = TargetKind._('fields', 'field');
44+
field,
6345

6446
/// Indicates that an annotation is valid on any top-level function
6547
/// declaration.
66-
static const function = TargetKind._('top-level functions', 'function');
48+
function,
6749

6850
/// Indicates that an annotation is valid on the first directive in a library,
6951
/// whether that's a `library`, `import`, `export` or `part` directive. This
7052
/// doesn't include the `part of` directive in a part file.
71-
static const library = TargetKind._('libraries', 'library');
53+
library,
7254

7355
/// Indicates that an annotation is valid on any getter declaration, both
7456
/// instance or static getters, whether it's in a class, mixin, extension, or
7557
/// at the top-level of a library.
76-
static const getter = TargetKind._('getters', 'getter');
58+
getter,
7759

7860
/// Indicates that an annotation is valid on any method declaration, both
7961
/// instance and static methods, whether it's in a class, mixin or extension.
80-
static const method = TargetKind._('methods', 'method');
62+
method,
8163

8264
/// Indicates that an annotation is valid on any mixin declaration.
83-
static const mixinType = TargetKind._('mixins', 'mixinType');
65+
mixinType,
8466

8567
/// Indicates that an annotation is valid on any formal parameter declaration,
8668
/// whether it's in a function, method, constructor, or closure.
87-
static const parameter = TargetKind._('parameters', 'parameter');
69+
parameter,
8870

8971
/// Indicates that an annotation is valid on any setter declaration, both
9072
/// instance or static setters, whether it's in a class, mixin, extension, or
9173
/// at the top-level of a library.
92-
static const setter = TargetKind._('setters', 'setter');
74+
setter,
9375

9476
/// Indicates that an annotation is valid on any top-level variable
9577
/// declaration.
96-
static const topLevelVariable =
97-
TargetKind._('top-level variables', 'topLevelVariable');
78+
topLevelVariable,
9879

9980
/// Indicates that an annotation is valid on any declaration that introduces a
10081
/// type. This includes classes, enums, mixins and typedefs, but does not
10182
/// include extensions because extensions don't introduce a type.
102-
static const type =
103-
TargetKind._('types (classes, enums, mixins, or typedefs)', 'type');
104-
105-
/// Indicates that an annotation is valid on any typedef declaration.`
106-
static const typedefType = TargetKind._('typedefs', 'typedefType');
107-
108-
/// Indicates that an annotation is valid on any type parameter declaration.
109-
static const typeParameter = TargetKind._('type parameters', 'typeParameter');
110-
111-
static const values = [
112-
classType,
113-
enumType,
114-
extension,
115-
field,
116-
function,
117-
library,
118-
getter,
119-
method,
120-
mixinType,
121-
parameter,
122-
setter,
123-
topLevelVariable,
124-
type,
125-
typedefType,
126-
typeParameter,
127-
];
128-
129-
@override
130-
String toString() => 'TargetKind.$name';
83+
type,
84+
85+
/// Indicates that an annotation is valid on any typedef declaration.
86+
typedefType,
87+
}
88+
89+
extension TargetKindExtension on TargetKind {
90+
/// Return a user visible string used to describe this target kind.
91+
String get displayString {
92+
switch (this) {
93+
case TargetKind.classType:
94+
return 'classes';
95+
case TargetKind.enumType:
96+
return 'enums';
97+
case TargetKind.extension:
98+
return 'extensions';
99+
case TargetKind.field:
100+
return 'fields';
101+
case TargetKind.function:
102+
return 'top-level functions';
103+
case TargetKind.library:
104+
return 'libraries';
105+
case TargetKind.getter:
106+
return 'getters';
107+
case TargetKind.method:
108+
return 'methods';
109+
case TargetKind.mixinType:
110+
return 'mixins';
111+
case TargetKind.parameter:
112+
return 'parameters';
113+
case TargetKind.setter:
114+
return 'setters';
115+
case TargetKind.topLevelVariable:
116+
return 'top-level variables';
117+
case TargetKind.type:
118+
return 'types (classes, enums, mixins, or typedefs)';
119+
case TargetKind.typedefType:
120+
return 'typedefs';
121+
}
122+
}
131123
}

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,14 @@ extension ElementAnnotationExtensions on ElementAnnotation {
4040
// We can't directly translate the index from the analyzed TargetKind
4141
// constant to TargetKinds.values because the analyzer from the SDK
4242
// may have been compiled with a different version of pkg:meta.
43-
final stringRepresentation =
44-
kindObject.getField('name')!.toStringValue()!;
45-
final name = 'TargetKind.$stringRepresentation';
43+
var index = kindObject.getField('index')!.toIntValue()!;
44+
var targetKindClass =
45+
(kindObject.type as InterfaceType).element as EnumElementImpl;
46+
// Instead, map constants to their TargetKind by comparing getter
47+
// names.
48+
var getter = targetKindClass.constants[index];
49+
var name = 'TargetKind.${getter.name}';
50+
4651
var foundTargetKind = _targetKindsByName[name];
4752
if (foundTargetKind != null) {
4853
kinds.add(foundTargetKind);

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1762,8 +1762,6 @@ class BestPracticesVerifier extends RecursiveAstVisitor<void> {
17621762
kinds.contains(TargetKind.type);
17631763
} else if (target is TopLevelVariableDeclaration) {
17641764
return kinds.contains(TargetKind.topLevelVariable);
1765-
} else if (target is TypeParameter) {
1766-
return kinds.contains(TargetKind.typeParameter);
17671765
}
17681766
return false;
17691767
}

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

Lines changed: 15 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -230,52 +230,21 @@ class Target {
230230
const Target(this.kinds);
231231
}
232232
233-
class TargetKind {
234-
const TargetKind._(this.displayString, this.name);
235-
236-
int get index => values.indexOf(this);
237-
238-
final String displayString;
239-
final String name;
240-
241-
static const classType = TargetKind._('classes', 'classType');
242-
static const enumType = TargetKind._('enums', 'enumType');
243-
static const extension = TargetKind._('extensions', 'extension');
244-
static const field = TargetKind._('fields', 'field');
245-
static const function = TargetKind._('top-level functions', 'function');
246-
static const library = TargetKind._('libraries', 'library');
247-
static const getter = TargetKind._('getters', 'getter');
248-
static const method = TargetKind._('methods', 'method');
249-
static const mixinType = TargetKind._('mixins', 'mixinType');
250-
static const parameter = TargetKind._('parameters', 'parameter');
251-
static const setter = TargetKind._('setters', 'setter');
252-
static const topLevelVariable =
253-
TargetKind._('top-level variables', 'topLevelVariable');
254-
static const type =
255-
TargetKind._('types (classes, enums, mixins, or typedefs)', 'type');
256-
static const typedefType = TargetKind._('typedefs', 'typedefType');
257-
static const typeParameter = TargetKind._('type parameters', 'typeParameter');
258-
259-
static const values = [
260-
classType,
261-
enumType,
262-
extension,
263-
field,
264-
function,
265-
library,
266-
getter,
267-
method,
268-
mixinType,
269-
parameter,
270-
setter,
271-
topLevelVariable,
272-
type,
273-
typedefType,
274-
typeParameter,
275-
];
276-
277-
@override
278-
String toString() => 'TargetKind.$name';
233+
enum TargetKind {
234+
classType,
235+
enumType,
236+
extension,
237+
field,
238+
function,
239+
library,
240+
getter,
241+
method,
242+
mixinType,
243+
parameter,
244+
setter,
245+
topLevelVariable,
246+
type,
247+
typedefType,
279248
}
280249
''');
281250
}

0 commit comments

Comments
 (0)