Skip to content

Commit

Permalink
Various changes to close a bunch of open issues (#21)
Browse files Browse the repository at this point in the history
* Various changes.

* Update presubmit.
  • Loading branch information
matanlurey committed Nov 15, 2016
1 parent 62d8db1 commit a0c6a33
Show file tree
Hide file tree
Showing 27 changed files with 416 additions and 49 deletions.
19 changes: 7 additions & 12 deletions analysis_options.yaml → .analysis_options
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,27 @@ analyzer:
strong-mode: true
linter:
rules:
# Errors
- avoid_empty_else
- comment_references
- control_flow_in_finally
- empty_statements
- hash_and_equals
- iterable_contains_unrelated_type
- list_remove_unrelated_type
- test_types_in_equals
- throw_in_finally
- unrelated_type_equality_checks
- valid_regexps
- always_declare_return_types

# Style
- annotate_overrides
- avoid_init_to_null
- avoid_return_types_on_setters
- await_only_futures
- camel_case_types
- constant_identifier_names
- comment_references
- empty_catches
- empty_constructor_bodies
- library_names
- hash_and_equals
- library_prefixes
- only_throw_errors
- overridden_fields
- package_prefixed_library_names
- non_constant_identifier_names
- prefer_is_not_empty
- slash_for_doc_comments
- type_init_formals
- unnecessary_getters_setters
- unrelated_type_equality_checks
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

## 1.0.0-alpha+1

- Slight updates to confusing documentation.
- Added support for null-aware assignments.
- Added `show` and `hide` support to `ImportBuilder`
- Added `deferred` support to `ImportBuilder`
- Added `ExportBuilder`
- Added `list` and `map` literals that support generic types

## 1.0.0-alpha

- Large refactor that makes the library more feature complete.
Expand Down
5 changes: 3 additions & 2 deletions lib/code_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ export 'src/builders/annotation.dart' show AnnotationBuilder;
export 'src/builders/class.dart'
show asStatic, clazz, extend, implement, mixin, ClassBuilder;
export 'src/builders/expression.dart'
show literal, ExpressionBuilder, InvocationBuilder;
show literal, list, map, ExpressionBuilder, InvocationBuilder;
export 'src/builders/field.dart'
show varConst, varField, varFinal, FieldBuilder;
export 'src/builders/file.dart' show ImportBuilder, LibraryBuilder, PartBuilder;
export 'src/builders/file.dart'
show ExportBuilder, ImportBuilder, LibraryBuilder, PartBuilder;
export 'src/builders/method.dart'
show
constructor,
Expand Down
6 changes: 6 additions & 0 deletions lib/dart/async.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ class DartAsync {
/// References [dart_async.Future].
final ReferenceBuilder Future = _ref('Future');

/// References [dart_async.Stream].
final ReferenceBuilder Stream = _ref('Stream');

/// References [dart_async.StreamSubscription].
final ReferenceBuilder StreamSubscription = _ref('StreamSubscription');

DartAsync._();

static ReferenceBuilder _ref(String name) => reference(name, 'dart:async');
Expand Down
5 changes: 5 additions & 0 deletions lib/dart/core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,11 @@ class DartCore {
/// References [dart_core.UriData].
final ReferenceBuilder UriData = _ref('UriData');

/// References `dynamic` type for returning any in a method.
///
/// **NOTE**: As a language limitation, this cannot be named `dynamic`.
final TypeBuilder $dynamic = new TypeBuilder('dynamic');

/// References `void` type for returning nothing in a method.
///
/// **NOTE**: As a language limitation, this cannot be named `void`.
Expand Down
153 changes: 132 additions & 21 deletions lib/src/builders/expression.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
library code_builder.src.builders.expression;

// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
library code_builder.src.builders.expression;

import 'package:analyzer/analyzer.dart';
import 'package:analyzer/dart/ast/token.dart';
Expand Down Expand Up @@ -32,7 +31,37 @@ final _true = new BooleanLiteral(new KeywordToken(Keyword.TRUE, 0), true);
/// Returns a pre-defined literal expression of [value].
///
/// Only primitive values are allowed.
ExpressionBuilder literal(value) => new _LiteralExpression(_literal(value));
ExpressionBuilder literal(value) {
if (value is List) {
return list(value);
}
if (value is Map) {
return map(value);
}
return new _LiteralExpression(_literal(value));
}

/// Returns a literal `List` expression from [values].
///
/// Optionally specify [asConst] or with a generic [type].
ExpressionBuilder list(
Iterable values, {
bool asConst: false,
TypeBuilder type,
}) =>
new _TypedListExpression(values, asConst: asConst, type: type);

/// Returns a literal `Map` expression from [values].
///
/// Optionally specify [asConst] or with a generic [keyType] or [valueType].
ExpressionBuilder map(
Map values, {
bool asConst: false,
TypeBuilder keyType,
TypeBuilder valueType,
}) =>
new _TypedMapExpression(values,
asConst: asConst, keyType: keyType, valueType: valueType);

Literal _literal(value) {
if (value == null) {
Expand All @@ -45,24 +74,6 @@ Literal _literal(value) {
return new IntegerLiteral(stringToken('$value'), value);
} else if (value is double) {
return new DoubleLiteral(stringToken('$value'), value);
} else if (value is List) {
return new ListLiteral(
null,
null,
$openBracket,
value.map/*<Literal>*/(_literal).toList(),
$closeBracket,
);
} else if (value is Map) {
return new MapLiteral(
null,
null,
$openBracket,
value.keys.map/*<MapLiteralEntry>*/((k) {
return new MapLiteralEntry(_literal(k), $colon, _literal(value[k]));
}).toList(),
$closeBracket,
);
}
throw new ArgumentError.value(value, 'Unsupported');
}
Expand Down Expand Up @@ -357,3 +368,103 @@ class _ParenthesesExpression extends Object with AbstractExpressionMixin {
);
}
}

ExpressionBuilder _expressionify(v) {
if (v == null || v is bool || v is String || v is int || v is double) {
return new _LiteralExpression(_literal(v));
}
if (v is ExpressionBuilder) {
return v;
}
throw new ArgumentError('Could not expressionify $v');
}

class _TypedListExpression extends Object with AbstractExpressionMixin {
static List<ExpressionBuilder> _toExpression(Iterable values) {
return values.map(_expressionify).toList();
}

final bool _asConst;
final TypeBuilder _type;
final List<ExpressionBuilder> _values;

_TypedListExpression(Iterable values, {bool asConst, TypeBuilder type})
: _values = _toExpression(values),
_asConst = asConst,
_type = type;

@override
AstNode buildAst([Scope scope]) => buildExpression(scope);

@override
Expression buildExpression([Scope scope]) {
return new ListLiteral(
_asConst ? $const : null,
_type != null
? new TypeArgumentList(
$openBracket,
[_type.buildType(scope)],
$closeBracket,
)
: null,
$openBracket,
_values.map((v) => v.buildExpression(scope)).toList(),
$closeBracket,
);
}
}

class _TypedMapExpression extends Object with AbstractExpressionMixin {
static Map<ExpressionBuilder, ExpressionBuilder> _toExpression(Map values) {
return new Map<ExpressionBuilder, ExpressionBuilder>.fromIterable(
values.keys,
key: (k) => _expressionify(k),
value: (k) => _expressionify(values[k]),
);
}

final bool _asConst;
final TypeBuilder _keyType;
final TypeBuilder _valueType;
final Map<ExpressionBuilder, ExpressionBuilder> _values;

_TypedMapExpression(Map values,
{bool asConst, TypeBuilder keyType, TypeBuilder valueType})
: _values = _toExpression(values),
_asConst = asConst,
_keyType = keyType != null
? keyType
: valueType != null ? lib$core.$dynamic : null,
_valueType = valueType != null
? valueType
: keyType != null ? lib$core.$dynamic : null;

@override
AstNode buildAst([Scope scope]) => buildExpression(scope);

@override
Expression buildExpression([Scope scope]) {
return new MapLiteral(
_asConst ? $const : null,
_keyType != null
? new TypeArgumentList(
$openBracket,
[
_keyType.buildType(scope),
_valueType.buildType(scope),
],
$closeBracket,
)
: null,
$openBracket,
_values.keys.map((k) {
return new MapLiteralEntry(
k.buildExpression(scope),
$colon,
_values[k].buildExpression(scope),
);
}).toList(),
$closeBracket,
);
}
}
4 changes: 4 additions & 0 deletions lib/src/builders/expression/assert.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

part of code_builder.src.builders.expression;

class _AsAssert implements StatementBuilder {
Expand Down
4 changes: 4 additions & 0 deletions lib/src/builders/expression/assign.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

part of code_builder.src.builders.expression;

class _AsAssign extends AbstractExpressionMixin {
Expand Down
4 changes: 4 additions & 0 deletions lib/src/builders/expression/invocation.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

part of code_builder.src.builders.expression;

/// Partial implementation of [InvocationBuilder].
Expand Down
4 changes: 4 additions & 0 deletions lib/src/builders/expression/negate.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

part of code_builder.src.builders.expression;

class _NegateExpression extends AbstractExpressionMixin {
Expand Down
4 changes: 4 additions & 0 deletions lib/src/builders/expression/operators.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

part of code_builder.src.builders.expression;

class _AsBinaryExpression extends Object with AbstractExpressionMixin {
Expand Down
4 changes: 4 additions & 0 deletions lib/src/builders/expression/return.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

part of code_builder.src.builders.expression;

class _AsReturn implements StatementBuilder {
Expand Down
4 changes: 4 additions & 0 deletions lib/src/builders/field.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:analyzer/analyzer.dart';
import 'package:analyzer/dart/ast/token.dart';
import 'package:analyzer/src/dart/ast/token.dart';
Expand Down
Loading

0 comments on commit a0c6a33

Please sign in to comment.