meta: Introduce TargetKind.importDirective

Fixes https://github.com/dart-lang/sdk/issues/63467

Change-Id: I02f048405878d9ca578f8cbea318c59697f2811b
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/509021
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
This commit is contained in:
Sam Rawlins
2026-06-03 13:34:57 -07:00
committed by Samuel Rawlins
parent ca02d3f1d6
commit 5a94256582
8 changed files with 49 additions and 2 deletions
@@ -783,6 +783,7 @@ class AnnotationVerifier {
FunctionDeclaration(isGetter: true) => kinds.contains(TargetKind.getter),
FunctionDeclaration(isSetter: true) => kinds.contains(TargetKind.setter),
FunctionDeclaration() => kinds.contains(TargetKind.function),
ImportDirective() => kinds.contains(TargetKind.importDirective),
MethodDeclaration(isGetter: true) => kinds.contains(TargetKind.getter),
MethodDeclaration(isSetter: true) => kinds.contains(TargetKind.setter),
MethodDeclaration() => kinds.contains(TargetKind.method),
@@ -351,6 +351,8 @@ class TargetKind {
static const function = TargetKind._('top-level functions', 'function');
static const library = TargetKind._('libraries', 'library');
static const getter = TargetKind._('getters', 'getter');
static const importDirective =
TargetKind._('import directives', 'importDirective');
static const method = TargetKind._('methods', 'method');
static const mixinType = TargetKind._('mixins', 'mixinType');
static const optionalParameter =
@@ -378,6 +380,7 @@ class TargetKind {
function,
library,
getter,
importDirective,
method,
mixinType,
optionalParameter,
+1 -1
View File
@@ -17,7 +17,7 @@ dependencies:
convert: ^3.0.0
crypto: ^3.0.0
glob: ^2.0.0
meta: ^1.18.0
meta: ^1.18.3
package_config: ^2.0.0
path: ^1.9.0
pub_semver: ^2.1.4
@@ -1063,6 +1063,35 @@ int get x => 0;
''');
}
void test_importDirective_exportDirective() async {
await resolveTestCodeWithDiagnostics(r'''
import 'package:meta/meta_meta.dart';
@A()
// [diag.invalidAnnotationTarget][column 2][length 1] The annotation 'A.new' can only be used on import directives.
export 'dart:core';
@Target({TargetKind.importDirective})
class A {
const A();
}
''');
}
void test_importDirective_importDirective() async {
await resolveTestCodeWithDiagnostics(r'''
import 'package:meta/meta_meta.dart';
@A()
import 'dart:core';
@Target({TargetKind.importDirective})
class A {
const A();
}
''');
}
void test_library_class() async {
await resolveTestCodeWithDiagnostics(r'''
import 'package:meta/meta_meta.dart';
@@ -43,6 +43,9 @@ class TargetKind {
static const getter = TargetKind._('getters', 'getter');
static const importDirective =
TargetKind._('import directives', 'importDirective');
static const method = TargetKind._('methods', 'method');
static const mixinType = TargetKind._('mixins', 'mixinType');
@@ -87,6 +90,7 @@ class TargetKind {
function,
library,
getter,
importDirective,
method,
mixinType,
optionalParameter,
+5
View File
@@ -1,3 +1,8 @@
## 1.18.3
- One new TargetKind is introduced: `TargetKind.importDirective`, which
indicates an annotation is valid on an import directive.
## 1.18.2
- Change private types in the public API signatures to `Object`.
+5
View File
@@ -95,6 +95,10 @@ class TargetKind {
/// extension, extension type, or at the top-level of a library.
static const getter = TargetKind._('getters', 'getter');
/// Indicates that an annotation is valid on any import directive.
static const importDirective =
TargetKind._('import directives', 'importDirective');
/// Indicates that an annotation is valid on any method declaration, both
/// instance and static methods, whether it's in a class, enum, mixin,
/// extension, or extension type.
@@ -164,6 +168,7 @@ class TargetKind {
function,
library,
getter,
importDirective,
method,
mixinType,
optionalParameter,
+1 -1
View File
@@ -1,7 +1,7 @@
name: meta
# Note, because version `2.0.0` was mistakenly released,
# the next major version must be `3.x.y`.
version: 1.18.2
version: 1.18.3
description: >-
Annotations used to express developer intentions that can't otherwise be
deduced by statically analyzing source code.