diff --git a/CHANGELOG.md b/CHANGELOG.md index 4da1ef88..7e25f9c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.7.3 + +* Allow null and empty outputs form `GeneratorForAnnotation`. + ## 0.7.2+1 * Allow `package:build` version 0.11.0 diff --git a/lib/src/generator_for_annotation.dart b/lib/src/generator_for_annotation.dart index 8c2a250b..23eb2cad 100644 --- a/lib/src/generator_for_annotation.dart +++ b/lib/src/generator_for_annotation.dart @@ -39,7 +39,7 @@ abstract class GeneratorForAnnotation extends Generator { var allOutput = await Future.wait(elements.map((e) async => await generateForAnnotatedElement(e.element, e.annotation, buildStep))); // TODO interleave comments indicating which element produced the output? - return allOutput.join('\n'); + return allOutput.where((o) => o != null && o.isNotEmpty).join('\n'); } /// Override to return source code to generate for [element]. diff --git a/pubspec.yaml b/pubspec.yaml index c8e870b5..4077f981 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: source_gen -version: 0.7.2+1 +version: 0.7.3 author: Dart Team description: Automated source code generation for Dart. homepage: https://github.com/dart-lang/source_gen diff --git a/test/generator_for_annotation_test.dart b/test/generator_for_annotation_test.dart new file mode 100644 index 00000000..5fe8ec62 --- /dev/null +++ b/test/generator_for_annotation_test.dart @@ -0,0 +1,32 @@ +// Copyright (c) 2017, 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. + +import 'dart:async'; + +import 'package:analyzer/dart/element/element.dart'; +import 'package:build/build.dart'; +import 'package:build_test/build_test.dart'; +import 'package:test/test.dart'; + +import 'package:source_gen/source_gen.dart'; + +void main() { + test('Skips output if per-annotation output is empty', () async { + final generator = new NoOutput(); + var builder = new LibraryBuilder(generator); + await testBuilder(builder, { + 'a|lib/file.dart': ''' + @deprecated + final foo = 'foo'; + ''' + }, outputs: {}); + }); +} + +class NoOutput extends GeneratorForAnnotation { + @override + FutureOr generateForAnnotatedElement( + Element element, ConstantReader annotation, BuildStep buildStep) => + null; +}