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

Commit a0c6a33

Browse files
authored
Various changes to close a bunch of open issues (#21)
* Various changes. * Update presubmit.
1 parent 62d8db1 commit a0c6a33

27 files changed

+416
-49
lines changed
Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,27 @@ analyzer:
22
strong-mode: true
33
linter:
44
rules:
5+
# Errors
56
- avoid_empty_else
6-
- comment_references
77
- control_flow_in_finally
88
- empty_statements
9-
- hash_and_equals
10-
- iterable_contains_unrelated_type
11-
- list_remove_unrelated_type
129
- test_types_in_equals
1310
- throw_in_finally
14-
- unrelated_type_equality_checks
1511
- valid_regexps
16-
- always_declare_return_types
12+
13+
# Style
1714
- annotate_overrides
1815
- avoid_init_to_null
1916
- avoid_return_types_on_setters
2017
- await_only_futures
2118
- camel_case_types
22-
- constant_identifier_names
19+
- comment_references
2320
- empty_catches
2421
- empty_constructor_bodies
25-
- library_names
22+
- hash_and_equals
2623
- library_prefixes
27-
- only_throw_errors
28-
- overridden_fields
29-
- package_prefixed_library_names
24+
- non_constant_identifier_names
3025
- prefer_is_not_empty
3126
- slash_for_doc_comments
3227
- type_init_formals
33-
- unnecessary_getters_setters
28+
- unrelated_type_equality_checks

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# Changelog
22

3+
## 1.0.0-alpha+1
4+
5+
- Slight updates to confusing documentation.
6+
- Added support for null-aware assignments.
7+
- Added `show` and `hide` support to `ImportBuilder`
8+
- Added `deferred` support to `ImportBuilder`
9+
- Added `ExportBuilder`
10+
- Added `list` and `map` literals that support generic types
11+
312
## 1.0.0-alpha
413

514
- Large refactor that makes the library more feature complete.

lib/code_builder.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ export 'src/builders/annotation.dart' show AnnotationBuilder;
66
export 'src/builders/class.dart'
77
show asStatic, clazz, extend, implement, mixin, ClassBuilder;
88
export 'src/builders/expression.dart'
9-
show literal, ExpressionBuilder, InvocationBuilder;
9+
show literal, list, map, ExpressionBuilder, InvocationBuilder;
1010
export 'src/builders/field.dart'
1111
show varConst, varField, varFinal, FieldBuilder;
12-
export 'src/builders/file.dart' show ImportBuilder, LibraryBuilder, PartBuilder;
12+
export 'src/builders/file.dart'
13+
show ExportBuilder, ImportBuilder, LibraryBuilder, PartBuilder;
1314
export 'src/builders/method.dart'
1415
show
1516
constructor,

lib/dart/async.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ class DartAsync {
3535
/// References [dart_async.Future].
3636
final ReferenceBuilder Future = _ref('Future');
3737

38+
/// References [dart_async.Stream].
39+
final ReferenceBuilder Stream = _ref('Stream');
40+
41+
/// References [dart_async.StreamSubscription].
42+
final ReferenceBuilder StreamSubscription = _ref('StreamSubscription');
43+
3844
DartAsync._();
3945

4046
static ReferenceBuilder _ref(String name) => reference(name, 'dart:async');

lib/dart/core.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,11 @@ class DartCore {
213213
/// References [dart_core.UriData].
214214
final ReferenceBuilder UriData = _ref('UriData');
215215

216+
/// References `dynamic` type for returning any in a method.
217+
///
218+
/// **NOTE**: As a language limitation, this cannot be named `dynamic`.
219+
final TypeBuilder $dynamic = new TypeBuilder('dynamic');
220+
216221
/// References `void` type for returning nothing in a method.
217222
///
218223
/// **NOTE**: As a language limitation, this cannot be named `void`.

lib/src/builders/expression.dart

Lines changed: 132 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
library code_builder.src.builders.expression;
2-
31
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
42
// for details. All rights reserved. Use of this source code is governed by a
53
// BSD-style license that can be found in the LICENSE file.
4+
library code_builder.src.builders.expression;
65

76
import 'package:analyzer/analyzer.dart';
87
import 'package:analyzer/dart/ast/token.dart';
@@ -32,7 +31,37 @@ final _true = new BooleanLiteral(new KeywordToken(Keyword.TRUE, 0), true);
3231
/// Returns a pre-defined literal expression of [value].
3332
///
3433
/// Only primitive values are allowed.
35-
ExpressionBuilder literal(value) => new _LiteralExpression(_literal(value));
34+
ExpressionBuilder literal(value) {
35+
if (value is List) {
36+
return list(value);
37+
}
38+
if (value is Map) {
39+
return map(value);
40+
}
41+
return new _LiteralExpression(_literal(value));
42+
}
43+
44+
/// Returns a literal `List` expression from [values].
45+
///
46+
/// Optionally specify [asConst] or with a generic [type].
47+
ExpressionBuilder list(
48+
Iterable values, {
49+
bool asConst: false,
50+
TypeBuilder type,
51+
}) =>
52+
new _TypedListExpression(values, asConst: asConst, type: type);
53+
54+
/// Returns a literal `Map` expression from [values].
55+
///
56+
/// Optionally specify [asConst] or with a generic [keyType] or [valueType].
57+
ExpressionBuilder map(
58+
Map values, {
59+
bool asConst: false,
60+
TypeBuilder keyType,
61+
TypeBuilder valueType,
62+
}) =>
63+
new _TypedMapExpression(values,
64+
asConst: asConst, keyType: keyType, valueType: valueType);
3665

3766
Literal _literal(value) {
3867
if (value == null) {
@@ -45,24 +74,6 @@ Literal _literal(value) {
4574
return new IntegerLiteral(stringToken('$value'), value);
4675
} else if (value is double) {
4776
return new DoubleLiteral(stringToken('$value'), value);
48-
} else if (value is List) {
49-
return new ListLiteral(
50-
null,
51-
null,
52-
$openBracket,
53-
value.map/*<Literal>*/(_literal).toList(),
54-
$closeBracket,
55-
);
56-
} else if (value is Map) {
57-
return new MapLiteral(
58-
null,
59-
null,
60-
$openBracket,
61-
value.keys.map/*<MapLiteralEntry>*/((k) {
62-
return new MapLiteralEntry(_literal(k), $colon, _literal(value[k]));
63-
}).toList(),
64-
$closeBracket,
65-
);
6677
}
6778
throw new ArgumentError.value(value, 'Unsupported');
6879
}
@@ -357,3 +368,103 @@ class _ParenthesesExpression extends Object with AbstractExpressionMixin {
357368
);
358369
}
359370
}
371+
372+
ExpressionBuilder _expressionify(v) {
373+
if (v == null || v is bool || v is String || v is int || v is double) {
374+
return new _LiteralExpression(_literal(v));
375+
}
376+
if (v is ExpressionBuilder) {
377+
return v;
378+
}
379+
throw new ArgumentError('Could not expressionify $v');
380+
}
381+
382+
class _TypedListExpression extends Object with AbstractExpressionMixin {
383+
static List<ExpressionBuilder> _toExpression(Iterable values) {
384+
return values.map(_expressionify).toList();
385+
}
386+
387+
final bool _asConst;
388+
final TypeBuilder _type;
389+
final List<ExpressionBuilder> _values;
390+
391+
_TypedListExpression(Iterable values, {bool asConst, TypeBuilder type})
392+
: _values = _toExpression(values),
393+
_asConst = asConst,
394+
_type = type;
395+
396+
@override
397+
AstNode buildAst([Scope scope]) => buildExpression(scope);
398+
399+
@override
400+
Expression buildExpression([Scope scope]) {
401+
return new ListLiteral(
402+
_asConst ? $const : null,
403+
_type != null
404+
? new TypeArgumentList(
405+
$openBracket,
406+
[_type.buildType(scope)],
407+
$closeBracket,
408+
)
409+
: null,
410+
$openBracket,
411+
_values.map((v) => v.buildExpression(scope)).toList(),
412+
$closeBracket,
413+
);
414+
}
415+
}
416+
417+
class _TypedMapExpression extends Object with AbstractExpressionMixin {
418+
static Map<ExpressionBuilder, ExpressionBuilder> _toExpression(Map values) {
419+
return new Map<ExpressionBuilder, ExpressionBuilder>.fromIterable(
420+
values.keys,
421+
key: (k) => _expressionify(k),
422+
value: (k) => _expressionify(values[k]),
423+
);
424+
}
425+
426+
final bool _asConst;
427+
final TypeBuilder _keyType;
428+
final TypeBuilder _valueType;
429+
final Map<ExpressionBuilder, ExpressionBuilder> _values;
430+
431+
_TypedMapExpression(Map values,
432+
{bool asConst, TypeBuilder keyType, TypeBuilder valueType})
433+
: _values = _toExpression(values),
434+
_asConst = asConst,
435+
_keyType = keyType != null
436+
? keyType
437+
: valueType != null ? lib$core.$dynamic : null,
438+
_valueType = valueType != null
439+
? valueType
440+
: keyType != null ? lib$core.$dynamic : null;
441+
442+
@override
443+
AstNode buildAst([Scope scope]) => buildExpression(scope);
444+
445+
@override
446+
Expression buildExpression([Scope scope]) {
447+
return new MapLiteral(
448+
_asConst ? $const : null,
449+
_keyType != null
450+
? new TypeArgumentList(
451+
$openBracket,
452+
[
453+
_keyType.buildType(scope),
454+
_valueType.buildType(scope),
455+
],
456+
$closeBracket,
457+
)
458+
: null,
459+
$openBracket,
460+
_values.keys.map((k) {
461+
return new MapLiteralEntry(
462+
k.buildExpression(scope),
463+
$colon,
464+
_values[k].buildExpression(scope),
465+
);
466+
}).toList(),
467+
$closeBracket,
468+
);
469+
}
470+
}

lib/src/builders/expression/assert.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright (c) 2016, 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+
15
part of code_builder.src.builders.expression;
26

37
class _AsAssert implements StatementBuilder {

lib/src/builders/expression/assign.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright (c) 2016, 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+
15
part of code_builder.src.builders.expression;
26

37
class _AsAssign extends AbstractExpressionMixin {

lib/src/builders/expression/invocation.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright (c) 2016, 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+
15
part of code_builder.src.builders.expression;
26

37
/// Partial implementation of [InvocationBuilder].

lib/src/builders/expression/negate.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright (c) 2016, 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+
15
part of code_builder.src.builders.expression;
26

37
class _NegateExpression extends AbstractExpressionMixin {

0 commit comments

Comments
 (0)