Revert "[analyzer][meta] Refactor TargetKind to be a class, add a value in TargetKind to represent type parameter"

This reverts commit 82143e6c95.

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: https://github.com/dart-lang/sdk/issues/49796
> Change-Id: Ide144ceb57bae94a71b9d1a7ec841d03363fc121
> Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/258200
> Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>

# Not skipping CQ checks because original CL landed > 1 day ago.

Bug: https://github.com/dart-lang/sdk/issues/49796
Change-Id: I6ec04ffe8d85c417d138626ffa4f1a7ac7dafe2b
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/268380
Reviewed-by: Samuel Rawlins <srawlins@google.com>
Commit-Queue: Samuel Rawlins <srawlins@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
Samuel Rawlins 2022-11-07 20:17:09 +00:00 committed by Commit Queue
parent fa6840ae13
commit 813ca7faf6
5 changed files with 127 additions and 171 deletions

View file

@ -29,103 +29,95 @@ class Target {
/// An enumeration of the kinds of targets to which an annotation can be
/// applied.
///
/// More values will be added in the future, as the Dart language evolves.
class TargetKind {
// This class is not meant to be instantiated or extended; this constructor
// prevents instantiation and extension.
const TargetKind._(this.displayString, this.name);
/// A numeric identifier for the enumerated value.
int get index => values.indexOf(this);
/// A user visible string used to describe this target kind.
final String displayString;
/// The name of the [TargetKind] value.
///
/// The name is a string containing the source identifier used to declare the [TargetKind] value.
/// For example, the result of `TargetKind.classType.name` is the string "classType".
final String name;
enum TargetKind {
/// Indicates that an annotation is valid on any class declaration.
static const classType = TargetKind._('classes', 'classType');
classType,
/// Indicates that an annotation is valid on any enum declaration.
static const enumType = TargetKind._('enums', 'enumType');
enumType,
/// Indicates that an annotation is valid on any extension declaration.
static const extension = TargetKind._('extensions', 'extension');
extension,
/// Indicates that an annotation is valid on any field declaration, both
/// instance and static fields, whether it's in a class, mixin or extension.
static const field = TargetKind._('fields', 'field');
field,
/// Indicates that an annotation is valid on any top-level function
/// declaration.
static const function = TargetKind._('top-level functions', 'function');
function,
/// Indicates that an annotation is valid on the first directive in a library,
/// whether that's a `library`, `import`, `export` or `part` directive. This
/// doesn't include the `part of` directive in a part file.
static const library = TargetKind._('libraries', 'library');
library,
/// Indicates that an annotation is valid on any getter declaration, both
/// instance or static getters, whether it's in a class, mixin, extension, or
/// at the top-level of a library.
static const getter = TargetKind._('getters', 'getter');
getter,
/// Indicates that an annotation is valid on any method declaration, both
/// instance and static methods, whether it's in a class, mixin or extension.
static const method = TargetKind._('methods', 'method');
method,
/// Indicates that an annotation is valid on any mixin declaration.
static const mixinType = TargetKind._('mixins', 'mixinType');
mixinType,
/// Indicates that an annotation is valid on any formal parameter declaration,
/// whether it's in a function, method, constructor, or closure.
static const parameter = TargetKind._('parameters', 'parameter');
parameter,
/// Indicates that an annotation is valid on any setter declaration, both
/// instance or static setters, whether it's in a class, mixin, extension, or
/// at the top-level of a library.
static const setter = TargetKind._('setters', 'setter');
setter,
/// Indicates that an annotation is valid on any top-level variable
/// declaration.
static const topLevelVariable =
TargetKind._('top-level variables', 'topLevelVariable');
topLevelVariable,
/// Indicates that an annotation is valid on any declaration that introduces a
/// type. This includes classes, enums, mixins and typedefs, but does not
/// include extensions because extensions don't introduce a type.
static const type =
TargetKind._('types (classes, enums, mixins, or typedefs)', 'type');
type,
/// Indicates that an annotation is valid on any typedef declaration.`
static const typedefType = TargetKind._('typedefs', 'typedefType');
/// Indicates that an annotation is valid on any type parameter declaration.
static const typeParameter = TargetKind._('type parameters', 'typeParameter');
static const values = [
classType,
enumType,
extension,
field,
function,
library,
getter,
method,
mixinType,
parameter,
setter,
topLevelVariable,
type,
typedefType,
typeParameter,
];
@override
String toString() => 'TargetKind.$name';
/// Indicates that an annotation is valid on any typedef declaration.
typedefType,
}
extension TargetKindExtension on TargetKind {
/// Return a user visible string used to describe this target kind.
String get displayString {
switch (this) {
case TargetKind.classType:
return 'classes';
case TargetKind.enumType:
return 'enums';
case TargetKind.extension:
return 'extensions';
case TargetKind.field:
return 'fields';
case TargetKind.function:
return 'top-level functions';
case TargetKind.library:
return 'libraries';
case TargetKind.getter:
return 'getters';
case TargetKind.method:
return 'methods';
case TargetKind.mixinType:
return 'mixins';
case TargetKind.parameter:
return 'parameters';
case TargetKind.setter:
return 'setters';
case TargetKind.topLevelVariable:
return 'top-level variables';
case TargetKind.type:
return 'types (classes, enums, mixins, or typedefs)';
case TargetKind.typedefType:
return 'typedefs';
}
}
}

View file

@ -40,9 +40,14 @@ extension ElementAnnotationExtensions on ElementAnnotation {
// We can't directly translate the index from the analyzed TargetKind
// constant to TargetKinds.values because the analyzer from the SDK
// may have been compiled with a different version of pkg:meta.
final stringRepresentation =
kindObject.getField('name')!.toStringValue()!;
final name = 'TargetKind.$stringRepresentation';
var index = kindObject.getField('index')!.toIntValue()!;
var targetKindClass =
(kindObject.type as InterfaceType).element as EnumElementImpl;
// Instead, map constants to their TargetKind by comparing getter
// names.
var getter = targetKindClass.constants[index];
var name = 'TargetKind.${getter.name}';
var foundTargetKind = _targetKindsByName[name];
if (foundTargetKind != null) {
kinds.add(foundTargetKind);

View file

@ -1762,8 +1762,6 @@ class BestPracticesVerifier extends RecursiveAstVisitor<void> {
kinds.contains(TargetKind.type);
} else if (target is TopLevelVariableDeclaration) {
return kinds.contains(TargetKind.topLevelVariable);
} else if (target is TypeParameter) {
return kinds.contains(TargetKind.typeParameter);
}
return false;
}

View file

@ -230,52 +230,21 @@ class Target {
const Target(this.kinds);
}
class TargetKind {
const TargetKind._(this.displayString, this.name);
int get index => values.indexOf(this);
final String displayString;
final String name;
static const classType = TargetKind._('classes', 'classType');
static const enumType = TargetKind._('enums', 'enumType');
static const extension = TargetKind._('extensions', 'extension');
static const field = TargetKind._('fields', 'field');
static const function = TargetKind._('top-level functions', 'function');
static const library = TargetKind._('libraries', 'library');
static const getter = TargetKind._('getters', 'getter');
static const method = TargetKind._('methods', 'method');
static const mixinType = TargetKind._('mixins', 'mixinType');
static const parameter = TargetKind._('parameters', 'parameter');
static const setter = TargetKind._('setters', 'setter');
static const topLevelVariable =
TargetKind._('top-level variables', 'topLevelVariable');
static const type =
TargetKind._('types (classes, enums, mixins, or typedefs)', 'type');
static const typedefType = TargetKind._('typedefs', 'typedefType');
static const typeParameter = TargetKind._('type parameters', 'typeParameter');
static const values = [
classType,
enumType,
extension,
field,
function,
library,
getter,
method,
mixinType,
parameter,
setter,
topLevelVariable,
type,
typedefType,
typeParameter,
];
@override
String toString() => 'TargetKind.$name';
enum TargetKind {
classType,
enumType,
extension,
field,
function,
library,
getter,
method,
mixinType,
parameter,
setter,
topLevelVariable,
type,
typedefType,
}
''');
}

View file

@ -29,103 +29,95 @@ class Target {
/// An enumeration of the kinds of targets to which an annotation can be
/// applied.
///
/// More values will be added in the future, as the Dart language evolves.
class TargetKind {
// This class is not meant to be instantiated or extended; this constructor
// prevents instantiation and extension.
const TargetKind._(this.displayString, this.name);
/// A numeric identifier for the enumerated value.
int get index => values.indexOf(this);
/// A user visible string used to describe this target kind.
final String displayString;
/// The name of the [TargetKind] value.
///
/// The name is a string containing the source identifier used to declare the [TargetKind] value.
/// For example, the result of `TargetKind.classType.name` is the string "classType".
final String name;
enum TargetKind {
/// Indicates that an annotation is valid on any class declaration.
static const classType = TargetKind._('classes', 'classType');
classType,
/// Indicates that an annotation is valid on any enum declaration.
static const enumType = TargetKind._('enums', 'enumType');
enumType,
/// Indicates that an annotation is valid on any extension declaration.
static const extension = TargetKind._('extensions', 'extension');
extension,
/// Indicates that an annotation is valid on any field declaration, both
/// instance and static fields, whether it's in a class, mixin or extension.
static const field = TargetKind._('fields', 'field');
field,
/// Indicates that an annotation is valid on any top-level function
/// declaration.
static const function = TargetKind._('top-level functions', 'function');
function,
/// Indicates that an annotation is valid on the first directive in a library,
/// whether that's a `library`, `import`, `export` or `part` directive. This
/// doesn't include the `part of` directive in a part file.
static const library = TargetKind._('libraries', 'library');
library,
/// Indicates that an annotation is valid on any getter declaration, both
/// instance or static getters, whether it's in a class, mixin, extension, or
/// at the top-level of a library.
static const getter = TargetKind._('getters', 'getter');
getter,
/// Indicates that an annotation is valid on any method declaration, both
/// instance and static methods, whether it's in a class, mixin or extension.
static const method = TargetKind._('methods', 'method');
method,
/// Indicates that an annotation is valid on any mixin declaration.
static const mixinType = TargetKind._('mixins', 'mixinType');
mixinType,
/// Indicates that an annotation is valid on any formal parameter declaration,
/// whether it's in a function, method, constructor, or closure.
static const parameter = TargetKind._('parameters', 'parameter');
parameter,
/// Indicates that an annotation is valid on any setter declaration, both
/// instance or static setters, whether it's in a class, mixin, extension, or
/// at the top-level of a library.
static const setter = TargetKind._('setters', 'setter');
setter,
/// Indicates that an annotation is valid on any top-level variable
/// declaration.
static const topLevelVariable =
TargetKind._('top-level variables', 'topLevelVariable');
topLevelVariable,
/// Indicates that an annotation is valid on any declaration that introduces a
/// type. This includes classes, enums, mixins and typedefs, but does not
/// include extensions because extensions don't introduce a type.
static const type =
TargetKind._('types (classes, enums, mixins, or typedefs)', 'type');
type,
/// Indicates that an annotation is valid on any typedef declaration.`
static const typedefType = TargetKind._('typedefs', 'typedefType');
/// Indicates that an annotation is valid on any type parameter declaration.
static const typeParameter = TargetKind._('type parameters', 'typeParameter');
static const values = [
classType,
enumType,
extension,
field,
function,
library,
getter,
method,
mixinType,
parameter,
setter,
topLevelVariable,
type,
typedefType,
typeParameter,
];
@override
String toString() => 'TargetKind.$name';
/// Indicates that an annotation is valid on any typedef declaration.
typedefType,
}
extension TargetKindExtension on TargetKind {
/// Return a user visible string used to describe this target kind.
String get displayString {
switch (this) {
case TargetKind.classType:
return 'classes';
case TargetKind.enumType:
return 'enums';
case TargetKind.extension:
return 'extensions';
case TargetKind.field:
return 'fields';
case TargetKind.function:
return 'top-level functions';
case TargetKind.library:
return 'libraries';
case TargetKind.getter:
return 'getters';
case TargetKind.method:
return 'methods';
case TargetKind.mixinType:
return 'mixins';
case TargetKind.parameter:
return 'parameters';
case TargetKind.setter:
return 'setters';
case TargetKind.topLevelVariable:
return 'top-level variables';
case TargetKind.type:
return 'types (classes, enums, mixins, or typedefs)';
case TargetKind.typedefType:
return 'typedefs';
}
}
}