Skip to content

Commit bfc9960

Browse files
authored
Handle empty outputs in GenerateForAnnotation (#286)
Fixes #285 Previously if the library had any matching annotations we would always output something - either the String "null" or empty lines. Skip outputs that are null or empty. Add a regression test.
1 parent 5c28bd1 commit bfc9960

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.7.3
2+
3+
* Allow null and empty outputs form `GeneratorForAnnotation`.
4+
15
## 0.7.2+1
26

37
* Allow `package:build` version 0.11.0

lib/src/generator_for_annotation.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ abstract class GeneratorForAnnotation<T> extends Generator {
3939
var allOutput = await Future.wait(elements.map((e) async =>
4040
await generateForAnnotatedElement(e.element, e.annotation, buildStep)));
4141
// TODO interleave comments indicating which element produced the output?
42-
return allOutput.join('\n');
42+
return allOutput.where((o) => o != null && o.isNotEmpty).join('\n');
4343
}
4444

4545
/// Override to return source code to generate for [element].

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: source_gen
2-
version: 0.7.2+1
2+
version: 0.7.3
33
author: Dart Team <[email protected]>
44
description: Automated source code generation for Dart.
55
homepage: https://github.com/dart-lang/source_gen
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) 2017, 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+
import 'dart:async';
6+
7+
import 'package:analyzer/dart/element/element.dart';
8+
import 'package:build/build.dart';
9+
import 'package:build_test/build_test.dart';
10+
import 'package:test/test.dart';
11+
12+
import 'package:source_gen/source_gen.dart';
13+
14+
void main() {
15+
test('Skips output if per-annotation output is empty', () async {
16+
final generator = new NoOutput();
17+
var builder = new LibraryBuilder(generator);
18+
await testBuilder(builder, {
19+
'a|lib/file.dart': '''
20+
@deprecated
21+
final foo = 'foo';
22+
'''
23+
}, outputs: {});
24+
});
25+
}
26+
27+
class NoOutput extends GeneratorForAnnotation<Deprecated> {
28+
@override
29+
FutureOr<String> generateForAnnotatedElement(
30+
Element element, ConstantReader annotation, BuildStep buildStep) =>
31+
null;
32+
}

0 commit comments

Comments
 (0)