Skip to content

Commit 319be98

Browse files
authored
code_builder: update deps, prepare release (#2160)
- reformat code for latest min SDK - fix leaked types - add and fix a lint
1 parent 24707e0 commit 319be98

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+3424
-2395
lines changed

.github/workflows/code_builder.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ jobs:
5151
fail-fast: false
5252
matrix:
5353
os: [ubuntu-latest]
54-
sdk: [3.6.0, dev]
54+
sdk: [3.7.0, dev]
5555
steps:
5656
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
5757
- uses: dart-lang/setup-dart@e51d8e571e22473a2ddebf0ef8a2123f0ab2c02c

pkgs/code_builder/CHANGELOG.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1-
## 4.10.2-wip
1+
## 4.11.0
22

3+
* Export `SpecVisitor`, `ClosureExpression`, `LiteralMapExpression`
4+
`LiteralRecordExpression`, `LiteralSetExpression` types.
35
* Support `Expression.newInstanceNamed` with empty name
46
* Consistently add blank lines between `=>` in class-like definitions.
57
* Fixed bug: Fields declared with `static` and `external` now produce code with
68
correct order
7-
* Upgrade `dart_style` and `source_gen` to remove `package:macros` dependency.
8-
* Require Dart `^3.6.0` due to the upgrades.
9+
* Require `built_collection: ^5.1.1`
10+
* Require `built_value: ^8.10.1`
11+
* Require `collection: ^1.19.0`
12+
* Require `matcher: ^0.12.16+1`
13+
* Require `meta: ^1.16.0`
14+
* Require `sdk: ^3.7.0`
915

1016
## 4.10.1
1117

pkgs/code_builder/analysis_options.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ linter:
2222
- prefer_expression_function_bodies
2323
- prefer_final_locals
2424
- unnecessary_await_in_return
25+
- unnecessary_ignore
2526
- use_string_buffers

pkgs/code_builder/example/example.dart

Lines changed: 50 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,20 @@ void main() {
2323
/// }
2424
/// ```
2525
String animalClass() {
26-
final animal = Class((b) => b
27-
..name = 'Animal'
28-
..extend = refer('Organism')
29-
..methods.add(Method.returnsVoid((b) => b
30-
..name = 'eat'
31-
..body = refer('print').call([literalString('Yum!')]).code)));
26+
final animal = Class(
27+
(b) =>
28+
b
29+
..name = 'Animal'
30+
..extend = refer('Organism')
31+
..methods.add(
32+
Method.returnsVoid(
33+
(b) =>
34+
b
35+
..name = 'eat'
36+
..body = refer('print').call([literalString('Yum!')]).code,
37+
),
38+
),
39+
);
3240
return _dartfmt.format('${animal.accept(DartEmitter())}');
3341
}
3442

@@ -43,14 +51,20 @@ String animalClass() {
4351
/// ```
4452
String scopedLibrary() {
4553
final methods = [
46-
Method((b) => b
47-
..body = const Code('')
48-
..name = 'doThing'
49-
..returns = refer('Thing', 'package:a/a.dart')),
50-
Method((b) => b
51-
..body = const Code('')
52-
..name = 'doOther'
53-
..returns = refer('Other', 'package:b/b.dart')),
54+
Method(
55+
(b) =>
56+
b
57+
..body = const Code('')
58+
..name = 'doThing'
59+
..returns = refer('Thing', 'package:a/a.dart'),
60+
),
61+
Method(
62+
(b) =>
63+
b
64+
..body = const Code('')
65+
..name = 'doOther'
66+
..returns = refer('Other', 'package:b/b.dart'),
67+
),
5468
];
5569
final library = Library((b) => b.body.addAll(methods));
5670
return _dartfmt.format('${library.accept(DartEmitter.scoped())}');
@@ -68,19 +82,28 @@ String scopedLibrary() {
6882
/// ```
6983
String jsonEnum() {
7084
final values = <EnumValue>[
71-
EnumValue((b) => b
72-
..name = 'metric'
73-
..annotations.addAll([
74-
refer('JsonKey').call([literalString('m')])
75-
])),
76-
EnumValue((b) => b
77-
..name = 'imperial'
78-
..annotations.addAll([
79-
refer('JsonKey').call([literalString('i')])
80-
])),
85+
EnumValue(
86+
(b) =>
87+
b
88+
..name = 'metric'
89+
..annotations.addAll([
90+
refer('JsonKey').call([literalString('m')]),
91+
]),
92+
),
93+
EnumValue(
94+
(b) =>
95+
b
96+
..name = 'imperial'
97+
..annotations.addAll([
98+
refer('JsonKey').call([literalString('i')]),
99+
]),
100+
),
81101
];
82-
final e = Enum((b) => b
83-
..name = 'Unit'
84-
..values.addAll(values));
102+
final e = Enum(
103+
(b) =>
104+
b
105+
..name = 'Unit'
106+
..values.addAll(values),
107+
);
85108
return _dartfmt.format('${e.accept(DartEmitter())}');
86109
}

pkgs/code_builder/lib/code_builder.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export 'src/specs/enum.dart'
1717
export 'src/specs/expression.dart'
1818
show
1919
BinaryExpression,
20+
ClosureExpression,
2021
CodeExpression,
2122
Expression,
2223
ExpressionEmitter,
@@ -25,6 +26,9 @@ export 'src/specs/expression.dart'
2526
InvokeExpressionType,
2627
LiteralExpression,
2728
LiteralListExpression,
29+
LiteralMapExpression,
30+
LiteralRecordExpression,
31+
LiteralSetExpression,
2832
ParenthesizedExpression,
2933
ToCodeExpression,
3034
declareConst,
@@ -70,3 +74,4 @@ export 'src/specs/type_function.dart' show FunctionType, FunctionTypeBuilder;
7074
export 'src/specs/type_record.dart' show RecordType, RecordTypeBuilder;
7175
export 'src/specs/type_reference.dart' show TypeReference, TypeReferenceBuilder;
7276
export 'src/specs/typedef.dart' show TypeDef, TypeDefBuilder;
77+
export 'src/visitors.dart' show SpecVisitor;

pkgs/code_builder/lib/src/allocator.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ class _PrefixedAllocator implements Allocator {
9090
int _nextKey() => _keys++;
9191

9292
@override
93-
Iterable<Directive> get imports => _imports.keys.map(
94-
(u) => Directive.import(u, as: '_i${_imports[u]}'),
95-
);
93+
Iterable<Directive> get imports =>
94+
_imports.keys.map((u) => Directive.import(u, as: '_i${_imports[u]}'));
9695
}

pkgs/code_builder/lib/src/emitter.dart

Lines changed: 57 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,21 @@ class DartEmitter extends Object
7676
/// May specify an [Allocator] to use for references and imports,
7777
/// otherwise uses [Allocator.none] which never prefixes references and will
7878
/// not automatically emit import directives.
79-
DartEmitter(
80-
{this.allocator = Allocator.none,
81-
this.orderDirectives = false,
82-
bool useNullSafetySyntax = false})
83-
: _useNullSafetySyntax = useNullSafetySyntax;
79+
DartEmitter({
80+
this.allocator = Allocator.none,
81+
this.orderDirectives = false,
82+
bool useNullSafetySyntax = false,
83+
}) : _useNullSafetySyntax = useNullSafetySyntax;
8484

8585
/// Creates a new instance of [DartEmitter] with simple automatic imports.
86-
factory DartEmitter.scoped(
87-
{bool orderDirectives = false, bool useNullSafetySyntax = false}) =>
88-
DartEmitter(
89-
allocator: Allocator.simplePrefixing(),
90-
orderDirectives: orderDirectives,
91-
useNullSafetySyntax: useNullSafetySyntax);
86+
factory DartEmitter.scoped({
87+
bool orderDirectives = false,
88+
bool useNullSafetySyntax = false,
89+
}) => DartEmitter(
90+
allocator: Allocator.simplePrefixing(),
91+
orderDirectives: orderDirectives,
92+
useNullSafetySyntax: useNullSafetySyntax,
93+
);
9294

9395
static bool _isLambdaBody(Code? code) =>
9496
code is ToCodeExpression && !code.isStatement;
@@ -145,13 +147,17 @@ class DartEmitter extends Object
145147
out
146148
..write(' with ')
147149
..writeAll(
148-
spec.mixins.map<StringSink>((m) => m.type.accept(this)), ',');
150+
spec.mixins.map<StringSink>((m) => m.type.accept(this)),
151+
',',
152+
);
149153
}
150154
if (spec.implements.isNotEmpty) {
151155
out
152156
..write(' implements ')
153157
..writeAll(
154-
spec.implements.map<StringSink>((m) => m.type.accept(this)), ',');
158+
spec.implements.map<StringSink>((m) => m.type.accept(this)),
159+
',',
160+
);
155161
}
156162
out.write(' {');
157163
for (var c in spec.constructors) {
@@ -188,7 +194,9 @@ class DartEmitter extends Object
188194
out
189195
..write(' implements ')
190196
..writeAll(
191-
spec.implements.map<StringSink>((m) => m.type.accept(this)), ',');
197+
spec.implements.map<StringSink>((m) => m.type.accept(this)),
198+
',',
199+
);
192200
}
193201
out.write(' {');
194202
for (var f in spec.fields) {
@@ -201,8 +209,11 @@ class DartEmitter extends Object
201209
}
202210

203211
@override
204-
StringSink visitConstructor(Constructor spec, String clazz,
205-
[StringSink? output]) {
212+
StringSink visitConstructor(
213+
Constructor spec,
214+
String clazz, [
215+
StringSink? output,
216+
]) {
206217
output ??= StringBuffer();
207218
spec.docs.forEach(output.writeln);
208219
for (var a in spec.annotations) {
@@ -342,7 +353,9 @@ class DartEmitter extends Object
342353
out
343354
..write(' implements ')
344355
..writeAll(
345-
spec.implements.map<StringSink>((m) => m.type.accept(this)), ',');
356+
spec.implements.map<StringSink>((m) => m.type.accept(this)),
357+
',',
358+
);
346359
}
347360

348361
out.writeln(' {');
@@ -360,7 +373,9 @@ class DartEmitter extends Object
360373
}
361374

362375
void _visitRepresentationDeclaration(
363-
RepresentationDeclaration spec, StringSink out) {
376+
RepresentationDeclaration spec,
377+
StringSink out,
378+
) {
364379
spec.docs.forEach(out.writeln);
365380
for (var a in spec.annotations) {
366381
visitAnnotation(a, out);
@@ -509,7 +524,8 @@ class DartEmitter extends Object
509524
Directive? previous;
510525
if (directives.any((d) => d.as?.startsWith('_') ?? false)) {
511526
output.writeln(
512-
'// ignore_for_file: no_leading_underscores_for_library_prefixes');
527+
'// ignore_for_file: no_leading_underscores_for_library_prefixes',
528+
);
513529
}
514530
for (final directive in directives) {
515531
if (_newLineBetween(orderDirectives, previous, directive)) {
@@ -543,15 +559,17 @@ class DartEmitter extends Object
543559
out.write('>');
544560
}
545561
out.write('(');
546-
final needsTrailingComma = spec.requiredParameters.length +
562+
final needsTrailingComma =
563+
spec.requiredParameters.length +
547564
spec.optionalParameters.length +
548565
spec.namedRequiredParameters.length +
549566
spec.namedParameters.length >
550567
1;
551568
visitAll<Reference>(spec.requiredParameters, out, (spec) {
552569
spec.accept(this, out);
553570
});
554-
final hasNamedParameters = spec.namedRequiredParameters.isNotEmpty ||
571+
final hasNamedParameters =
572+
spec.namedRequiredParameters.isNotEmpty ||
555573
spec.namedParameters.isNotEmpty;
556574
if (spec.requiredParameters.isNotEmpty &&
557575
(needsTrailingComma ||
@@ -610,8 +628,9 @@ class DartEmitter extends Object
610628
out.write(', ');
611629
}
612630
out.write('{');
613-
visitAll<MapEntry<String, Reference>>(spec.namedFieldTypes.entries, out,
614-
(entry) {
631+
visitAll<MapEntry<String, Reference>>(spec.namedFieldTypes.entries, out, (
632+
entry,
633+
) {
615634
entry.value.accept(this, out);
616635
out.write(' ${entry.key}');
617636
});
@@ -811,8 +830,10 @@ class DartEmitter extends Object
811830
}
812831

813832
@override
814-
StringSink visitTypeParameters(Iterable<Reference> specs,
815-
[StringSink? output]) {
833+
StringSink visitTypeParameters(
834+
Iterable<Reference> specs, [
835+
StringSink? output,
836+
]) {
816837
output ??= StringBuffer();
817838
if (specs.isNotEmpty) {
818839
output
@@ -836,13 +857,17 @@ class DartEmitter extends Object
836857
out
837858
..write(' with ')
838859
..writeAll(
839-
spec.mixins.map<StringSink>((m) => m.type.accept(this)), ', ');
860+
spec.mixins.map<StringSink>((m) => m.type.accept(this)),
861+
', ',
862+
);
840863
}
841864
if (spec.implements.isNotEmpty) {
842865
out
843866
..write(' implements ')
844867
..writeAll(
845-
spec.implements.map<StringSink>((m) => m.type.accept(this)), ', ');
868+
spec.implements.map<StringSink>((m) => m.type.accept(this)),
869+
', ',
870+
);
846871
}
847872
out.write(' { ');
848873
for (var v in spec.values) {
@@ -855,15 +880,18 @@ class DartEmitter extends Object
855880
out.write('.${v.constructorName}');
856881
}
857882
visitTypeParameters(v.types.map((r) => r.type), out);
858-
final takesArguments = v.constructorName != null ||
883+
final takesArguments =
884+
v.constructorName != null ||
859885
v.arguments.isNotEmpty ||
860886
v.namedArguments.isNotEmpty;
861887
if (takesArguments) {
862888
out.write('(');
863889
}
864890
if (v.arguments.isNotEmpty) {
865891
out.writeAll(
866-
v.arguments.map<StringSink>((arg) => arg.accept(this)), ', ');
892+
v.arguments.map<StringSink>((arg) => arg.accept(this)),
893+
', ',
894+
);
867895
}
868896
if (v.arguments.isNotEmpty && v.namedArguments.isNotEmpty) {
869897
out.write(', ');

pkgs/code_builder/lib/src/matchers.dart

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@ String _dart(Spec spec, DartEmitter emitter) =>
1616
/// Both [source] and the result emitted from the compared [Spec] are formatted
1717
/// with [EqualsDart.format]. A plain [DartEmitter] is used by default and may
1818
/// be overridden with [emitter].
19-
Matcher equalsDart(
20-
String source, [
21-
DartEmitter? emitter,
22-
]) =>
19+
Matcher equalsDart(String source, [DartEmitter? emitter]) =>
2320
EqualsDart._(EqualsDart._format(source), emitter ?? DartEmitter());
2421

2522
/// Implementation detail of using the [equalsDart] matcher.
@@ -59,12 +56,9 @@ class EqualsDart extends Matcher {
5956
bool verbose,
6057
) {
6158
final actualSource = _dart(item, _emitter);
62-
return equals(_expectedSource).describeMismatch(
63-
actualSource,
64-
mismatchDescription,
65-
matchState,
66-
verbose,
67-
);
59+
return equals(
60+
_expectedSource,
61+
).describeMismatch(actualSource, mismatchDescription, matchState, verbose);
6862
}
6963

7064
@override

0 commit comments

Comments
 (0)