diff --git a/CHANGELOG.md b/CHANGELOG.md index 20eb42c..2e0e129 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,12 @@ +## 3.0.0-alpha + +* Using `equalsDart` no longer formats automatically with `dartfmt`. + +* Removed deprecated `Annotation` and `File` classes. + +* `Method.lambda` is inferred based on `Method.body` where possible and now + defaults to `null`. + ## 2.4.0 * Add `equalTo`, `notEqualTo`, `greaterThan`, `lessThan`, `greateOrEqualTo`, and diff --git a/README.md b/README.md index 8c80d6a..036be72 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,6 @@ void main() { ..extend = refer('Organism') ..methods.add(new Method.returnsVoid((b) => b ..name = 'eat' - ..lambda = true ..body = const Code('print(\'Yum\')')))); final emitter = new DartEmitter(); print(new DartFormatter().format('${animal.accept(emitter)}')); diff --git a/example/example.dart b/example/example.dart index abf2be0..ccb77df 100644 --- a/example/example.dart +++ b/example/example.dart @@ -25,7 +25,6 @@ String animalClass() { ..extend = refer('Organism') ..methods.add(new Method.returnsVoid((b) => b ..name = 'eat' - ..lambda = null ..body = refer('print').call([literalString('Yum!')]).code))); return _dartfmt.format('${animal.accept(new DartEmitter())}'); } diff --git a/lib/code_builder.dart b/lib/code_builder.dart index 77edcbf..2b16c23 100644 --- a/lib/code_builder.dart +++ b/lib/code_builder.dart @@ -7,8 +7,6 @@ export 'src/base.dart' show lazySpec, Spec; export 'src/emitter.dart' show DartEmitter; export 'src/matchers.dart' show equalsDart, EqualsDart; export 'src/matchers.dart' show equalsDart; -// ignore: deprecated_member_use -export 'src/specs/annotation.dart' show Annotation, AnnotationBuilder; export 'src/specs/class.dart' show Class, ClassBuilder; export 'src/specs/code.dart' show lazyCode, Block, BlockBuilder, Code, StaticCode, ScopedCode; @@ -38,9 +36,7 @@ export 'src/specs/expression.dart' literalTrue, literalFalse; export 'src/specs/field.dart' show Field, FieldBuilder, FieldModifier; -// TODO: Remove File, FileBuilder in 3.0.0. -// ignore: deprecated_member_use -export 'src/specs/library.dart' show File, FileBuilder, Library, LibraryBuilder; +export 'src/specs/library.dart' show Library, LibraryBuilder; export 'src/specs/method.dart' show Method, diff --git a/lib/src/emitter.dart b/lib/src/emitter.dart index 814df89..82284a4 100644 --- a/lib/src/emitter.dart +++ b/lib/src/emitter.dart @@ -6,7 +6,6 @@ import 'package:meta/meta.dart'; import 'allocator.dart'; import 'base.dart'; -import 'specs/annotation.dart'; import 'specs/class.dart'; import 'specs/code.dart'; import 'specs/constructor.dart'; @@ -79,11 +78,7 @@ class DartEmitter extends Object @override visitAnnotation(Expression spec, [StringSink output]) { (output ??= new StringBuffer()).write('@'); - if (spec is Annotation) { - spec.code.accept(this, output); - } else { - spec.accept(this, output); - } + spec.accept(this, output); output.write(' '); return output; } @@ -281,12 +276,7 @@ class DartEmitter extends Object } @override - visitLibrary(Library spec, [StringSink output]) => visitFile(spec, output); - - @override - // TODO: Remove File in 3.0.0. - // ignore: deprecated_member_use - visitFile(File spec, [StringSink output]) { + visitLibrary(Library spec, [StringSink output]) { output ??= new StringBuffer(); // Process the body first in order to prime the allocators. final body = new StringBuffer(); diff --git a/lib/src/matchers.dart b/lib/src/matchers.dart index 49796c3..92b6547 100644 --- a/lib/src/matchers.dart +++ b/lib/src/matchers.dart @@ -2,7 +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:matcher/matcher.dart'; import 'base.dart'; @@ -23,23 +22,13 @@ Matcher equalsDart( /// /// See [EqualsDart.format] to specify the default source code formatter. class EqualsDart extends Matcher { - // TODO: Remove in 3.0.0. - static final _formatter = new DartFormatter(); - /// May override to provide a function to format Dart on [equalsDart]. /// - /// As of `2.x.x`, this defaults to using `dartfmt`, but in an upcoming - /// release (`3.x.x`) it will default to [collapseWhitespace]. This is in - /// order to avoid a dependency on specific versions of `dartfmt` and the - /// `analyzer` package. - /// - /// To future proof, see an example in code_builder's `test/common.dart`. + /// By default, uses [collapseWhitespace], but it is recommended to instead + /// use `dart_style` (dartfmt) where possible. See `test/common.dart` for an + /// example. static String Function(String) format = (String source) { - try { - return _formatter.format(source); - } on FormatException catch (_) { - return _formatter.formatStatement(source); - } + return collapseWhitespace(source); }; static String _format(String source) { diff --git a/lib/src/specs/annotation.dart b/lib/src/specs/annotation.dart deleted file mode 100644 index 1bf330b..0000000 --- a/lib/src/specs/annotation.dart +++ /dev/null @@ -1,56 +0,0 @@ -// Copyright (c) 2017, 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.specs.annotation; - -import 'package:built_value/built_value.dart'; -import 'package:meta/meta.dart'; - -import '../base.dart'; -import '../visitors.dart'; -import 'code.dart'; -import 'expression.dart'; - -part 'annotation.g.dart'; - -@Deprecated('Use an Expression node instead. Will be removed in 3.0.0.') -@immutable -abstract class Annotation extends Expression - implements Built, Spec { - factory Annotation([void updates(AnnotationBuilder b)]) = _$Annotation; - - Annotation._(); - - /// Part after the `@` in annotation. - @override - Code get code => codeForAnnotation; - - /// Use [code] instead. - /// - /// This property exists in order to keep compatibility with having a concrete - /// [Annotation] class, but also allow migrating to using [Expression] instead - /// to add annotations. - @protected - Code get codeForAnnotation; - - @override - R accept(SpecVisitor visitor, [R context]) { - return visitor.visitAnnotation(this, context); - } -} - -abstract class AnnotationBuilder - // ignore: deprecated_member_use - implements - Builder { - factory AnnotationBuilder() = _$AnnotationBuilder; - - AnnotationBuilder._(); - - Code get code => codeForAnnotation; - set code(Code code) => codeForAnnotation = code; - - @protected - Code codeForAnnotation; -} diff --git a/lib/src/specs/annotation.g.dart b/lib/src/specs/annotation.g.dart deleted file mode 100644 index 6368923..0000000 --- a/lib/src/specs/annotation.g.dart +++ /dev/null @@ -1,99 +0,0 @@ -// GENERATED CODE - DO NOT MODIFY BY HAND - -part of code_builder.src.specs.annotation; - -// ************************************************************************** -// Generator: BuiltValueGenerator -// ************************************************************************** - -// ignore_for_file: always_put_control_body_on_new_line -// ignore_for_file: annotate_overrides -// ignore_for_file: avoid_annotating_with_dynamic -// ignore_for_file: avoid_returning_this -// ignore_for_file: omit_local_variable_types -// ignore_for_file: prefer_expression_function_bodies -// ignore_for_file: sort_constructors_first - -class _$Annotation extends Annotation { - @override - final Code codeForAnnotation; - - factory _$Annotation([void updates(AnnotationBuilder b)]) => - (new AnnotationBuilder()..update(updates)).build() as _$Annotation; - - _$Annotation._({this.codeForAnnotation}) : super._() { - if (codeForAnnotation == null) - throw new ArgumentError.notNull('codeForAnnotation'); - } - - @override - Annotation rebuild(void updates(AnnotationBuilder b)) => - (toBuilder()..update(updates)).build(); - - @override - _$AnnotationBuilder toBuilder() => new _$AnnotationBuilder()..replace(this); - - @override - bool operator ==(dynamic other) { - if (identical(other, this)) return true; - if (other is! Annotation) return false; - return codeForAnnotation == other.codeForAnnotation; - } - - @override - int get hashCode { - return $jf($jc(0, codeForAnnotation.hashCode)); - } - - @override - String toString() { - return (newBuiltValueToStringHelper('Annotation') - ..add('codeForAnnotation', codeForAnnotation)) - .toString(); - } -} - -class _$AnnotationBuilder extends AnnotationBuilder { - _$Annotation _$v; - - @override - Code get codeForAnnotation { - _$this; - return super.codeForAnnotation; - } - - @override - set codeForAnnotation(Code codeForAnnotation) { - _$this; - super.codeForAnnotation = codeForAnnotation; - } - - _$AnnotationBuilder() : super._(); - - AnnotationBuilder get _$this { - if (_$v != null) { - super.codeForAnnotation = _$v.codeForAnnotation; - _$v = null; - } - return this; - } - - @override - void replace(Annotation other) { - if (other == null) throw new ArgumentError.notNull('other'); - _$v = other as _$Annotation; - } - - @override - void update(void updates(AnnotationBuilder b)) { - if (updates != null) updates(this); - } - - @override - _$Annotation build() { - final _$result = - _$v ?? new _$Annotation._(codeForAnnotation: codeForAnnotation); - replace(_$result); - return _$result; - } -} diff --git a/lib/src/specs/expression.dart b/lib/src/specs/expression.dart index 4b82fd6..50600a4 100644 --- a/lib/src/specs/expression.dart +++ b/lib/src/specs/expression.dart @@ -9,7 +9,6 @@ import 'package:meta/meta.dart'; import '../base.dart'; import '../emitter.dart'; import '../visitors.dart'; -import 'annotation.dart'; import 'code.dart'; import 'method.dart'; import 'reference.dart'; @@ -244,44 +243,6 @@ abstract class Expression implements Spec { ); } - /// Returns an annotation as a result of calling this constructor. - @Deprecated('Use "call" instead. Will be removed in 3.0.0.') - Expression annotation([ - Iterable positionalArguments, - Map namedArguments = const {}, - List typeArguments = const [], - ]) { - if (positionalArguments == null) { - return new Annotation((b) { - b.code = code; - }); - } - return new Annotation((b) { - b.code = new InvokeExpression._( - this, - positionalArguments.toList(), - namedArguments, - typeArguments, - ) - .code; - }); - } - - /// Returns an annotation as a result of calling a named constructor. - @Deprecated('Use a combination of "property" and "call". Removing in 3.0.0.') - Expression annotationNamed( - String name, - Iterable positionalArguments, [ - Map namedArguments = const {}, - List typeArguments = const [], - ]) { - // ignore: deprecated_member_use - return new Annotation((b) => b - ..code = new InvokeExpression._(this, positionalArguments.toList(), - namedArguments, typeArguments, name) - .code); - } - /// This expression preceded by `return`. Expression get returned { return new BinaryExpression._( diff --git a/lib/src/specs/library.dart b/lib/src/specs/library.dart index ae63180..180bc89 100644 --- a/lib/src/specs/library.dart +++ b/lib/src/specs/library.dart @@ -14,10 +14,10 @@ import 'directive.dart'; part 'library.g.dart'; -@Deprecated('Replace with "Library" by 3.0.0') -abstract class File implements Built, Spec { - factory File([void updates(FileBuilder b)]) = _$File; - File._(); +@immutable +abstract class Library implements Built, Spec { + factory Library([void updates(LibraryBuilder b)]) = _$Library; + Library._(); BuiltList get directives; BuiltList get body; @@ -27,68 +27,13 @@ abstract class File implements Built, Spec { SpecVisitor visitor, [ R context, ]) => - visitor.visitFile(this, context); + visitor.visitLibrary(this, context); } -@Deprecated('Replace with "LibraryBuilder" by 3.0.0') -abstract class FileBuilder implements Builder { - factory FileBuilder() = _$FileBuilder; - FileBuilder._(); - - ListBuilder directives = new ListBuilder(); - ListBuilder body = new ListBuilder(); -} - -@immutable -// TODO: Remove File in 3.0.0. -// ignore: deprecated_member_use -class Library extends _$File implements Spec { - factory Library([void updates(LibraryBuilder b)]) { - return (new LibraryBuilder()..update(updates)).build(); - } - - Library._({ - BuiltList directives, - BuiltList body, - }) - : super._(directives: directives, body: body); -} +abstract class LibraryBuilder implements Builder { + factory LibraryBuilder() = _$LibraryBuilder; + LibraryBuilder._(); -// TODO: Remove File in 3.0.0. -// -// This implementation is imperfect (not identical to the code emitted by -// the built_value package but seems to the only way to adhere to the -// inheritance chain without breaking the code generator). -// -// May be changed as a result of -// https://github.com/google/built_value.dart/issues/257. -// -// ignore: deprecated_member_use -class LibraryBuilder implements FileBuilder { - @override ListBuilder body = new ListBuilder(); - - @override ListBuilder directives = new ListBuilder(); - - @override - Library build() { - return new Library._(directives: directives.build(), body: body.build()); - } - - @override - void replace(covariant Library other) { - if (other == null) { - throw new ArgumentError.notNull('other'); - } - body = other.body.toBuilder(); - directives = other.directives.toBuilder(); - } - - @override - void update(void updates(LibraryBuilder builder)) { - if (updates != null) { - updates(this); - } - } } diff --git a/lib/src/specs/library.g.dart b/lib/src/specs/library.g.dart index bbe5c77..1fe24aa 100644 --- a/lib/src/specs/library.g.dart +++ b/lib/src/specs/library.g.dart @@ -14,31 +14,31 @@ part of code_builder.src.specs.library; // ignore_for_file: prefer_expression_function_bodies // ignore_for_file: sort_constructors_first -class _$File extends File { +class _$Library extends Library { @override final BuiltList directives; @override final BuiltList body; - factory _$File([void updates(FileBuilder b)]) => - (new FileBuilder()..update(updates)).build() as _$File; + factory _$Library([void updates(LibraryBuilder b)]) => + (new LibraryBuilder()..update(updates)).build() as _$Library; - _$File._({this.directives, this.body}) : super._() { + _$Library._({this.directives, this.body}) : super._() { if (directives == null) throw new ArgumentError.notNull('directives'); if (body == null) throw new ArgumentError.notNull('body'); } @override - File rebuild(void updates(FileBuilder b)) => + Library rebuild(void updates(LibraryBuilder b)) => (toBuilder()..update(updates)).build(); @override - _$FileBuilder toBuilder() => new _$FileBuilder()..replace(this); + _$LibraryBuilder toBuilder() => new _$LibraryBuilder()..replace(this); @override bool operator ==(dynamic other) { if (identical(other, this)) return true; - if (other is! File) return false; + if (other is! Library) return false; return directives == other.directives && body == other.body; } @@ -49,15 +49,15 @@ class _$File extends File { @override String toString() { - return (newBuiltValueToStringHelper('File') + return (newBuiltValueToStringHelper('Library') ..add('directives', directives) ..add('body', body)) .toString(); } } -class _$FileBuilder extends FileBuilder { - _$File _$v; +class _$LibraryBuilder extends LibraryBuilder { + _$Library _$v; @override ListBuilder get directives { @@ -83,9 +83,9 @@ class _$FileBuilder extends FileBuilder { super.body = body; } - _$FileBuilder() : super._(); + _$LibraryBuilder() : super._(); - FileBuilder get _$this { + LibraryBuilder get _$this { if (_$v != null) { super.directives = _$v.directives?.toBuilder(); super.body = _$v.body?.toBuilder(); @@ -95,20 +95,20 @@ class _$FileBuilder extends FileBuilder { } @override - void replace(File other) { + void replace(Library other) { if (other == null) throw new ArgumentError.notNull('other'); - _$v = other as _$File; + _$v = other as _$Library; } @override - void update(void updates(FileBuilder b)) { + void update(void updates(LibraryBuilder b)) { if (updates != null) updates(this); } @override - _$File build() { + _$Library build() { final _$result = _$v ?? - new _$File._(directives: directives?.build(), body: body?.build()); + new _$Library._(directives: directives?.build(), body: body?.build()); replace(_$result); return _$result; } diff --git a/lib/src/specs/method.dart b/lib/src/specs/method.dart index 7e3483e..31180a2 100644 --- a/lib/src/specs/method.dart +++ b/lib/src/specs/method.dart @@ -128,7 +128,9 @@ abstract class MethodBuilder extends Object bool external = false; /// Whether this method is a simple lambda expression. - bool lambda = false; + /// + /// If not specified this is inferred from the [body]. + bool lambda; /// Whether this method should be prefixed with `static`. /// diff --git a/lib/src/visitors.dart b/lib/src/visitors.dart index ab80f8d..62b47bc 100644 --- a/lib/src/visitors.dart +++ b/lib/src/visitors.dart @@ -30,9 +30,6 @@ abstract class SpecVisitor { T visitField(Field spec, [T context]); - @Deprecated('Replace with visitLibrary by 3.0.0') - T visitFile(File spec, [T context]); - T visitLibrary(Library spec, [T context]); T visitFunctionType(FunctionType spec, [T context]); diff --git a/pubspec.yaml b/pubspec.yaml index 8f50da9..8766be2 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: code_builder -version: 2.4.0 +version: 3.0.0-alpha description: A fluent API for generating Dart code author: Dart Team homepage: https://github.com/dart-lang/code_builder @@ -14,14 +14,13 @@ web: dependencies: built_collection: '>=1.0.0 <3.0.0' built_value: '>=2.0.0 <5.0.0' - # TODO: Move to dev_dependency in 3.0.0. - dart_style: ^1.0.0 matcher: ^0.12.0 meta: ^1.0.5 dev_dependencies: build_runner: '>=0.4.0 <0.7.0' built_value_generator: '>=2.0.0 <5.0.0' + dart_style: ^1.0.0 source_gen: '^0.7.0' test: ^0.12.0 diff --git a/test/e2e/injection_test.dart b/test/e2e/injection_test.dart index 0858e53..9b45bbf 100644 --- a/test/e2e/injection_test.dart +++ b/test/e2e/injection_test.dart @@ -33,7 +33,6 @@ void main() { refer('_module').property('get1').call([]), refer('_module').property('get2').call([]), ]).code - ..lambda = true ..returns = $Thing ..annotations.add(refer('override')))); diff --git a/test/specs/class_test.dart b/test/specs/class_test.dart index 3ae4974..49f6439 100644 --- a/test/specs/class_test.dart +++ b/test/specs/class_test.dart @@ -179,23 +179,6 @@ void main() { ); }); - test('should create a class with an annotated constructor [deprecated]', () { - expect( - new Class((b) => b - ..name = 'Foo' - ..constructors.add(new Constructor((b) => b - ..annotations.add( - new Annotation((b) => b..code = const Code('deprecated')), - )))), - equalsDart(r''' - class Foo { - @deprecated - Foo(); - } - '''), - ); - }); - test('should create a class with a annotated constructor', () { expect( new Class((b) => b diff --git a/test/specs/code/expression_test.dart b/test/specs/code/expression_test.dart index 920f1a6..d64cb64 100644 --- a/test/specs/code/expression_test.dart +++ b/test/specs/code/expression_test.dart @@ -252,9 +252,7 @@ void main() { expect( refer('map').property('putIfAbsent').call([ literalString('foo'), - new Method((b) => b - ..lambda = true - ..body = literalTrue.code).closure, + new Method((b) => b..body = literalTrue.code).closure, ]), equalsDart("map.putIfAbsent('foo', () => true)"), ); diff --git a/test/specs/method_test.dart b/test/specs/method_test.dart index 1ebcb85..9e87018 100644 --- a/test/specs/method_test.dart +++ b/test/specs/method_test.dart @@ -24,7 +24,6 @@ void main() { new Method((b) => b ..name = 'foo' ..modifier = MethodModifier.async - ..lambda = true ..body = literalNull.code), equalsDart(r''' foo() async => null @@ -37,7 +36,6 @@ void main() { new Method((b) => b ..name = 'foo' ..modifier = MethodModifier.asyncStar - ..lambda = true ..body = literalNull.code), equalsDart(r''' foo() async* => null @@ -50,7 +48,6 @@ void main() { new Method((b) => b ..name = 'foo' ..modifier = MethodModifier.syncStar - ..lambda = true ..body = literalNull.code), equalsDart(r''' foo() sync* => null @@ -62,7 +59,6 @@ void main() { expect( new Method((b) => b ..name = 'returnsTrue' - ..lambda = null ..returns = refer('bool') ..body = literalTrue.code), equalsDart(r''' @@ -75,7 +71,6 @@ void main() { expect( new Method.returnsVoid((b) => b ..name = 'assignTrue' - ..lambda = null ..body = refer('topLevelFoo').assign(literalTrue).statement), equalsDart(r''' void assignTrue() { @@ -227,7 +222,7 @@ void main() { ); }); - test('should create a lambda method', () { + test('should create a lambda method (explicitly)', () { expect( new Method((b) => b ..name = 'foo' @@ -270,24 +265,6 @@ void main() { ); }); - test('should create a method with an annotated parameter [deprecated]', () { - expect( - new Method( - (b) => b - ..name = 'fib' - ..requiredParameters.add( - new Parameter((b) => b - ..name = 'i' - ..annotations.add( - new Annotation((a) => a..code = const Code('deprecated')))), - ), - ), - equalsDart(r''' - fib(@deprecated i); - '''), - ); - }); - test('should create a method with an annotated parameter', () { expect( new Method( diff --git a/tool/src/codegen.dart b/tool/src/codegen.dart index 80edcf8..8d8bbd2 100644 --- a/tool/src/codegen.dart +++ b/tool/src/codegen.dart @@ -10,7 +10,7 @@ import 'package:source_gen/source_gen.dart'; BuildAction buildAction() { return new BuildAction( new PartBuilder([ - new BuiltValueGenerator(), + const BuiltValueGenerator(), ]), 'code_builder', inputs: const ['lib/src/specs/**.dart'],