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

Support annotations on unnamed libraries; 4.9.0 #440

Merged
merged 2 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 4.9.0

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

## 4.8.0

Expand Down
11 changes: 7 additions & 4 deletions lib/src/emitter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -512,13 +512,16 @@ class DartEmitter extends Object
}
}

for (var a in spec.annotations) {
visitAnnotation(a, output);
}
if (spec.name != null) {
for (var a in spec.annotations) {
visitAnnotation(a, output);
}
output.write('library ${spec.name!};');
} else if (spec.annotations.isNotEmpty) {
throw StateError('a library name is required for annotations');
// An explicit _unnamed_ library directive is only required if there are
// annotations or doc comments on the library (though doc comments are not
// currently supported in code_builder).
output.write('library;');
}

final directives = <Directive>[...allocator.imports, ...spec.directives];
Expand Down
22 changes: 12 additions & 10 deletions test/specs/library_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -277,17 +277,19 @@ void main() {
);
});

test('should error on unnamed library with annotations', () {
test('should emit an unnamed library source file with annotations', () {
expect(
() {
Library(
(b) => b
..annotations.add(
refer('JS', 'package:js/js.dart').call([]),
),
).accept(DartEmitter());
},
throwsStateError,
Library(
(b) => b
..annotations.add(
refer('JS', 'package:js/js.dart').call([]),
),
),
equalsDart(r'''
@JS()
library;
import 'package:js/js.dart';
''', DartEmitter(allocator: Allocator())),
);
});
});
Expand Down