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

Commit

Permalink
Add a new field Library.generatedByComment to support emitting 'gen…
Browse files Browse the repository at this point in the history
…erated by' comments. (#441)

add a Library.generatedByComment field
  • Loading branch information
devoncarew authored Dec 12, 2023
1 parent 10ebf2a commit d7b662a
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 4.9.0

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

## 4.8.0

* Add `Expression.operatorSubtract`
Expand Down
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,7 @@ run from the snapshot instead of from source to avoid problems with deleted
files. These steps must be run without deleting the source files.
```bash
$ dart run build_runner generate-build-script
$ dart compile kernel .dart_tool/build/entrypoint/build.dart
$ dart .dart_tool/build/entrypoint/build.dill build --delete-conflicting-outputs
./tool/regenerate.sh
```
[build_runner]: https://pub.dev/packages/build_runner
6 changes: 6 additions & 0 deletions lib/src/emitter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,12 @@ class DartEmitter extends Object
output.writeln();
}

if (spec.generatedByComment != null) {
output
..writeln('// ${spec.generatedByComment}')
..writeln();
}

if (spec.ignoreForFile.isNotEmpty) {
final ignores = spec.ignoreForFile.toList()..sort();
final lines = ['// ignore_for_file: ${ignores.first}'];
Expand Down
8 changes: 8 additions & 0 deletions lib/src/specs/library.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ abstract class Library
/// Line comments to place at the start of the library.
BuiltList<String> get comments;

/// A comment indicating the tool this library was generated by.
///
/// This is typically of the form `Generated by xxx.`; it should exclude
/// leading line comment characters.
String? get generatedByComment;

/// A list of analysis issues to ignore (`ignore_for_file: ...`).
BuiltList<String> get ignoreForFile;

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

ListBuilder<String> comments = ListBuilder<String>();
String? generatedByComment;
ListBuilder<String> ignoreForFile = ListBuilder<String>();

String? name;
}
21 changes: 21 additions & 0 deletions lib/src/specs/library.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: code_builder
version: 4.8.0
version: 4.9.0
description: >-
A fluent, builder-based library for generating valid Dart code
repository: https://github.com/dart-lang/code_builder
Expand Down
41 changes: 41 additions & 0 deletions test/specs/library_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,23 @@ void main() {
);
});

test('should emit a source file with a generated-by comment', () {
expect(
Library(
(b) => b
..generatedByComment = 'Generated by fooBar.'
..body.add(
Class((b) => b..name = 'Foo'),
),
),
equalsDart(r'''
// Generated by fooBar.
class Foo { }
''', DartEmitter(allocator: Allocator())),
);
});

test('should emit a source file with ignore comments', () {
expect(
Library(
Expand Down Expand Up @@ -93,6 +110,30 @@ void main() {
);
});

test('should emit with line comments, generated-by, and ignore-for-file',
() {
expect(
Library(
(b) => b
..comments.add('Generic copyright statement.')
..generatedByComment = 'Generated by fooBar.'
..ignoreForFile.add('sort_constructors_first')
..body.add(
Class((b) => b..name = 'Foo'),
),
),
equalsDart(r'''
// Generic copyright statement.
// Generated by fooBar.
// ignore_for_file: sort_constructors_first
class Foo { }
''', DartEmitter(allocator: Allocator())),
);
});

test('should emit a source file with manual imports', () {
expect(
Library((b) => b
Expand Down
7 changes: 7 additions & 0 deletions tool/regenerate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
# for details. All rights reserved. Use of this source code is governed by a
# BSD-style license that can be found in the LICENSE file.

dart run build_runner generate-build-script
dart compile kernel .dart_tool/build/entrypoint/build.dart
dart .dart_tool/build/entrypoint/build.dill build --delete-conflicting-outputs

0 comments on commit d7b662a

Please sign in to comment.