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

Commit 513bc9c

Browse files
authored
More features towards completion (#94)
* Add more features towards final. * Oops. * Update type_def.dart
1 parent d19fc0f commit 513bc9c

File tree

10 files changed

+234
-26
lines changed

10 files changed

+234
-26
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 1.0.0-beta+6
2+
3+
- Added `TypeDefBuilder`.
4+
- Added `FunctionParameterBuilder`.
5+
- Added `asAbstract` to various `MethodBuilder` constructors.
6+
17
## 1.0.0-beta+5
28

39
- Re-published the package without merge conflicts.

lib/code_builder.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ export 'src/builders/method.dart'
3030
MethodBuilder,
3131
MethodModifier,
3232
ValidMethodMember;
33-
export 'src/builders/parameter.dart' show parameter, ParameterBuilder;
33+
export 'src/builders/parameter.dart'
34+
show parameter, FunctionParameterBuilder, ParameterBuilder;
3435
export 'src/pretty_printer.dart' show prettyToSource;
3536
export 'src/builders/reference.dart'
3637
show explicitThis, reference, ReferenceBuilder;
@@ -52,4 +53,5 @@ export 'src/builders/statement.dart'
5253
SwitchDefaultCaseBuilder,
5354
SwitchStatementBuilder,
5455
WhileStatementBuilder;
55-
export 'src/builders/type.dart' show NewInstanceBuilder, TypeBuilder;
56+
export 'src/builders/type.dart'
57+
show NewInstanceBuilder, TypeBuilder, TypeDefBuilder;

lib/src/builders/method.dart

Lines changed: 64 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,18 @@ ConstructorBuilder constructorNamed(
3434

3535
/// Various types of modifiers for methods.
3636
class MethodModifier implements ValidMethodMember {
37-
static const MethodModifier asAsync = const MethodModifier._('async', false);
38-
static const MethodModifier asAsyncStar =
39-
const MethodModifier._('async', true);
40-
static const MethodModifier asSyncStar = const MethodModifier._('sync', true);
37+
static const MethodModifier asAsync = const MethodModifier._(
38+
'async',
39+
false,
40+
);
41+
static const MethodModifier asAsyncStar = const MethodModifier._(
42+
'async',
43+
true,
44+
);
45+
static const MethodModifier asSyncStar = const MethodModifier._(
46+
'sync',
47+
true,
48+
);
4149

4250
final String _keyword;
4351

@@ -237,6 +245,7 @@ abstract class MethodBuilder
237245
/// Creates a new [MethodBuilder].
238246
factory MethodBuilder(
239247
String name, {
248+
bool asAbstract: false,
240249
MethodModifier modifier,
241250
ExpressionBuilder returns,
242251
TypeBuilder returnType,
@@ -248,12 +257,14 @@ abstract class MethodBuilder
248257
returnType,
249258
null,
250259
modifier,
260+
asAbstract,
251261
);
252262
} else {
253263
return new _MethodBuilderImpl(
254264
name,
255265
modifier: modifier,
256266
returns: returnType,
267+
asAbstract: asAbstract,
257268
);
258269
}
259270
}
@@ -263,6 +274,7 @@ abstract class MethodBuilder
263274
MethodModifier modifier,
264275
ExpressionBuilder returns,
265276
TypeBuilder returnType,
277+
bool asAbstract: false,
266278
}) {
267279
if (returns != null) {
268280
return new _LambdaMethodBuilder(
@@ -271,12 +283,14 @@ abstract class MethodBuilder
271283
returnType,
272284
null,
273285
modifier,
286+
asAbstract,
274287
);
275288
} else {
276289
return new _MethodBuilderImpl(
277290
null,
278291
modifier: modifier,
279292
returns: returnType,
293+
asAbstract: asAbstract,
280294
);
281295
}
282296
}
@@ -287,13 +301,15 @@ abstract class MethodBuilder
287301
MethodModifier modifier,
288302
TypeBuilder returnType,
289303
ExpressionBuilder returns,
304+
bool asAbstract: false,
290305
}) {
291306
if (returns == null) {
292307
return new _MethodBuilderImpl(
293308
name,
294309
modifier: modifier,
295310
returns: returnType,
296311
property: Keyword.GET,
312+
asAbstract: asAbstract,
297313
);
298314
} else {
299315
return new _LambdaMethodBuilder(
@@ -302,33 +318,45 @@ abstract class MethodBuilder
302318
returnType,
303319
Keyword.GET,
304320
modifier,
321+
asAbstract,
305322
);
306323
}
307324
}
308325

