Skip to content
This repository was archived by the owner on Apr 8, 2025. It is now read-only.

Commit c36646a

Browse files
committed
Add annotations to libraries
1 parent 4fd85d8 commit c36646a

File tree

5 files changed

+115
-7
lines changed

5 files changed

+115
-7
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Add support for `Expression.nullChecked` to add a null assertion operator.
66
* Add support for creating `mixin`s.
77
* Add `Expression.nullSafeSpread` for the null aware spread operator `...?`.
8+
* A `Library` can now be annotated.
89

910
## 4.0.0
1011

lib/src/emitter.dart

+7
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,13 @@ class DartEmitter extends Object
389389
}
390390
}
391391

392+
if (spec.name != null) {
393+
spec.annotations.forEach((a) => visitAnnotation(a, output));
394+
output.write('library ${spec.name!};');
395+
} else if (spec.annotations.isNotEmpty) {
396+
throw StateError('a library name is required for annotations');
397+
}
398+
392399
final directives = <Directive>[...allocator.imports, ...spec.directives];
393400

394401
if (orderDirectives) {

lib/src/specs/library.dart

+21-2
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,31 @@ import 'package:built_value/built_value.dart';
77
import 'package:meta/meta.dart';
88

99
import '../base.dart';
10+
import '../mixins/annotations.dart';
1011
import '../visitors.dart';
1112
import 'directive.dart';
13+
import 'expression.dart';
1214

1315
part 'library.g.dart';
1416

1517
@immutable
16-
abstract class Library implements Built<Library, LibraryBuilder>, Spec {
18+
abstract class Library
19+
with HasAnnotations
20+
implements Built<Library, LibraryBuilder>, Spec {
1721
factory Library([void Function(LibraryBuilder) updates]) = _$Library;
1822
Library._();
1923

24+
@override
25+
BuiltList<Expression> get annotations;
26+
2027
BuiltList<Directive> get directives;
2128
BuiltList<Spec> get body;
2229

30+
/// Name of the library.
31+
///
32+
/// May be `null` when no [annotations] are specified.
33+
String? get name;
34+
2335
@override
2436
R accept<R>(
2537
SpecVisitor<R> visitor, [
@@ -28,10 +40,17 @@ abstract class Library implements Built<Library, LibraryBuilder>, Spec {
2840
visitor.visitLibrary(this, context);
2941
}
3042

31-
abstract class LibraryBuilder implements Builder<Library, LibraryBuilder> {
43+
abstract class LibraryBuilder
44+
with HasAnnotationsBuilder
45+
implements Builder<Library, LibraryBuilder> {
3246
factory LibraryBuilder() = _$LibraryBuilder;
3347
LibraryBuilder._();
3448

49+
@override
50+
ListBuilder<Expression> annotations = ListBuilder<Expression>();
51+
3552
ListBuilder<Spec> body = ListBuilder<Spec>();
3653
ListBuilder<Directive> directives = ListBuilder<Directive>();
54+
55+
String? name;
3756
}

lib/src/specs/library.g.dart

+55-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/specs/library_test.dart

+31
Original file line numberDiff line numberDiff line change
@@ -137,5 +137,36 @@ void main() {
137137
''', DartEmitter()),
138138
);
139139
});
140+
141+
test('should emit a source file with annotations', () {
142+
expect(
143+
Library(
144+
(b) => b
145+
..name = 'js_interop'
146+
..annotations.add(
147+
refer('JS', 'package:js/js.dart').call([]),
148+
),
149+
),
150+
equalsDart(r'''
151+
@JS()
152+
library js_interop;
153+
import 'package:js/js.dart';
154+
''', DartEmitter(allocator: Allocator())),
155+
);
156+
});
157+
158+
test('should error on unnamed library with annotations', () {
159+
expect(
160+
() {
161+
Library(
162+
(b) => b
163+
..annotations.add(
164+
refer('JS', 'package:js/js.dart').call([]),
165+
),
166+
).accept(DartEmitter());
167+
},
168+
throwsStateError,
169+
);
170+
});
140171
});
141172
}

0 commit comments

Comments
 (0)