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

feat: merge simplePrefixing allocator ignore with other file ignores #448

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
44 changes: 24 additions & 20 deletions lib/src/emitter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <Directive>[...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) {
Expand All @@ -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) {
Expand All @@ -524,17 +534,11 @@ class DartEmitter extends Object
output.write('library;');
}

final directives = <Directive>[...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.
Expand Down
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.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
Expand Down
2 changes: 2 additions & 0 deletions test/directive_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
24 changes: 24 additions & 0 deletions test/specs/library_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
});
}