309326
/// Creates a new [MethodBuilder] that returns `void`.
310-
factory MethodBuilder.returnVoid(String name, {ExpressionBuilder returns}) {
327+
factory MethodBuilder.returnVoid(
328+
String name, {
329+
ExpressionBuilder returns,
330+
bool asAbstract: false,
331+
}) {
311332
if (returns == null) {
312-
return new _MethodBuilderImpl(name, returns: lib$core.$void);
333+
return new _MethodBuilderImpl(
334+
name,
335+
returns: lib$core.$void,
336+
asAbstract: asAbstract,
337+
);
313338
}
314339
return new _LambdaMethodBuilder(
315340
name,
316341
returns,
317342
lib$core.$void,
318343
null,
319344
null,
345+
asAbstract,
320346
);
321347
}
322348

323349
/// Creates a setter.
324350
factory MethodBuilder.setter(
325351
String name, {
326352
ExpressionBuilder returns,
353+
bool asAbstract: false,
327354
}) {
328355
if (returns == null) {
329356
return new _MethodBuilderImpl(
330357
name,
331358
property: Keyword.SET,
359+
asAbstract: asAbstract,
332360
);
333361
} else {
334362
return new _LambdaMethodBuilder(
@@ -337,6 +365,7 @@ abstract class MethodBuilder
337365
null,
338366
Keyword.SET,
339367
null,
368+
asAbstract,
340369
);
341370
}
342371
}
@@ -376,13 +405,15 @@ class _LambdaMethodBuilder extends Object
376405
final String _name;
377406
final TypeBuilder _returnType;
378407
final Keyword _property;
408+
final bool _asAbstract;
379409

380410
_LambdaMethodBuilder(
381411
this._name,
382412
this._expression,
383413
this._returnType,
384414
this._property,
385415
this._modifier,
416+
this._asAbstract,
386417
);
387418

388419
@override
@@ -430,7 +461,9 @@ class _LambdaMethodBuilder extends Object
430461
_returnType?.buildType(scope),
431462
_property != null ? new KeywordToken(_property, 0) : null,
432463
_name != null ? stringIdentifier(_name) : null,
433-
_buildExpression(scope, isStatement: true),
464+
_asAbstract
465+
? astFactory.emptyFunctionBody($semicolon)
466+
: _buildExpression(scope, isStatement: true),
434467
);
435468
}
436469

@@ -447,12 +480,14 @@ class _LambdaMethodBuilder extends Object
447480
stringIdentifier(_name),
448481
null,
449482
_property != Keyword.GET ? buildParameterList(scope) : null,
450-
astFactory.expressionFunctionBody(
451-
_modifier?.keyword(),
452-
null,
453-
_expression.buildExpression(scope),
454-
$semicolon,
455-
),
483+
_asAbstract
484+
? astFactory.emptyFunctionBody($semicolon)
485+
: astFactory.expressionFunctionBody(
486+
_modifier?.keyword(),
487+
null,
488+
_expression.buildExpression(scope),
489+
$semicolon,
490+
),
456491
);
457492
}
458493

@@ -472,12 +507,14 @@ class _MethodBuilderImpl extends Object
472507
final String _name;
473508
final TypeBuilder _returnType;
474509
final Keyword _property;
510+
final bool asAbstract;
475511

476512
_MethodBuilderImpl(
477513
this._name, {
478514
MethodModifier modifier,
479515
TypeBuilder returns,
480516
Keyword property,
517+
this.asAbstract: false,
481518
})
482519
: _modifier = modifier,
483520
_returnType = returns,
@@ -496,11 +533,13 @@ class _MethodBuilderImpl extends Object
496533
return astFactory.functionExpression(
497534
null,
498535
_property != Keyword.GET ? buildParameterList(scope) : null,
499-
astFactory.blockFunctionBody(
500-
_modifier?.keyword(),
501-
_modifier?.isStar == true ? $star : null,
502-
buildBlock(scope),
503-
),
536+
asAbstract
537+
? astFactory.emptyFunctionBody($semicolon)
538+
: astFactory.blockFunctionBody(
539+
_modifier?.keyword(),
540+
_modifier?.isStar == true ? $star : null,
541+
buildBlock(scope),
542+
),
504543
);
505544
}
506545

