diff --git a/pkg/analysis_server/test/mock_packages/meta/lib/meta_meta.dart b/pkg/analysis_server/test/mock_packages/meta/lib/meta_meta.dart index 173d68b66c6..18902bec522 100644 --- a/pkg/analysis_server/test/mock_packages/meta/lib/meta_meta.dart +++ b/pkg/analysis_server/test/mock_packages/meta/lib/meta_meta.dart @@ -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'; + } + } } diff --git a/pkg/analyzer/lib/src/dart/element/extensions.dart b/pkg/analyzer/lib/src/dart/element/extensions.dart index cf9a3442b9d..beef866eb0f 100644 --- a/pkg/analyzer/lib/src/dart/element/extensions.dart +++ b/pkg/analyzer/lib/src/dart/element/extensions.dart @@ -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); diff --git a/pkg/analyzer/lib/src/error/best_practices_verifier.dart b/pkg/analyzer/lib/src/error/best_practices_verifier.dart index 7d58d091266..dcbd48e368e 100644 --- a/pkg/analyzer/lib/src/error/best_practices_verifier.dart +++ b/pkg/analyzer/lib/src/error/best_practices_verifier.dart @@ -1762,8 +1762,6 @@ class BestPracticesVerifier extends RecursiveAstVisitor { 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; } diff --git a/pkg/analyzer/lib/src/test_utilities/mock_packages.dart b/pkg/analyzer/lib/src/test_utilities/mock_packages.dart index 9303833a241..d6c64beff70 100644 --- a/pkg/analyzer/lib/src/test_utilities/mock_packages.dart +++ b/pkg/analyzer/lib/src/test_utilities/mock_packages.dart @@ -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, } '''); } diff --git a/pkg/meta/lib/meta_meta.dart b/pkg/meta/lib/meta_meta.dart index 173d68b66c6..18902bec522 100644 --- a/pkg/meta/lib/meta_meta.dart +++ b/pkg/meta/lib/meta_meta.dart @@ -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'; + } + } }