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

Commit d7b662a

Browse files
authored
Add a new field Library.generatedByComment to support emitting 'generated by' comments. (#441)
add a Library.generatedByComment field
1 parent 10ebf2a commit d7b662a

File tree

8 files changed

+89
-4
lines changed

8 files changed

+89
-4
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 4.9.0
2+
3+
* Add `Library.generatedByComment` to support emitting 'generated by' comments.
4+
15
## 4.8.0
26

37
* Add `Expression.operatorSubtract`

README.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,7 @@ run from the snapshot instead of from source to avoid problems with deleted
103103
files. These steps must be run without deleting the source files.
104104
105105
```bash
106-
$ dart run build_runner generate-build-script
107-
$ dart compile kernel .dart_tool/build/entrypoint/build.dart
108-
$ dart .dart_tool/build/entrypoint/build.dill build --delete-conflicting-outputs
106+
./tool/regenerate.sh
109107
```
110108
111109
[build_runner]: https://pub.dev/packages/build_runner

lib/src/emitter.dart

+6
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,12 @@ class DartEmitter extends Object
483483
output.writeln();
484484
}
485485

486+
if (spec.generatedByComment != null) {
487+
output
488+
..writeln('// ${spec.generatedByComment}')
489+
..writeln();
490+
}
491+
486492
if (spec.ignoreForFile.isNotEmpty) {
487493
final ignores = spec.ignoreForFile.toList()..sort();
488494
final lines = ['// ignore_for_file: ${ignores.first}'];

lib/src/specs/library.dart

+8
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ abstract class Library
3030
/// Line comments to place at the start of the library.
3131
BuiltList<String> get comments;
3232

33+
/// A comment indicating the tool this library was generated by.
34+
///
35+
/// This is typically of the form `Generated by xxx.`; it should exclude
36+
/// leading line comment characters.
37+
String? get generatedByComment;
38+
3339
/// A list of analysis issues to ignore (`ignore_for_file: ...`).
3440
BuiltList<String> get ignoreForFile;
3541

@@ -59,6 +65,8 @@ abstract class LibraryBuilder
5965
ListBuilder<Directive> directives = ListBuilder<Directive>();
6066

6167
ListBuilder<String> comments = ListBuilder<String>();
68+
String? generatedByComment;
6269
ListBuilder<String> ignoreForFile = ListBuilder<String>();
70+
6371
String? name;
6472
}

lib/src/specs/library.g.dart

+21
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.8.0
2+
version: 4.9.0
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

+41
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,23 @@ void main() {
5353
);
5454
});
5555

56+
test('should emit a source file with a generated-by comment', () {
57+
expect(
58+
Library(
59+
(b) => b
60+
..generatedByComment = 'Generated by fooBar.'
61+
..body.add(
62+
Class((b) => b..name = 'Foo'),
63+
),
64+
),
65+
equalsDart(r'''
66+
// Generated by fooBar.
67+
68+
class Foo { }
69+
''', DartEmitter(allocator: Allocator())),
70+
);
71+
});
72+
5673
test('should emit a source file with ignore comments', () {
5774
expect(
5875
Library(
@@ -93,6 +110,30 @@ void main() {
93110
);
94111
});
95112

113+
test('should emit with line comments, generated-by, and ignore-for-file',
114+
() {
115+
expect(
116+
Library(
117+
(b) => b
118+
..comments.add('Generic copyright statement.')
119+
..generatedByComment = 'Generated by fooBar.'
120+
..ignoreForFile.add('sort_constructors_first')
121+
..body.add(
122+
Class((b) => b..name = 'Foo'),
123+
),
124+
),
125+
equalsDart(r'''
126+
// Generic copyright statement.
127+
128+
// Generated by fooBar.
129+
130+
// ignore_for_file: sort_constructors_first
131+
132+
class Foo { }
133+
''', DartEmitter(allocator: Allocator())),
134+
);
135+
});
136+
96137
test('should emit a source file with manual imports', () {
97138
expect(
98139
Library((b) => b

tool/regenerate.sh

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
# for details. All rights reserved. Use of this source code is governed by a
3+
# BSD-style license that can be found in the LICENSE file.
4+
5+
dart run build_runner generate-build-script
6+
dart compile kernel .dart_tool/build/entrypoint/build.dart
7+
dart .dart_tool/build/entrypoint/build.dill build --delete-conflicting-outputs

0 commit comments

Comments
 (0)