Skip to content
This repository has been archived by the owner on Nov 1, 2024. It is now read-only.

Commit

Permalink
Support annotations on unnamed libraries; 4.9.0 (#440)
Browse files Browse the repository at this point in the history
  • Loading branch information
srawlins authored Dec 13, 2023
1 parent d7b662a commit 4d097c4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
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

0 comments on commit 4d097c4

Please sign in to comment.