@@ -530,11 +569,13 @@ class _MethodBuilderImpl extends Object
530569
identifier(scope, _name),
531570
null,
532571
_property != Keyword.GET ? buildParameterList(scope) : null,
533-
astFactory.blockFunctionBody(
534-
_modifier?.keyword(),
535-
_modifier?.isStar == true ? $star : null,
536-
buildBlock(scope),
537-
),
572+
asAbstract
573+
? astFactory.emptyFunctionBody($semicolon)
574+
: astFactory.blockFunctionBody(
575+
_modifier?.keyword(),
576+
_modifier?.isStar == true ? $star : null,
577+
buildBlock(scope),
578+
),
538579
);
539580
}
540581

lib/src/builders/parameter.dart

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,43 @@ class _SimpleParameterBuilder extends Object
218218
);
219219
}
220220
}
221+
222+
/// A parameter type that represents a function definition.
223+
class FunctionParameterBuilder extends Object
224+
with HasAnnotationsMixin, HasParametersMixin
225+
implements ParameterBuilder {
226+
@override
227+
final String name;
228+
final TypeBuilder returnType;
229+
230+
FunctionParameterBuilder(
231+
this.name, {
232+
this.returnType,
233+
});
234+
235+
@override
236+
ParameterBuilder asOptional([ExpressionBuilder defaultTo]) {
237+
return new _OptionalParameterBuilder(this, defaultTo);
238+
}
239+
240+
@override
241+
FunctionTypedFormalParameter buildAst([Scope scope]) =>
242+
buildPositional(false, scope);
243+
244+
@override
245+
FunctionTypedFormalParameter buildNamed(bool field, [Scope scope]) {
246+
return asOptional().buildNamed(field, scope);
247+
}
248+
249+
@override
250+
FunctionTypedFormalParameter buildPositional(bool field, [Scope scope]) {
251+
return astFactory.functionTypedFormalParameter(
252+
null,
253+
buildAnnotations(scope),
254+
returnType?.buildType(scope),
255+
stringIdentifier(name),
256+
null,
257+
buildParameterList(scope),
258+
);
259+
}
260+
}

lib/src/builders/type.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import 'package:code_builder/src/builders/statement.dart';
1818
import 'package:code_builder/src/tokens.dart';
1919

2020
part 'type/new_instance.dart';
21+
part 'type/type_def.dart';
2122

2223
/// Implements the `new` and `const` constructor calls.
2324
abstract class AbstractTypeBuilderMixin {
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
part of code_builder.src.builders.type;
6+
7+
TypeParameterList typeParameters(Scope scope, Map<String, TypeBuilder> types) {
8+
if (types.isEmpty) {
9+
return null;
10+
}
11+
return astFactory.typeParameterList(
12+
$openBracket,
13+
types.keys.map((name) {
14+
final type = types[name];
15+
return astFactory.typeParameter(
16+
null,
17+
null,
18+
stringIdentifier(name),
19+
type != null ? $extends : null,
20+
type?.buildType(scope),
21+
);
22+
}).toList(),
23+
$closeBracket,
24+
);
25+
}
26+
27+
/// Lazily build a `typedef`, or [FunctionTypeAlias] AST.
28+
class TypeDefBuilder extends AstBuilder<FunctionTypeAlias>
29+
with HasAnnotationsMixin, HasParametersMixin, TopLevelMixin {
30+
/// Optional generic type parameters.
31+
final Map<String, TypeBuilder> genericTypes;
32+
33+
/// Name of the `typedef`.
34+
final String name;
35+
36+
/// Return type; if `null` defaults to implicitly dynamic.
37+
final TypeBuilder returnType;
38+
39+
TypeDefBuilder(
40+
this.name, {
41+
this.genericTypes: const {},
42+
this.returnType,
43+
});
44+
45+
@override
46+
FunctionTypeAlias buildAst([Scope scope]) {
47+
return astFactory.functionTypeAlias(
48+
null,
49+
buildAnnotations(scope),
50+
null,
51+
returnType?.buildType(scope),
52+
stringIdentifier(name),
53+
typeParameters(scope, genericTypes),
54+
buildParameterList(scope),
55+
$semicolon,
56+
);
57+
}
58+
59+
@override
60+
CompilationUnitMember buildTopLevelAst([Scope scope]) => buildAst(scope);
61+
}

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-beta+5
2+
version: 1.0.0-beta+6
33
description: A fluent API for generating Dart code
44
author: Dart Team <[email protected]>
55
homepage: https://github.com/dart-lang/code_builder

0 commit comments

Comments
 (0)