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

Commit 62d8db1

Browse files
authored
Rewrite: 1.0.0-alpha, with full statement support, fluid syntax (#15)
* Branch to start v1, a partial rewrite. * Incremental work. * Start on new v1 * . * . * . * . * . * . * Another checkpoint. * Another checkpoint with if statements. * Next large incremental work. * Fix docs, more dart core types. * Add scoping back. * Add e2e test. * Update docs, pubspec. * Address comments. Address comments and presubmit script. . * . * .
1 parent e2c28f2 commit 62d8db1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+4013
-2578
lines changed

.gitignore

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# See https://www.dartlang.org/tools/private-files.html
2-
31
# Files and directories created by pub
42
.buildlog
53
.packages
@@ -8,23 +6,9 @@
86
**/build
97
**/packages
108

11-
# Files created by dart2js
12-
# (Most Dart developers will use pub build to compile Dart, use/modify these
13-
# rules if you intend to use dart2js directly
14-
# Convention is to use extension '.dart.js' for Dart compiled to Javascript to
15-
# differentiate from explicit Javascript files)
16-
*.dart.js
17-
*.part.js
18-
*.js.deps
19-
*.js.map
20-
*.info.json
21-
229
# Directory created by dartdoc
2310
doc/api/
2411

2512
# Don't commit pubspec lock file
2613
# (Library packages only! Remove pattern if developing an application package)
2714
pubspec.lock
28-
29-
*.iml
30-
.idea

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ dart:
44
- dev
55
- stable
66

7-
script: ./tool/travis.sh
7+
script: ./tool/presubmit.sh

CHANGELOG.md

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

3+
## 1.0.0-alpha
4+
5+
- Large refactor that makes the library more feature complete.
6+
37
## 0.1.1
48

59
- Add concept of `Scope` and change `toAst` to support it

README.md

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# code_builder
22

3+
[![pub package](https://img.shields.io/pub/v/code_builder.svg)](https://pub.dartlang.org/packages/code_builder)
34
[![Build Status](https://travis-ci.org/dart-lang/code_builder.svg)](https://travis-ci.org/dart-lang/code_builder)
4-
[![Coverage Status](https://coveralls.io/repos/dart-lang/code_builder/badge.svg)](https://coveralls.io/r/dart-lang/code_builder)
5+
[![Coverage Status](https://coveralls.io/repos/github/dart-lang/code_builder/badge.svg?branch=master)](https://coveralls.io/github/dart-lang/code_builder?branch=master)
56

67
`code_builder` is a fluent Dart API for generating valid Dart source code.
78

@@ -34,10 +35,14 @@ Code builder has a narrow and user-friendly API.
3435
For example creating a class with a method:
3536

3637
```dart
37-
new ClassBuilder('Animal', extends: 'Organism')
38-
..addMethod(new MethodBuilder.returnVoid('eat')
39-
..setExpression(new ExpressionBuilder.invoke('print',
40-
positional: [new LiteralString('Yum!')])));
38+
var base = reference('Organism');
39+
var clazz = new ClassBuilder('Animal', asExtends: base);
40+
clazz.addMethod(
41+
new MethodBuilder.returnVoid(
42+
'eat',
43+
returns: reference('print').call([literal('Yum')]),
44+
),
45+
);
4146
```
4247

4348
Outputs:
@@ -53,26 +58,16 @@ use prefixes to avoid symbol conflicts:
5358

5459
```dart
5560
var lib = new LibraryBuilder.scope()
56-
..addDeclaration(new MethodBuilder(
57-
name: 'doThing',
58-
returns: new TypeBuilder(
59-
'Thing',
60-
importFrom: 'package:thing/thing.dart',
61-
),
62-
))
63-
..addDeclaration(new MethodBuilder(
64-
name: 'doOtherThing',
65-
returns: new TypeBuilder(
66-
'Thing',
67-
importFrom: 'package:thing/alternative.dart',
68-
))
69-
..addParameter(new ParameterBuilder(
70-
'thing',
71-
type: new TypeBuilder(
72-
'Thing',
73-
importFrom: 'package:thing/thing.dart',
74-
),
75-
)));
61+
..addMembers([
62+
new MethodBuilder(
63+
'doThing',
64+
returnType: reference('Thing', 'package:thing/thing.dart'),
65+
),
66+
new MethodBuilder(
67+
'doOtherThing',
68+
returnType: reference('Thing', 'package:thing/alternative.dart'),
69+
),
70+
]);
7671
```
7772

7873
Outputs:
@@ -81,5 +76,5 @@ import 'package:thing/thing.dart' as _i1;
8176
import 'package:thing/alternative.dart' as _i2;
8277
8378
_i1.Thing doThing() {}
84-
_i2.Thing doOtherThing(_i1.Thing thing) {}
79+
_i2.Thing doOtherThing() {}
8580
```

analysis_options.yaml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ linter:
1515
- valid_regexps
1616
- always_declare_return_types
1717
- annotate_overrides
18-
- avoid_as
1918
- avoid_init_to_null
2019
- avoid_return_types_on_setters
2120
- await_only_futures
@@ -25,17 +24,10 @@ linter:
2524
- empty_constructor_bodies
2625
- library_names
2726
- library_prefixes
28-
- non_constant_identifier_names
2927
- only_throw_errors
3028
- overridden_fields
31-
- package_api_docs
3229
- package_prefixed_library_names
3330
- prefer_is_not_empty
34-
- public_member_api_docs
3531
- slash_for_doc_comments
36-
- sort_constructors_first
37-
- sort_unnamed_constructors_first
3832
- type_init_formals
39-
- unnecessary_brace_in_string_interp
4033
- unnecessary_getters_setters
41-
- package_names

doc/SHORT_LINKS.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
When using `goo.gl` links in the library, please re-use the following:
2+
3+
- References to the Dart Library
4+
- `dart:async`: https://goo.gl/Ulqbfz
5+
- `dart:core`: https://goo.gl/XbSfmT
6+
7+
- References to Github
8+
- CONTRIBUTING.md: https://goo.gl/2LvV7f
9+
- File an issue to update `dart/*.dart`: https://goo.gl/Xc6xAz
10+

lib/code_builder.dart

Lines changed: 29 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -2,69 +2,32 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
/// Code builder is a fluent Dart API for generating valid Dart source code.
6-
///
7-
/// Generally speaking, code generation usually is done through a series of
8-
/// string concatenation which results in messy and sometimes invalid code that
9-
/// is not easily readable.
10-
///
11-
/// Code builder uses the `analyzer` package to create real Dart language ASTs,
12-
/// which, while not guaranteed to be correct, always follows the analyzer's
13-
/// own understood format.
14-
///
15-
/// Code builder also adds a more narrow and user-friendly API. For example
16-
/// creating a class with a method is an easy affair:
17-
/// new ClassBuilder('Animal', extends: 'Organism')
18-
/// ..addMethod(new MethodBuilder.returnVoid('eat')
19-
/// ..setExpression(new ExpressionBuilder.invoke('print',
20-
/// positional: [new LiteralString('Yum!')])));
21-
///
22-
/// Outputs:
23-
/// class Animal extends Organism {
24-
/// void eat() => print('Yum!');
25-
/// }
26-
///
27-
/// This package is in development and APIs are subject to frequent change. See
28-
/// the `README.md` and `CONTRIBUTING.md` for more information.
29-
library code_builder;
30-
31-
import 'package:analyzer/analyzer.dart';
32-
import 'package:analyzer/dart/ast/token.dart';
33-
import 'package:dart_style/dart_style.dart';
34-
import 'package:meta/meta.dart';
35-
36-
import 'src/analyzer_patch.dart';
37-
import 'src/tokens.dart';
38-
39-
part 'src/builders/annotation_builder.dart';
40-
part 'src/builders/class_builder.dart';
41-
part 'src/builders/constructor_builder.dart';
42-
part 'src/builders/expression_builder.dart';
43-
part 'src/builders/field_builder.dart';
44-
part 'src/builders/file_builder.dart';
45-
part 'src/builders/method_builder.dart';
46-
part 'src/builders/parameter_builder.dart';
47-
part 'src/builders/statement_builder.dart';
48-
part 'src/builders/type_builder.dart';
49-
part 'src/pretty_printer.dart';
50-
part 'src/scope.dart';
51-
52-
final DartFormatter _dartfmt = new DartFormatter();
53-
54-
// Simplifies some of the builders by having a mutable node we clone from.
55-
/// Returns [source] formatted by `dartfmt`.
56-
@visibleForTesting
57-
String dartfmt(String source) => _dartfmt.format(source);
58-
59-
SimpleIdentifier _stringIdentifier(String s) =>
60-
new SimpleIdentifier(stringToken(s));
61-
62-
Literal _stringLiteral(String s) => new SimpleStringLiteral(stringToken(s), s);
63-
64-
/// Base class for building and emitting a Dart language [AstNode].
65-
abstract class CodeBuilder<A extends AstNode> {
66-
/// Returns a copy-safe [AstNode] representing the current builder state.
67-
///
68-
/// Uses [scope] to output an AST re-written to use appropriate prefixes.
69-
A toAst([Scope scope = const Scope.identity()]);
70-
}
5+
export 'src/builders/annotation.dart' show AnnotationBuilder;
6+
export 'src/builders/class.dart'
7+
show asStatic, clazz, extend, implement, mixin, ClassBuilder;
8+
export 'src/builders/expression.dart'
9+
show literal, ExpressionBuilder, InvocationBuilder;
10+
export 'src/builders/field.dart'
11+
show varConst, varField, varFinal, FieldBuilder;
12+
export 'src/builders/file.dart' show ImportBuilder, LibraryBuilder, PartBuilder;
13+
export 'src/builders/method.dart'
14+
show
15+
constructor,
16+
constructorNamed,
17+
getter,
18+
setter,
19+
thisField,
20+
lambda,
21+
method,
22+
named,
23+
ConstructorBuilder,
24+
MethodBuilder,
25+
ValidMethodMember;
26+
export 'src/builders/parameter.dart' show parameter, ParameterBuilder;
27+
export 'src/pretty_printer.dart' show prettyToSource;
28+
export 'src/builders/reference.dart'
29+
show explicitThis, reference, ReferenceBuilder;
30+
export 'src/builders/shared.dart' show AstBuilder, Scope;
31+
export 'src/builders/statement.dart'
32+
show ifThen, elseIf, elseThen, IfStatementBuilder, StatementBuilder;
33+
export 'src/builders/type.dart' show NewInstanceBuilder, TypeBuilder;

lib/dart/async.dart

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
5+
/// Contains references to the `dart:async` SDK for use in code generation.
6+
///
7+
/// This library is currently *experimental*, and is subject to change; it is
8+
/// currently manually maintained but there might be a strong use case for this
9+
/// to be automatically generated (at least partially) in the near future.
10+
///
11+
/// ## Usage
12+
///
13+
/// First import the library:
14+
/// import 'package:code_builder/code_builder.dart';
15+
/// import 'package:code_builder/dart/async.dart';
16+
///
17+
/// All references are _namespaced_ under [lib$async]. Try it:
18+
/// // Outputs: new Future.value('Hello')
19+
/// async.Future.newInstanceNamed('value', [literal('Hello')]);
20+
///
21+
/// If you are [missing a symbol from `dart:async`](https://goo.gl/XbSfmT)
22+
/// please send us a [pull request](https://goo.gl/2LvV7f) or
23+
/// [file an issue](https://goo.gl/IooPfl).
24+
library code_builder.dart.async;
25+
26+
import 'dart:async' as dart_async;
27+
28+
import 'package:code_builder/code_builder.dart';
29+
30+
/// References to `dart:async`.
31+
final DartAsync lib$async = new DartAsync._();
32+
33+
/// References to the `dart:async` library for code generation. See [lib$async].
34+
class DartAsync {
35+
/// References [dart_async.Future].
36+
final ReferenceBuilder Future = _ref('Future');
37+
38+
DartAsync._();
39+
40+
static ReferenceBuilder _ref(String name) => reference(name, 'dart:async');
41+
}

0 commit comments

Comments
 (0)