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

Commit 2dd18d7

Browse files
committed
Started on V2, not using ASTs... (#115)
* Initial 2.0. * Add some classes. * Start using BuiltCollection|Value. * Add generics, inheritance. * Add methods (without bodies). * Added method bodies. * Further augment methods. * Add method parameters. * Added methods, bodies, and constructors. * Fix travis. * Add annotation support. * Add fields and constructor initializers. * Add e2e example.
1 parent 74229dc commit 2dd18d7

Some content is hidden

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

93 files changed

+3926
-7129
lines changed

.analysis_options

Lines changed: 0 additions & 29 deletions
This file was deleted.

.travis.yml

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,28 @@
11
language: dart
2-
32
dart:
4-
- 1.22.0
3+
- dev
54
- stable
5+
cache:
6+
directories:
7+
- $HOME/.pub-cache
8+
9+
branches:
10+
only: [master, v2]
11+
12+
# Check for analysis issues, run the test cases, and ensure `dartfmt` is run.
13+
dart_task:
14+
- test: --platform vm
15+
- dartanalyzer
16+
- dartfmt
617

7-
script: ./tool/presubmit.sh
18+
# The Dart language is constantly being worked on, and sometimes the dev and
19+
# stable builds don't agree on what is considered formatted or what is analysis
20+
# warning free (though this does not effect _using_ the library, likely).
21+
#
22+
# We exclude `dev` from analysis and formatting checks.
23+
matrix:
24+
allow_failure:
25+
- dart: dev
26+
dart_task: dartfmt
27+
- dart: dev
28+
dart_task: dartanalyzer

README.md

Lines changed: 3 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -6,78 +6,7 @@
66

77
`code_builder` is a fluent Dart API for generating valid Dart source code.
88

9-
Code generation was traditionally done through a series of
10-
package-specific string concatenations which usually results in messy
11-
and sometimes invalid Dart code that is not easily readable and is very
12-
difficult to refactor.
9+
**Experimental Branch for _2.0_**:
10+
- Drops use of the `package:analyzer` API.
11+
- Uses a proper [builder pattern](https://github.com/square/javapoet).
1312

14-
`code_builder` uses the [analyzer](analyzer) package to create real Dart
15-
language ASTs, which, while not guaranteed to be correct, always follows
16-
the analyzer's own understood format.
17-
18-
[analyzer]: https://pub.dartlang.org/packages/analyzer
19-
20-
## Contributing
21-
22-
* Read and help us document common patterns over [at the wiki][wiki].
23-
* Is there a *bug* in the code? [File an issue][issue].
24-
25-
If a feature is missing (the Dart language is always evolving) or you'd like an
26-
easier or better way to do something, consider [opening a pull request][pull].
27-
You can always [file an issue][issue], but generally speaking feature requests
28-
will be on a best-effort basis.
29-
30-
[wiki]: https://github.com/dart-lang/code_builder/wiki
31-
[issue]: https://github.com/dart-lang/code_builder/issues
32-
[pull]: https://github.com/dart-lang/code_builder/pulls
33-
34-
## Usage
35-
36-
Code builder has a narrow and user-friendly API.
37-
38-
For example creating a class with a method:
39-
40-
```dart
41-
var base = reference('Organism');
42-
var clazz = new ClassBuilder('Animal', asExtends: base);
43-
clazz.addMethod(
44-
new MethodBuilder.returnVoid(
45-
'eat',
46-
returns: reference('print').call([literal('Yum')]),
47-
),
48-
);
49-
```
50-
51-
Outputs:
52-
```dart
53-
class Animal extends Organism {
54-
void eat() => print('Yum!');
55-
}
56-
```
57-
58-
Have a complicated set of dependencies for your generated code?
59-
`code_builder` supports automatic scoping of your ASTs to automatically
60-
use prefixes to avoid symbol conflicts:
61-
62-
```dart
63-
var lib = new LibraryBuilder.scope()
64-
..addMembers([
65-
new MethodBuilder(
66-
'doThing',
67-
returnType: reference('Thing', 'package:thing/thing.dart'),
68-
),
69-
new MethodBuilder(
70-
'doOtherThing',
71-
returnType: reference('Thing', 'package:thing/alternative.dart'),
72-
),
73-
]);
74-
```
75-
76-
Outputs:
77-
```dart
78-
import 'package:thing/thing.dart' as _i1;
79-
import 'package:thing/alternative.dart' as _i2;
80-
81-
_i1.Thing doThing() {}
82-
_i2.Thing doOtherThing() {}
83-
```

analysis_options.yaml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
analyzer:
2+
strong-mode:
3+
implicit-casts: false
4+
implicit-dynamic: false
5+
6+
linter:
7+
rules:
8+
# Error Rules
9+
- avoid_empty_else
10+
- comment_references
11+
- control_flow_in_finally
12+
- empty_statements
13+
- hash_and_equals
14+
- invariant_booleans
15+
- iterable_contains_unrelated_type
16+
- list_remove_unrelated_type
17+
- no_adjacent_strings_in_list
18+
- no_duplicate_case_values
19+
- test_types_in_equals
20+
- throw_in_finally
21+
- unrelated_type_equality_checks
22+
- valid_regexps
23+
24+
# Style Rules
25+
- annotate_overrides
26+
- avoid_init_to_null
27+
- avoid_return_types_on_setters
28+
- camel_case_types
29+
- cascade_invocations
30+
- constant_identifier_names
31+
- directives_ordering
32+
- empty_catches
33+
- empty_constructor_bodies
34+
- implementation_imports
35+
- library_names
36+
- library_prefixes
37+
- non_constant_identifier_names
38+
- omit_local_variable_types
39+
- only_throw_errors
40+
- prefer_adjacent_string_concatenation
41+
- prefer_collection_literals
42+
- prefer_const_constructors
43+
- prefer_contains
44+
- prefer_final_fields
45+
- prefer_final_locals
46+
- prefer_initializing_formals
47+
- prefer_interpolation_to_compose_strings
48+
- prefer_is_empty
49+
- prefer_is_not_empty
50+
- recursive_getters
51+
- slash_for_doc_comments
52+
- type_init_formals
53+
- unnecessary_brace_in_string_interps
54+
- unnecessary_this

doc/SHORT_LINKS.md

Lines changed: 0 additions & 10 deletions
This file was deleted.

lib/code_builder.dart

Lines changed: 13 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,16 @@
1-
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
1+
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
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-
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, list, map, ExpressionBuilder, InvocationBuilder;
10-
export 'src/builders/field.dart'
11-
show varConst, varField, varFinal, FieldBuilder;
12-
export 'src/builders/file.dart'
13-
show
14-
ExportBuilder,
15-
ImportBuilder,
16-
LibraryBuilder,
17-
PartBuilder,
18-
PartOfBuilder,
19-
UriDirectiveBuilder;
20-
export 'src/builders/method.dart'
21-
show
22-
constructor,
23-
constructorNamed,
24-
getter,
25-
setter,
26-
thisField,
27-
lambda,
28-
method,
29-
named,
30-
ConstructorBuilder,
31-
MethodBuilder,
32-
MethodModifier,
33-
ValidMethodMember;
34-
export 'src/builders/parameter.dart'
35-
show parameter, FunctionParameterBuilder, ParameterBuilder;
36-
export 'src/pretty_printer.dart' show prettyToSource;
37-
export 'src/builders/reference.dart'
38-
show explicitThis, reference, ReferenceBuilder;
39-
export 'src/builders/shared.dart' show AstBuilder, Scope;
40-
export 'src/builders/statement.dart'
41-
show
42-
breakStatement,
43-
ifThen,
44-
elseIf,
45-
elseThen,
46-
returnVoid,
47-
switchCase,
48-
switchDefault,
49-
switchStatement,
50-
ForStatementBuilder,
51-
IfStatementBuilder,
52-
StatementBuilder,
53-
SwitchCaseBuilder,
54-
SwitchDefaultCaseBuilder,
55-
SwitchStatementBuilder,
56-
WhileStatementBuilder;
57-
export 'src/builders/type.dart'
58-
show NewInstanceBuilder, TypeBuilder, TypeDefBuilder;
5+
export 'src/base.dart' show Spec;
6+
export 'src/emitter.dart' show DartEmitter;
7+
export 'src/matchers.dart' show equalsDart;
8+
export 'src/specs/annotation.dart' show Annotation, AnnotationBuilder;
9+
export 'src/specs/class.dart' show Class, ClassBuilder;
10+
export 'src/specs/code.dart' show Code, CodeBuilder;
11+
export 'src/specs/constructor.dart' show Constructor, ConstructorBuilder;
12+
export 'src/specs/field.dart' show Field, FieldBuilder, FieldModifier;
13+
export 'src/specs/method.dart'
14+
show Method, MethodBuilder, MethodType, Parameter, ParameterBuilder;
15+
export 'src/specs/reference.dart' show Reference;
16+
export 'src/specs/type_reference.dart' show TypeReference, TypeReferenceBuilder;

lib/dart/async.dart

Lines changed: 0 additions & 47 deletions
This file was deleted.

0 commit comments

Comments
 (0)