Line data Source code
1 : // Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
2 : // for details. All rights reserved. Use of this source code is governed by a
3 : // BSD-style license that can be found in the LICENSE file.
4 :
5 : /// Annotations that describe the intended use of other annotations.
6 : library meta_meta;
7 :
8 : /// An annotation used on classes that are intended to be used as annotations
9 : /// to indicate the kinds of declarations and directives for which the
10 : /// annotation is appropriate.
11 : ///
12 : /// The kinds are represented by the constants defined in [TargetKind].
13 : ///
14 : /// Tools, such as the analyzer, can provide feedback if
15 : ///
16 : /// * the annotation is associated with anything other than a class, where the
17 : /// class must be usable as an annotation (that is, contain at least one
18 : /// `const` constructor).
19 : /// * the annotated annotation is associated with anything other than the kinds
20 : /// of declarations listed as valid targets.
21 : @Target({TargetKind.classType})
22 : class Target {
23 : /// The kinds of declarations with which the annotated annotation can be
24 : /// associated.
25 : final Set<TargetKind> kinds;
26 :
27 77 : const Target(this.kinds);
28 : }
29 :
30 : /// An enumeration of the kinds of targets to which an annotation can be
31 : /// applied.
32 11 : enum TargetKind {
33 : /// Indicates that an annotation is valid on any class declaration.
34 : classType,
35 :
36 : /// Indicates that an annotation is valid on any enum declaration.
37 : enumType,
38 :
39 : /// Indicates that an annotation is valid on any extension declaration.
40 : extension,
41 :
42 : /// Indicates that an annotation is valid on any field declaration, both
43 : /// instance and static fields, whether it's in a class, mixin or extension.
44 : field,
45 :
46 : /// Indicates that an annotation is valid on any top-level function
47 : /// declaration.
48 : function,
49 :
50 : /// Indicates that an annotation is valid on the first directive in a library,
51 : /// whether that's a `library`, `import`, `export` or `part` directive. This
52 : /// doesn't include the `part of` directive in a part file.
53 : library,
54 :
55 : /// Indicates that an annotation is valid on any getter declaration, both
56 : /// instance or static getters, whether it's in a class, mixin, extension, or
57 : /// at the top-level of a library.
58 : getter,
59 :
60 : /// Indicates that an annotation is valid on any method declaration, both
61 : /// instance and static methods, whether it's in a class, mixin or extension.
62 : method,
63 :
64 : /// Indicates that an annotation is valid on any mixin declaration.
65 : mixinType,
66 :
67 : /// Indicates that an annotation is valid on any formal parameter declaration,
68 : /// whether it's in a function, method, constructor, or closure.
69 : parameter,
70 :
71 : /// Indicates that an annotation is valid on any setter declaration, both
72 : /// instance or static setters, whether it's in a class, mixin, extension, or
73 : /// at the top-level of a library.
74 : setter,
75 :
76 : /// Indicates that an annotation is valid on any top-level variable
77 : /// declaration.
78 : topLevelVariable,
79 :
80 : /// Indicates that an annotation is valid on any declaration that introduces a
81 : /// type. This includes classes, enums, mixins and typedefs, but does not
82 : /// include extensions because extensions don't introduce a type.
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 0 : String get displayString {
92 : switch (this) {
93 0 : case TargetKind.classType:
94 : return 'classes';
95 0 : case TargetKind.enumType:
96 : return 'enums';
97 0 : case TargetKind.extension:
98 : return 'extensions';
99 0 : case TargetKind.field:
100 : return 'fields';
101 0 : case TargetKind.function:
102 : return 'top-level functions';
103 0 : case TargetKind.library:
104 : return 'libraries';
105 0 : case TargetKind.getter:
106 : return 'getters';
107 0 : case TargetKind.method:
108 : return 'methods';
109 0 : case TargetKind.mixinType:
110 : return 'mixins';
111 0 : case TargetKind.parameter:
112 : return 'parameters';
113 0 : case TargetKind.setter:
114 : return 'setters';
115 0 : case TargetKind.topLevelVariable:
116 : return 'top-level variables';
117 0 : case TargetKind.type:
118 : return 'types (classes, enums, mixins, or typedefs)';
119 0 : case TargetKind.typedefType:
120 : return 'typedefs';
121 : }
122 : }
123 : }
|