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

Commit 4d097c4

Browse files
authored
Support annotations on unnamed libraries; 4.9.0 (#440)
1 parent d7b662a commit 4d097c4

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## 4.9.0
22

33
* Add `Library.generatedByComment` to support emitting 'generated by' comments.
4+
* Support emitting an unnamed library with annotations.
45

56
## 4.8.0
67

lib/src/emitter.dart

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -512,13 +512,16 @@ class DartEmitter extends Object
512512
}
513513
}
514514

515+
for (var a in spec.annotations) {
516+
visitAnnotation(a, output);
517+
}
515518
if (spec.name != null) {
516-
for (var a in spec.annotations) {
517-
visitAnnotation(a, output);
518-
}
519519
output.write('library ${spec.name!};');
520520
} else if (spec.annotations.isNotEmpty) {
521-
throw StateError('a library name is required for annotations');
521+
// An explicit _unnamed_ library directive is only required if there are
522+
// annotations or doc comments on the library (though doc comments are not
523+
// currently supported in code_builder).
524+
output.write('library;');
522525
}
523526

524527
final directives = <Directive>[...allocator.imports, ...spec.directives];

test/specs/library_test.dart

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -277,17 +277,19 @@ void main() {
277277
);
278278
});
279279

280-
test('should error on unnamed library with annotations', () {
280+
test('should emit an unnamed library source file with annotations', () {
281281
expect(
282-
() {
283-
Library(
284-
(b) => b
285-
..annotations.add(
286-
refer('JS', 'package:js/js.dart').call([]),
287-
),
288-
).accept(DartEmitter());
289-
},
290-
throwsStateError,
282+
Library(
283+
(b) => b
284+
..annotations.add(
285+
refer('JS', 'package:js/js.dart').call([]),
286+
),
287+
),
288+
equalsDart(r'''
289+
@JS()
290+
library;
291+
import 'package:js/js.dart';
292+
''', DartEmitter(allocator: Allocator())),
291293
);
292294
});
293295
});

0 commit comments

Comments
 (0)