Skip to content
This repository was archived by the owner on Apr 8, 2025. It is now read-only.

Commit f0d8541

Browse files
authored
Remove semicolons from where they don't belong. (#33)
* Remove semicolons from where they don't belong. * Update changelog * Cleanup Changelog
1 parent 7f984ba commit f0d8541

File tree

5 files changed

+81
-31
lines changed

5 files changed

+81
-31
lines changed

CHANGELOG.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
## 1.0.0-alpha+5
2+
3+
- MethodBuilder with no statements will create an empty block instead of a
4+
semicolon.
5+
6+
```dart
7+
// main() {}
8+
method('main')
9+
```
10+
11+
- Fix lambdas and closures to not include a trailing semicolon when used as an
12+
expression.
13+
14+
```dart
15+
// () => false
16+
new MethodBuilder.closure(returns: literal(false));
17+
```
18+
119
## 1.0.0-alpha+4
220

321
- Add support for latest `pkg/analyzer`.

lib/src/builders/method.dart

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -326,14 +326,18 @@ class _LambdaMethodBuilder extends Object
326326

327327
@override
328328
Expression buildExpression([Scope scope]) {
329+
return _buildExpression(scope, isStatement: false);
330+
}
331+
332+
FunctionExpression _buildExpression(Scope scope, {bool isStatement}) {
329333
return new FunctionExpression(
330334
null,
331335
_property != Keyword.GET ? buildParameterList(scope) : null,
332336
new ExpressionFunctionBody(
333337
null,
334338
null,
335339
_expression.buildExpression(scope),
336-
$semicolon,
340+
isStatement ? $semicolon : null,
337341
),
338342
);
339343
}
@@ -347,7 +351,7 @@ class _LambdaMethodBuilder extends Object
347351
_returnType?.buildType(scope),
348352
_property != null ? new KeywordToken(_property, 0) : null,
349353
stringIdentifier(_name),
350-
buildExpression(scope),
354+
_buildExpression(scope, isStatement: true),
351355
);
352356
}
353357

@@ -407,9 +411,7 @@ class _MethodBuilderImpl extends Object
407411
return new FunctionExpression(
408412
null,
409413
_property != Keyword.GET ? buildParameterList(scope) : null,
410-
!hasStatements
411-
? new EmptyFunctionBody($semicolon)
412-
: new BlockFunctionBody(
414+
new BlockFunctionBody(
413415
null,
414416
null,
415417
buildBlock(scope),
@@ -443,15 +445,16 @@ class _MethodBuilderImpl extends Object
443445
identifier(scope, _name),
444446
null,
445447
_property != Keyword.GET ? buildParameterList(scope) : null,
446-
!hasStatements
447-
? new EmptyFunctionBody($semicolon)
448-
: new BlockFunctionBody(
449-
null,
450-
null,
451-
buildBlock(scope),
452-
),
448+
new BlockFunctionBody(
449+
null,
450+
null,
451+
buildBlock(scope),
452+
),
453453
);
454454
}
455+
456+
@override
457+
CompilationUnitMember buildTopLevelAst([Scope scope]) => buildFunction(scope);
455458
}
456459

457460
class _NamedParameterWrapper

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: code_builder
2-
version: 1.0.0-alpha+4
2+
version: 1.0.0-alpha+5
33
description: A fluent API for generating Dart code
44
author: Dart Team <[email protected]>
55
homepage: https://github.com/dart-lang/code_builder

test/builders/file_test.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,14 @@ void main() {
5858
'''),
5959
);
6060
});
61+
62+
group('$LibraryBuilder', () {
63+
test('should handle empty methods', () {
64+
expect(
65+
new LibraryBuilder()
66+
..addMember(new MethodBuilder.returnVoid('main')),
67+
equalsSource('void main() {}'));
68+
});
69+
});
6170
});
6271
}

test/builders/method_test.dart

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ void main() {
1212
expect(
1313
method('main'),
1414
equalsSource(r'''
15-
main();
15+
main() {}
1616
'''),
1717
);
1818
});
@@ -23,7 +23,7 @@ void main() {
2323
lib$core.$void,
2424
]).buildMethod(false).toSource(),
2525
equalsIgnoringWhitespace(r'''
26-
void main();
26+
void main() {}
2727
'''),
2828
);
2929
});
@@ -34,7 +34,7 @@ void main() {
3434
parameter('args', [lib$core.List]),
3535
]).buildMethod(false).toSource(),
3636
equalsIgnoringWhitespace(r'''
37-
main(List args);
37+
main(List args) {}
3838
'''),
3939
);
4040
});
@@ -47,7 +47,7 @@ void main() {
4747
parameter('c').asOptional(),
4848
]),
4949
equalsSource(r'''
50-
main(a, b, [c]);
50+
main(a, b, [c]) {}
5151
'''),
5252
);
5353
});
@@ -60,7 +60,7 @@ void main() {
6060
parameter('c').asOptional(literal(true)),
6161
]),
6262
equalsSource(r'''
63-
main(a, b, [c = true]);
63+
main(a, b, [c = true]) {}
6464
'''),
6565
);
6666
});
@@ -72,7 +72,7 @@ void main() {
7272
named(parameter('b').asOptional(literal(true))),
7373
]).buildMethod(false).toSource(),
7474
equalsIgnoringWhitespace(r'''
75-
main({a, b : true});
75+
main({a, b : true}) {}
7676
'''),
7777
);
7878
});
@@ -170,18 +170,38 @@ void main() {
170170
);
171171
});
172172

173-
test('should emit a closure', () {
174-
final closure = new MethodBuilder.closure(
175-
returns: literal(false).or(reference('defaultTo')),
176-
returnType: lib$core.bool,
177-
)..addPositional(parameter('defaultTo', [lib$core.bool]));
178-
// Should be usable as an expression/parameter itself.
179-
expect(closure, const isInstanceOf<ExpressionBuilder>());
180-
expect(
181-
closure,
182-
equalsSource(r'''
183-
(bool defaultTo) => false || defaultTo;
173+
group('closure', () {
174+
MethodBuilder closure;
175+
setUp(() {
176+
closure = new MethodBuilder.closure(
177+
returns: literal(false).or(reference('defaultTo')),
178+
returnType: lib$core.bool,
179+
)..addPositional(parameter('defaultTo', [lib$core.bool]));
180+
});
181+
182+
test('should emit a closure', () {
183+
// Should be usable as an expression/parameter itself.
184+
expect(closure, const isInstanceOf<ExpressionBuilder>());
185+
expect(
186+
closure,
187+
equalsSource(r'''
188+
(bool defaultTo) => false || defaultTo
184189
'''),
185-
);
190+
);
191+
192+
test('should treat closure as expression', () {
193+
expect(
194+
list([true, false]).invoke('where', [closure]),
195+
equalsSource(
196+
'[true, false].where((bool defaultTo) => false || defaultTo)'));
197+
});
198+
199+
test('should emit a closure as a function in a library', () {
200+
final library = new LibraryBuilder();
201+
library.addMember(closure);
202+
expect(
203+
library, equalsSource('(bool defaultTo) => false || defaultTo;'));
204+
});
205+
});
186206
});
187207
}

0 commit comments

Comments
 (0)