Skip to content

Commit

Permalink
Address comments.
Browse files Browse the repository at this point in the history
Address comments and presubmit script.

.
  • Loading branch information
Matan Lurey authored and matanlurey committed Oct 27, 2016
1 parent 8b8613f commit cacaa75
Show file tree
Hide file tree
Showing 27 changed files with 143 additions and 89 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ dart:
- dev
- stable

script: ./tool/travis.sh
script: ./tool/presubmit.sh
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ var clazz = new ClassBuilder('Animal', asExtends: base);
clazz.addMethod(
new MethodBuilder.returnVoid(
'eat',
returns: reference('print).call([literal('Yum')]),
returns: reference('print').call([literal('Yum')]),
),
);
```
Expand Down
15 changes: 0 additions & 15 deletions lib/code_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
// 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:dart_style/dart_style.dart';
import 'package:meta/meta.dart';

export 'src/builders/annotation.dart' show AnnotationBuilder;
export 'src/builders/class.dart'
show asStatic, clazz, extend, implement, mixin, ClassBuilder;
Expand Down Expand Up @@ -34,15 +31,3 @@ export 'src/builders/shared.dart' show AstBuilder, Scope;
export 'src/builders/statement.dart'
show ifThen, elseIf, elseThen, IfStatementBuilder, StatementBuilder;
export 'src/builders/type.dart' show NewInstanceBuilder, TypeBuilder;

final _dartFmt = new DartFormatter();

/// Returns [source] formatted by `dartfmt`.
@visibleForTesting
String dartfmt(String source) {
try {
return _dartFmt.format(source);
} on FormatterException catch (_) {
return _dartFmt.formatStatement(source);
}
}
12 changes: 6 additions & 6 deletions lib/dart/async.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// 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.

/// Contains reference to the `dart:async` SDK for use in code generation.
/// Contains references to the `dart:async` SDK for use in code generation.
///
/// This library is currently *experimental*, and is subject to change; it is
/// currently manually maintained but there might be a strong use case for this
Expand All @@ -14,11 +14,11 @@
/// import 'package:code_builder/code_builder.dart';
/// import 'package:code_builder/dart/async.dart';
///
/// All references are _namespaced_ under [async]. Try it:
/// All references are _namespaced_ under [lib$async]. Try it:
/// // Outputs: new Future.value('Hello')
/// async.Future.newInstanceNamed('value', [literal('Hello')]);
///
/// If you are [missing a reference from `dart:async`](https://goo.gl/XbSfmT)
/// If you are [missing a symbol from `dart:async`](https://goo.gl/XbSfmT)
/// please send us a [pull request](https://goo.gl/2LvV7f) or
/// [file an issue](https://goo.gl/IooPfl).
library code_builder.dart.async;
Expand All @@ -27,10 +27,10 @@ import 'dart:async' as dart_async;

import 'package:code_builder/code_builder.dart';

/// A namespace for references in `dart:async`.
final DartAsync async = new DartAsync._();
/// References to `dart:async`.
final DartAsync lib$async = new DartAsync._();

/// References to the `dart:async` library for code generation. See [async].
/// References to the `dart:async` library for code generation. See [lib$async].
class DartAsync {
/// References [dart_async.Future].
final ReferenceBuilder Future = _ref('Future');
Expand Down
8 changes: 4 additions & 4 deletions lib/dart/core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// 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.

/// Contains reference to the `dart:core` SDK for use in code generation.
/// Contains references to the `dart:core` SDK for use in code generation.
///
/// This library is currently *experimental*, and is subject to change; it is
/// currently manually maintained but there might be a strong use case for this
Expand All @@ -14,7 +14,7 @@
/// import 'package:code_builder/code_builder.dart';
/// import 'package:code_builder/dart/core.dart';
///
/// All references are _namespaced_ under [core]. Try it:
/// All references are _namespaced_ under [lib$core]. Try it:
/// // Outputs: print('Hello World')
/// core.print.call([literal('Hello World')]);
///
Expand All @@ -28,9 +28,9 @@ import 'dart:core' as dart_core;
import 'package:code_builder/code_builder.dart';

/// A namespace for references in `dart:core`.
final DartCore core = new DartCore._();
final DartCore lib$core = new DartCore._();

/// References to the `dart:core` library for code generation. See [core].
/// References to the `dart:core` library for code generation. See [lib$core].
class DartCore {
/// References [dart_core.bool].
final ReferenceBuilder bool = _ref('bool');
Expand Down
4 changes: 2 additions & 2 deletions lib/src/builders/annotation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'package:code_builder/src/builders/class.dart';
import 'package:code_builder/src/builders/parameter.dart';
import 'package:code_builder/src/builders/shared.dart';

/// Lazily builds an [Annotation] AST when [buildAnnotation] is invoked.
/// Builds an [Annotation] AST when [buildAnnotation] is invoked.
abstract class AnnotationBuilder
implements ValidClassMember, ValidParameterMember {
/// Returns an [Annotation] AST representing the builder.
Expand Down Expand Up @@ -43,7 +43,7 @@ abstract class HasAnnotationsMixin implements HasAnnotations {
.toList();

/// Clones all annotations to [clone].
void cloneAnnotationsTo(HasAnnotations clone) {
void copyAnnotationsTo(HasAnnotations clone) {
clone.addAnnotations(_annotations);
}
}
4 changes: 2 additions & 2 deletions lib/src/builders/class.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ ClassBuilder clazz(
}

/// Wrap [member] to be emitted as a `static` method or field.
_StaticFieldWrapper asStatic(ValidClassMember member) {
ValidClassMember asStatic(ValidClassMember member) {
return new _StaticFieldWrapper(member);
}

Expand Down Expand Up @@ -198,7 +198,7 @@ class _ClassBuilderImpl extends Object
ClassDeclaration buildClass([Scope scope]) {
var extend = _extends;
if (extend == null && _with.isNotEmpty) {
extend = core.Object;
extend = lib$core.Object;
}
final clazz = new ClassDeclaration(
null,
Expand Down
16 changes: 13 additions & 3 deletions lib/src/builders/expression.dart
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,11 @@ abstract class AbstractExpressionMixin implements ExpressionBuilder {
StatementBuilder asAssert() => new _AsAssert(this);

@override
StatementBuilder asAssign(String variable) => new _AsAssign(this, variable);
StatementBuilder asAssign(
String variable, {
bool nullAware: false,
}) =>
new _AsAssign(this, variable, nullAware);

@override
StatementBuilder asConst(String variable, [TypeBuilder type]) {
Expand Down Expand Up @@ -162,7 +166,7 @@ abstract class AbstractExpressionMixin implements ExpressionBuilder {

@override
ExpressionBuilder identical(ExpressionBuilder other) {
return core.identical.call([
return lib$core.identical.call([
this,
other,
]);
Expand All @@ -183,6 +187,9 @@ abstract class AbstractExpressionMixin implements ExpressionBuilder {
@override
ExpressionBuilder negate() => new _NegateExpression(this);

@override
ExpressionBuilder negative() => new _NegativeExpression(this);

@override
ExpressionBuilder notEquals(ExpressionBuilder other) {
return new _AsBinaryExpression(
Expand All @@ -196,7 +203,7 @@ abstract class AbstractExpressionMixin implements ExpressionBuilder {
ExpressionBuilder parentheses() => new _ParenthesesExpression(this);
}

/// Lazily builds an [Expression] AST when [buildExpression] is invoked.
/// Builds an [Expression] AST when [buildExpression] is invoked.
abstract class ExpressionBuilder
implements AstBuilder, StatementBuilder, ValidParameterMember {
/// Returns as an [ExpressionBuilder] multiplying by [other].
Expand Down Expand Up @@ -266,6 +273,9 @@ abstract class ExpressionBuilder
/// Returns as an [ExpressionBuilder] negating using the `!` operator.
ExpressionBuilder negate();

/// Returns as an [ExpressionBuilder] negating the value,
ExpressionBuilder negative();

/// Returns as an [ExpressionBuilder] comparing using `!=` against [other].
ExpressionBuilder notEquals(ExpressionBuilder other);

Expand Down
7 changes: 4 additions & 3 deletions lib/src/builders/expression/assign.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
part of code_builder.src.builders.expression;

class _AsAssign extends AbstractExpressionMixin {
final ExpressionBuilder _value;
final String _name;
final bool _nullAware;
final ExpressionBuilder _value;

_AsAssign(this._value, this._name);
_AsAssign(this._value, this._name, this._nullAware);

@override
AstNode buildAst([Scope scope]) => buildExpression(scope);
Expand All @@ -13,7 +14,7 @@ class _AsAssign extends AbstractExpressionMixin {
Expression buildExpression([Scope scope]) {
return new AssignmentExpression(
stringIdentifier(_name),
$equals,
_nullAware ? $nullAwareEquals : $equals,
_value.buildExpression(scope),
);
}
Expand Down
17 changes: 17 additions & 0 deletions lib/src/builders/expression/negate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,20 @@ class _NegateExpression extends AbstractExpressionMixin {
);
}
}

class _NegativeExpression extends AbstractExpressionMixin {
final ExpressionBuilder _expression;

_NegativeExpression(this._expression);

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

@override
Expression buildExpression([Scope scope]) {
return new PrefixExpression(
$minus,
_expression.parentheses().buildExpression(scope),
);
}
}
4 changes: 2 additions & 2 deletions lib/src/builders/field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ FieldBuilder varField(
);
}

/// Short-hand for [FieldBuilder].
/// Short-hand for [FieldBuilder.asFinal].
FieldBuilder varFinal(
String name, {
TypeBuilder type,
Expand All @@ -35,7 +35,7 @@ FieldBuilder varFinal(
);
}

/// Short-hand for [FieldBuilder].
/// Short-hand for [FieldBuilder.asConst].
FieldBuilder varConst(
String name, {
TypeBuilder type,
Expand Down
4 changes: 2 additions & 2 deletions lib/src/builders/file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'package:analyzer/analyzer.dart';
import 'package:code_builder/src/builders/shared.dart';
import 'package:code_builder/src/tokens.dart';

/// Lazily builds a file of Dart source code.
/// Builds a file of Dart source code.
///
/// See [LibraryBuilder] and [PartBuilder] for concrete implementations.
abstract class FileBuilder implements AstBuilder<CompilationUnit> {
Expand All @@ -26,7 +26,7 @@ abstract class FileBuilder implements AstBuilder<CompilationUnit> {
}
}

/// Lazily builds a standalone file (library) of Dart source code.
/// Builds a standalone file (library) of Dart source code.
class LibraryBuilder extends FileBuilder {
final List<AstBuilder<Directive>> _directives = <AstBuilder<Directive>>[];
final Scope _scope;
Expand Down
4 changes: 2 additions & 2 deletions lib/src/builders/method.dart
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,12 @@ abstract class MethodBuilder
/// Creates a new [MethodBuilder] that returns `void`.
factory MethodBuilder.returnVoid(String name, {ExpressionBuilder returns}) {
if (returns == null) {
return new _MethodBuilderImpl(name, returns: core.$void);
return new _MethodBuilderImpl(name, returns: lib$core.$void);
}
return new _LambdaMethodBuilder(
name,
returns,
core.$void,
lib$core.$void,
null,
);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/builders/parameter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import 'package:code_builder/src/builders/shared.dart';
import 'package:code_builder/src/builders/type.dart';
import 'package:code_builder/src/tokens.dart';

/// A more short-hand way of constructing a [ParameterBuilder].
/// A short-hand way of constructing a [ParameterBuilder].
ParameterBuilder parameter(
String name, [
Iterable<ValidParameterMember> members = const [],
Expand Down
4 changes: 2 additions & 2 deletions lib/src/builders/type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ abstract class AbstractTypeBuilderMixin {
}

/// Invokes `const` on this type with a [name]d constructor.
NewInstanceBuilder constInstanceWith(
NewInstanceBuilder namedConstInstance(
String name,
Iterable<ExpressionBuilder> positional, [
Map<String, ExpressionBuilder> named = const {},
Expand All @@ -50,7 +50,7 @@ abstract class AbstractTypeBuilderMixin {
}

/// Invokes `new` on this type with a [name]d constructor.
NewInstanceBuilder newInstanceWith(
NewInstanceBuilder namedNewInstance(
String name,
Iterable<ExpressionBuilder> positional, [
Map<String, ExpressionBuilder> named = const {},
Expand Down
4 changes: 2 additions & 2 deletions lib/src/builders/type/new_instance.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ part of code_builder.src.builders.type;
///
/// See [TypeBuilder]:
/// - [TypeBuilder.constInstance]
/// - [TypeBuilder.constInstanceWith]
/// - [TypeBuilder.namedConstInstance]
/// - [TypeBuilder.newInstance]
/// - [TypeBuilder.newInstanceWith]
/// - [TypeBuilder.namedNewInstance]
abstract class NewInstanceBuilder
implements AnnotationBuilder, InvocationBuilder {
factory NewInstanceBuilder._const(TypeBuilder type, [String name]) {
Expand Down
12 changes: 11 additions & 1 deletion lib/src/pretty_printer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,21 @@

import 'package:analyzer/analyzer.dart';
import 'package:analyzer/dart/ast/token.dart';
import 'package:code_builder/code_builder.dart';
import 'package:dart_style/dart_style.dart';

import 'analyzer_patch.dart';

final _dartFmt = new DartFormatter();

/// Returns [source] formatted by `dartfmt`.
String dartfmt(String source) {
try {
return _dartFmt.format(source);
} on FormatterException catch (_) {
return _dartFmt.formatStatement(source);
}
}

/// Augments [AstNode.toSource] by adding some whitespace/line breaks.
///
/// The final result is run through `dartfmt`.
Expand Down
8 changes: 4 additions & 4 deletions lib/src/scope.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ Identifier identifier(Scope scope, String name, [String importFrom]) {
/// print(scope.identifier('Foo', 'package:foo/foo.dart'));
///
/// // May print 'i1.Bar'.
/// print(scope.identifier('Bar'), 'package:foo/foo.dart');
/// print(scope.identifier('Bar', 'package:foo/foo.dart'));
///
/// // May print 'i2.Bar'.
/// print(scope.getIdentifier('Baz'), 'package:bar/bar.dart');
/// print(scope.getIdentifier('Baz', 'package:bar/bar.dart'));
/// }
abstract class Scope {
/// A no-op [Scope]. Ideal for use for tests or example cases.
Expand All @@ -45,8 +45,8 @@ abstract class Scope {

/// Create a new scoping context.
///
/// Actual implementation of is _not_ guaranteed, only that all import
/// prefixes will be unique in a given scope.
/// Actual implementation of [Scope] is _not_ guaranteed, only that all
/// import prefixes will be unique in a given scope.
factory Scope() = _IncrementingScope;

/// Create a context that just collects and de-duplicates imports.
Expand Down
1 change: 1 addition & 0 deletions lib/testing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// BSD-style license that can be found in the LICENSE file.

import 'package:code_builder/code_builder.dart';
import 'package:code_builder/src/pretty_printer.dart';
import 'package:dart_style/dart_style.dart';
import 'package:matcher/matcher.dart';

Expand Down
Loading

0 comments on commit cacaa75

Please sign in to comment.