It's common practice for an annotation without parameters to be defined as a private class which is exposed as a public top-level variable:
const myAnnotation = _MyAnnotation();
class _MyAnnotation() {
const _MyAnnotation();
}
import 'my_annotation.dart';
@myAnnotation
class MyClass {}
The new @Target annotation is a fantastic idea for enforcing correct usage of annotations. Unfortunately, the hints produced by this annotation (invalid_annotation_target) only occur when the annotation is used as a constructor and not as a top-level variable:
import 'package:meta/meta_meta.dart';
const myAnnotation = MyAnnotation();
@Target({TargetKind.classType})
class MyAnnotation() {
const MyAnnotation();
}
import 'my_annotation.dart';
@myAnnotation // Hint is *NOT* produced for this incorrect target.
void f() {}
@MyAnnotation()
// ^^^^^^^^^^
// The annotation 'MyAnnotation' can only be used on classes [invalid_annotation_target]
void g() {}
I'd expect the invalid_annotation_target hint to be produced, regardless of how the annotation is consumed.
Sorry for the bug, but thanks for letting us know! This should be fixed by https://dart-review.googlesource.com/c/sdk/+/168126
Thank you for the quick fix!
Most helpful comment
Sorry for the bug, but thanks for letting us know! This should be fixed by https://dart-review.googlesource.com/c/sdk/+/168126