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

Commit 9ab2a9e

Browse files
authored
feat: support docs for libraries (#444)
Signed-off-by: Nikolas Rimikis <[email protected]> Co-authored-by: Nikolas Rimikis <[email protected]>
1 parent 4d097c4 commit 9ab2a9e

File tree

7 files changed

+59
-7
lines changed

7 files changed

+59
-7
lines changed

AUTHORS

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
#
44
# Name/Organization <email address>
55

6-
Google Inc.
6+
Nikolas Rimikis <[email protected]>
7+
Google Inc.

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 4.10.0-wip
2+
3+
* Add `Library.docs` to support emitting doc comments on libraries.
4+
15
## 4.9.0
26

37
* Add `Library.generatedByComment` to support emitting 'generated by' comments.

lib/src/emitter.dart

+3-3
Original file line numberDiff line numberDiff line change
@@ -512,15 +512,15 @@ class DartEmitter extends Object
512512
}
513513
}
514514

515+
spec.docs.forEach(output.writeln);
515516
for (var a in spec.annotations) {
516517
visitAnnotation(a, output);
517518
}
518519
if (spec.name != null) {
519520
output.write('library ${spec.name!};');
520-
} else if (spec.annotations.isNotEmpty) {
521+
} else if (spec.annotations.isNotEmpty || spec.docs.isNotEmpty) {
521522
// 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).
523+
// annotations or doc comments on the library.
524524
output.write('library;');
525525
}
526526

lib/src/specs/library.dart

+9-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'package:meta/meta.dart';
88

99
import '../base.dart';
1010
import '../mixins/annotations.dart';
11+
import '../mixins/dartdoc.dart';
1112
import '../visitors.dart';
1213
import 'directive.dart';
1314
import 'expression.dart';
@@ -16,14 +17,17 @@ part 'library.g.dart';
1617

1718
@immutable
1819
abstract class Library
19-
with HasAnnotations
20+
with HasAnnotations, HasDartDocs
2021
implements Built<Library, LibraryBuilder>, Spec {
2122
factory Library([void Function(LibraryBuilder) updates]) = _$Library;
2223
Library._();
2324

2425
@override
2526
BuiltList<Expression> get annotations;
2627

28+
@override
29+
BuiltList<String> get docs;
30+
2731
BuiltList<Directive> get directives;
2832
BuiltList<Spec> get body;
2933

@@ -53,14 +57,17 @@ abstract class Library
5357
}
5458

5559
abstract class LibraryBuilder
56-
with HasAnnotationsBuilder
60+
with HasAnnotationsBuilder, HasDartDocsBuilder
5761
implements Builder<Library, LibraryBuilder> {
5862
factory LibraryBuilder() = _$LibraryBuilder;
5963
LibraryBuilder._();
6064

6165
@override
6266
ListBuilder<Expression> annotations = ListBuilder<Expression>();
6367

68+
@override
69+
ListBuilder<String> docs = ListBuilder<String>();
70+
6471
ListBuilder<Spec> body = ListBuilder<Spec>();
6572
ListBuilder<Directive> directives = ListBuilder<Directive>();
6673

lib/src/specs/library.g.dart

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

pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: code_builder
2-
version: 4.9.0
2+
version: 4.10.0-wip
33
description: >-
44
A fluent, builder-based library for generating valid Dart code
55
repository: https://github.com/dart-lang/code_builder

test/specs/library_test.dart

+17
Original file line numberDiff line numberDiff line change
@@ -292,5 +292,22 @@ void main() {
292292
''', DartEmitter(allocator: Allocator())),
293293
);
294294
});
295+
296+
test('should emit an unnamed library source file with documentation', () {
297+
expect(
298+
Library(
299+
(b) => b
300+
..docs.addAll(
301+
const [
302+
'/// My favorite library.',
303+
],
304+
),
305+
),
306+
equalsDart(r'''
307+
/// My favorite library.
308+
library;
309+
'''),
310+
);
311+
});
295312
});
296313
}

0 commit comments

Comments
 (0)