Skip to content

Commit

Permalink
Add support for an ignore_for_file option to combining_builder. (d…
Browse files Browse the repository at this point in the history
  • Loading branch information
kevmoo authored Feb 24, 2020
1 parent 3d7c1d9 commit 3bcd6db
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 4 deletions.
7 changes: 7 additions & 0 deletions pkgs/source_gen/example_usage/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,10 @@ targets:
source_gen_example|property_product:
generate_for:
- lib/*.dart
# The end-user of a builder which applies "source_gen|combining_builder"
# may configure the builder to ignore specific lints for their project
source_gen|combining_builder:
options:
ignore_for_file:
- lint_a
- lint_b
2 changes: 2 additions & 0 deletions pkgs/source_gen/example_usage/lib/library_source.g.dart

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

2 changes: 2 additions & 0 deletions pkgs/source_gen/source_gen/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## 0.9.5-dev

* Add support for an `ignore_for_file` option to `combining_builder`.

* Expand documentation for `GeneratorForAnnotation` to make it clear that it
only targets elements at the top level of a library.

Expand Down
24 changes: 24 additions & 0 deletions pkgs/source_gen/source_gen/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,30 @@ builders:
applies_builders: ["source_gen|combining_builder"]
```
### Configuring `combining_builder` `ignore_for_file`

Sometimes generated code does not support all of the
[lints](https://dart-lang.github.io/linter/) specified in the target package.
When using a `Builder` based on `package:source_gen` which applies
`combining_builder`, set the `ignore_for_file` option to a list of lints you
wish to be ignored in all generated libraries.

_Example `build.yaml` configuration:_

```yaml
targets:
$default":
builders:
source_gen|combining_builder:
options:
ignore_for_file:
- lint_alpha
- lint_beta
```

If you provide a builder that uses `SharedPartBuilder` and `combining_builder`,
you should document this feature for your users.

## FAQ

### What is the difference between `source_gen` and [build][]?
Expand Down
24 changes: 20 additions & 4 deletions pkgs/source_gen/source_gen/lib/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,15 @@ const _partFiles = '.g.part';
Builder combiningBuilder([BuilderOptions options]) {
final optionsMap = Map<String, dynamic>.from(options?.config ?? {});

final includePartName = optionsMap.remove('include_part_name') as bool;
final ignoreForFile = Set<String>.from(
optionsMap.remove('ignore_for_file') as List ?? <String>[],
);

final builder = CombiningBuilder(
includePartName: optionsMap.remove('include_part_name') as bool);
includePartName: includePartName,
ignoreForFile: ignoreForFile,
);

if (optionsMap.isNotEmpty) {
if (log == null) {
Expand All @@ -49,6 +56,8 @@ PostProcessBuilder partCleanup(BuilderOptions options) =>
class CombiningBuilder implements Builder {
final bool _includePartName;

final Set<String> _ignoreForFile;

@override
Map<String, List<String>> get buildExtensions => const {
'.dart': [_outputExtensions]
Expand All @@ -59,8 +68,11 @@ class CombiningBuilder implements Builder {
/// If [includePartName] is `true`, the name of each source part file
/// is output as a comment before its content. This can be useful when
/// debugging build issues.
const CombiningBuilder({bool includePartName = false})
: _includePartName = includePartName ?? false;
const CombiningBuilder({
bool includePartName = false,
Set<String> ignoreForFile,
}) : _includePartName = includePartName ?? false,
_ignoreForFile = ignoreForFile ?? const <String>{};

@override
Future build(BuildStep buildStep) async {
Expand Down Expand Up @@ -100,9 +112,13 @@ class CombiningBuilder implements Builder {
if (assets.isEmpty) return;
final partOf =
nameOfPartial(await buildStep.inputLibrary, buildStep.inputId);

final ignoreForFile = _ignoreForFile.isEmpty
? ''
: '\n// ignore_for_file: ${_ignoreForFile.join(', ')}\n';
final output = '''
$defaultFileHeader
$ignoreForFile
part of $partOf;
$assets
Expand Down
27 changes: 27 additions & 0 deletions pkgs/source_gen/source_gen/test/builder_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,33 @@ void main() {
'// Part: a.only_whitespace.g.part\n\n')),
});
});

test('includes ignore for file if enabled', () async {
await testBuilder(
const CombiningBuilder(ignoreForFile: {
'lines_longer_than_80_chars',
'prefer_expression_function_bodies',
}),
{
'$_pkgName|lib/a.dart': 'library a; part "a.g.dart";',
'$_pkgName|lib/a.foo.g.part': '\n\nfoo generated content\n',
'$_pkgName|lib/a.only_whitespace.g.part': '\n\n\t \n \n',
'$_pkgName|lib/a.bar.g.part': '\nbar generated content',
},
generateFor: {'$_pkgName|lib/a.dart'},
outputs: {
'$_pkgName|lib/a.g.dart': decodedMatches(endsWith(r'''
// ignore_for_file: lines_longer_than_80_chars, prefer_expression_function_bodies
part of a;
bar generated content
foo generated content
''')),
},
);
});
});

test('can skip formatting with a trivial lambda', () async {
Expand Down

0 comments on commit 3bcd6db

Please sign in to comment.