diff --git a/CHANGELOG.md b/CHANGELOG.md index 4991b56..710bf0f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## 4.11.0-wip + +* Merge `no_leading_underscores_for_library_prefixes` from the simplePrefixing + allocator with other file ignores. + ## 4.10.0 * Add `Library.docs` to support emitting doc comments on libraries. diff --git a/lib/src/emitter.dart b/lib/src/emitter.dart index 12109cc..b17b6d8 100644 --- a/lib/src/emitter.dart +++ b/lib/src/emitter.dart @@ -489,8 +489,28 @@ class DartEmitter extends Object ..writeln(); } - if (spec.ignoreForFile.isNotEmpty) { - final ignores = spec.ignoreForFile.toList()..sort(); + // Process the body and annotations first in order to prime the allocators. + final body = StringBuffer(); + for (final spec in spec.body) { + spec.accept(this, body); + if (spec is Method && _isLambdaMethod(spec)) { + body.write(';'); + } + } + final annotations = StringBuffer(); + for (var a in spec.annotations) { + visitAnnotation(a, annotations); + } + + final directives = [...allocator.imports, ...spec.directives]; + final ignores = spec.ignoreForFile.toList(); + + if (directives.any((d) => d.as?.startsWith('_') ?? false)) { + ignores.add('no_leading_underscores_for_library_prefixes'); + } + + if (ignores.isNotEmpty) { + ignores.sort(); final lines = ['// ignore_for_file: ${ignores.first}']; for (var ignore in ignores.skip(1)) { if (lines.last.length + 2 + ignore.length > 80) { @@ -503,19 +523,9 @@ class DartEmitter extends Object output.writeln(); } - // Process the body first in order to prime the allocators. - final body = StringBuffer(); - for (final spec in spec.body) { - spec.accept(this, body); - if (spec is Method && _isLambdaMethod(spec)) { - body.write(';'); - } - } - spec.docs.forEach(output.writeln); - for (var a in spec.annotations) { - visitAnnotation(a, output); - } + output.write(annotations); + if (spec.name != null) { output.write('library ${spec.name!};'); } else if (spec.annotations.isNotEmpty || spec.docs.isNotEmpty) { @@ -524,17 +534,11 @@ class DartEmitter extends Object output.write('library;'); } - final directives = [...allocator.imports, ...spec.directives]; - if (orderDirectives) { directives.sort(); } Directive? previous; - if (directives.any((d) => d.as?.startsWith('_') ?? false)) { - output.writeln( - '// ignore_for_file: no_leading_underscores_for_library_prefixes'); - } for (final directive in directives) { if (_newLineBetween(orderDirectives, previous, directive)) { // Note: dartfmt handles creating new lines between directives. diff --git a/pubspec.yaml b/pubspec.yaml index f76a6c9..a1376f3 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: code_builder -version: 4.10.0 +version: 4.11.0-wip description: >- A fluent, builder-based library for generating valid Dart code repository: https://github.com/dart-lang/code_builder diff --git a/test/directive_test.dart b/test/directive_test.dart index 0aee6ae..04b3ce7 100644 --- a/test/directive_test.dart +++ b/test/directive_test.dart @@ -39,6 +39,7 @@ void main() { library, equalsDart(r''' // ignore_for_file: no_leading_underscores_for_library_prefixes + import '../relative.dart' as _i1; import 'package:foo/foo.dart' as _i2; import 'package:foo/bar.dart' as _i3; @@ -59,6 +60,7 @@ void main() { library, equalsDart(r''' // ignore_for_file: no_leading_underscores_for_library_prefixes + import 'dart:collection' as _i4; import 'package:foo/bar.dart' as _i3; diff --git a/test/specs/library_test.dart b/test/specs/library_test.dart index 8ea4c58..a5c93cf 100644 --- a/test/specs/library_test.dart +++ b/test/specs/library_test.dart @@ -225,6 +225,7 @@ void main() { ..assignment = Code.scope((a) => '${a($LinkedHashMap)}()')))), equalsDart(r''' // ignore_for_file: no_leading_underscores_for_library_prefixes + import 'dart:collection' as _i1; final test = _i1.LinkedHashMap(); @@ -309,5 +310,28 @@ void main() { '''), ); }); + + test('should emit a source file with library allocation + prefixing', () { + expect( + Library((b) => b + ..docs.add( + '/// My favorite library.', + ) + ..body.add(Field((b) => b + ..name = 'test' + ..modifier = FieldModifier.final$ + ..assignment = Code.scope((a) => '${a($LinkedHashMap)}()')))), + equalsDart(r''' + // ignore_for_file: no_leading_underscores_for_library_prefixes + + /// My favorite library. + library; + + import 'dart:collection' as _i1; + + final test = _i1.LinkedHashMap(); + ''', DartEmitter(allocator: Allocator.simplePrefixing())), + ); + }, skip: true); }); }