diff --git a/AUTHORS b/AUTHORS index 609e0dc..a904f0c 100644 --- a/AUTHORS +++ b/AUTHORS @@ -3,4 +3,5 @@ # # Name/Organization -Google Inc. \ No newline at end of file +Nikolas Rimikis +Google Inc. diff --git a/CHANGELOG.md b/CHANGELOG.md index 1fc26c7..8e51fab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 4.10.0-wip + +* Add `Library.docs` to support emitting doc comments on libraries. + ## 4.9.0 * Add `Library.generatedByComment` to support emitting 'generated by' comments. diff --git a/lib/src/emitter.dart b/lib/src/emitter.dart index 99c91e4..12109cc 100644 --- a/lib/src/emitter.dart +++ b/lib/src/emitter.dart @@ -512,15 +512,15 @@ class DartEmitter extends Object } } + spec.docs.forEach(output.writeln); for (var a in spec.annotations) { visitAnnotation(a, output); } if (spec.name != null) { output.write('library ${spec.name!};'); - } else if (spec.annotations.isNotEmpty) { + } else if (spec.annotations.isNotEmpty || spec.docs.isNotEmpty) { // 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). + // annotations or doc comments on the library. output.write('library;'); } diff --git a/lib/src/specs/library.dart b/lib/src/specs/library.dart index 69a6dbf..bbfbf3f 100644 --- a/lib/src/specs/library.dart +++ b/lib/src/specs/library.dart @@ -8,6 +8,7 @@ import 'package:meta/meta.dart'; import '../base.dart'; import '../mixins/annotations.dart'; +import '../mixins/dartdoc.dart'; import '../visitors.dart'; import 'directive.dart'; import 'expression.dart'; @@ -16,7 +17,7 @@ part 'library.g.dart'; @immutable abstract class Library - with HasAnnotations + with HasAnnotations, HasDartDocs implements Built, Spec { factory Library([void Function(LibraryBuilder) updates]) = _$Library; Library._(); @@ -24,6 +25,9 @@ abstract class Library @override BuiltList get annotations; + @override + BuiltList get docs; + BuiltList get directives; BuiltList get body; @@ -53,7 +57,7 @@ abstract class Library } abstract class LibraryBuilder - with HasAnnotationsBuilder + with HasAnnotationsBuilder, HasDartDocsBuilder implements Builder { factory LibraryBuilder() = _$LibraryBuilder; LibraryBuilder._(); @@ -61,6 +65,9 @@ abstract class LibraryBuilder @override ListBuilder annotations = ListBuilder(); + @override + ListBuilder docs = ListBuilder(); + ListBuilder body = ListBuilder(); ListBuilder directives = ListBuilder(); diff --git a/lib/src/specs/library.g.dart b/lib/src/specs/library.g.dart index 1858cd6..63cfc20 100644 --- a/lib/src/specs/library.g.dart +++ b/lib/src/specs/library.g.dart @@ -10,6 +10,8 @@ class _$Library extends Library { @override final BuiltList annotations; @override + final BuiltList docs; + @override final BuiltList directives; @override final BuiltList body; @@ -27,6 +29,7 @@ class _$Library extends Library { _$Library._( {required this.annotations, + required this.docs, required this.directives, required this.body, required this.comments, @@ -36,6 +39,7 @@ class _$Library extends Library { : super._() { BuiltValueNullFieldError.checkNotNull( annotations, r'Library', 'annotations'); + BuiltValueNullFieldError.checkNotNull(docs, r'Library', 'docs'); BuiltValueNullFieldError.checkNotNull(directives, r'Library', 'directives'); BuiltValueNullFieldError.checkNotNull(body, r'Library', 'body'); BuiltValueNullFieldError.checkNotNull(comments, r'Library', 'comments'); @@ -55,6 +59,7 @@ class _$Library extends Library { if (identical(other, this)) return true; return other is Library && annotations == other.annotations && + docs == other.docs && directives == other.directives && body == other.body && comments == other.comments && @@ -67,6 +72,7 @@ class _$Library extends Library { int get hashCode { var _$hash = 0; _$hash = $jc(_$hash, annotations.hashCode); + _$hash = $jc(_$hash, docs.hashCode); _$hash = $jc(_$hash, directives.hashCode); _$hash = $jc(_$hash, body.hashCode); _$hash = $jc(_$hash, comments.hashCode); @@ -81,6 +87,7 @@ class _$Library extends Library { String toString() { return (newBuiltValueToStringHelper(r'Library') ..add('annotations', annotations) + ..add('docs', docs) ..add('directives', directives) ..add('body', body) ..add('comments', comments) @@ -106,6 +113,18 @@ class _$LibraryBuilder extends LibraryBuilder { super.annotations = annotations; } + @override + ListBuilder get docs { + _$this; + return super.docs; + } + + @override + set docs(ListBuilder docs) { + _$this; + super.docs = docs; + } + @override ListBuilder get directives { _$this; @@ -184,6 +203,7 @@ class _$LibraryBuilder extends LibraryBuilder { final $v = _$v; if ($v != null) { super.annotations = $v.annotations.toBuilder(); + super.docs = $v.docs.toBuilder(); super.directives = $v.directives.toBuilder(); super.body = $v.body.toBuilder(); super.comments = $v.comments.toBuilder(); @@ -215,6 +235,7 @@ class _$LibraryBuilder extends LibraryBuilder { _$result = _$v ?? new _$Library._( annotations: annotations.build(), + docs: docs.build(), directives: directives.build(), body: body.build(), comments: comments.build(), @@ -226,6 +247,8 @@ class _$LibraryBuilder extends LibraryBuilder { try { _$failedField = 'annotations'; annotations.build(); + _$failedField = 'docs'; + docs.build(); _$failedField = 'directives'; directives.build(); _$failedField = 'body'; diff --git a/pubspec.yaml b/pubspec.yaml index f088adf..03f2eda 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: code_builder -version: 4.9.0 +version: 4.10.0-wip description: >- A fluent, builder-based library for generating valid Dart code repository: https://github.com/dart-lang/code_builder diff --git a/test/specs/library_test.dart b/test/specs/library_test.dart index 6b7fdea..8ea4c58 100644 --- a/test/specs/library_test.dart +++ b/test/specs/library_test.dart @@ -292,5 +292,22 @@ void main() { ''', DartEmitter(allocator: Allocator())), ); }); + + test('should emit an unnamed library source file with documentation', () { + expect( + Library( + (b) => b + ..docs.addAll( + const [ + '/// My favorite library.', + ], + ), + ), + equalsDart(r''' + /// My favorite library. + library; + '''), + ); + }); }); }