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

Commit c2beebd

Browse files
authored
Split FileBuilder into LibraryBuilder and PartBuilder (#3)
* Remove unused dependency. * Split FileBuilder into Library/PartBuilder. * Move FileBuilder#addDirectove to LibraryBuilder.
1 parent 8d023e6 commit c2beebd

File tree

5 files changed

+71
-79
lines changed

5 files changed

+71
-79
lines changed

lib/code_builder.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ library code_builder;
3131
import 'package:analyzer/analyzer.dart';
3232
import 'package:analyzer/dart/ast/token.dart';
3333
import 'package:analyzer/src/dart/ast/token.dart';
34-
import 'package:analyzer/src/generated/java_core.dart';
3534
import 'package:dart_style/dart_style.dart';
3635
import 'package:meta/meta.dart';
3736

lib/src/file_builder.dart

Lines changed: 65 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -4,73 +4,74 @@
44

55
part of code_builder;
66

7+
CompilationUnit _emptyCompilationUnit() => new CompilationUnit(
8+
null,
9+
null,
10+
null,
11+
null,
12+
null,
13+
);
14+
715
/// Builds files of Dart source code.
816
///
9-
/// Files may either be a standalone library or `part of` another library.
10-
class FileBuilder extends _AbstractCodeBuilder<CompilationUnit> {
11-
static Token _library = new KeywordToken(Keyword.LIBRARY, 0);
12-
static Token _part = new KeywordToken(Keyword.PART, 0);
13-
static Token _of = new StringToken(TokenType.KEYWORD, 'of', 0);
17+
/// See [LibraryBuilder] and [PartBuilder] for concrete implementations.
18+
abstract class FileBuilder extends _AbstractCodeBuilder<CompilationUnit> {
19+
FileBuilder._(CompilationUnit astNode) : super._(astNode);
20+
21+
/// Adds [declaration]'s resulting AST to the source.
22+
void addDeclaration(CodeBuilder<Declaration> declaration) {
23+
_astNode.declarations.add(declaration.toAst());
24+
}
25+
}
26+
27+
/// Builds a standalone Dart library [CompilationUnit] AST.
28+
class LibraryBuilder extends FileBuilder {
29+
static final Token _library = new KeywordToken(Keyword.LIBRARY, 0);
1430

1531
/// Create a new standalone Dart library, optionally with a [name].
16-
factory FileBuilder([String name]) {
32+
factory LibraryBuilder([String name]) {
1733
var astNode = _emptyCompilationUnit();
1834
if (name != null) {
1935
astNode.directives.add(new LibraryDirective(
20-
null,
21-
null,
22-
_library,
23-
new LibraryIdentifier([_stringId(name)]),
24-
null,
25-
));
36+
null,
37+
null,
38+
_library,
39+
new LibraryIdentifier([_stringId(name)]),
40+
null,));
2641
}
27-
return new FileBuilder._(astNode);
42+
return new LibraryBuilder._(astNode);
2843
}
2944

30-
/// Create a new `part of` the dart library with [name].
31-
factory FileBuilder.partOf(String name) {
32-
var astNode = _emptyCompilationUnit();
33-
astNode.directives.add(new PartOfDirective(
34-
null,
35-
null,
36-
_part,
37-
_of,
38-
new LibraryIdentifier([_stringId(name)]),
39-
null,
40-
));
41-
return new FileBuilder._(astNode);
42-
}
43-
44-
FileBuilder._(CompilationUnit astNode) : super._(astNode);
45+
LibraryBuilder._(CompilationUnit astNode) : super._(astNode);
4546

46-
/// Add a copy of [clazz] as a declaration in this file.
47-
void addClass(ClassBuilder clazz) {
48-
_astNode.declarations.add(clazz.toAst());
49-
}
50-
51-
/// Adds an `import` or `export` [directive].
47+
/// Adds [directive]'s resulting AST to the source.
5248
void addDirective(CodeBuilder<Directive> directive) {
53-
if (isPartOf) {
54-
throw const DartPartFileException._();
55-
}
5649
_astNode.directives.add(directive.toAst());
5750
}
51+
}
5852

59-
static CompilationUnit _emptyCompilationUnit() => new CompilationUnit(
60-
null,
61-
null,
53+
/// Builds a `part of` [CompilationUnit] AST for an existing Dart library.
54+
class PartBuilder extends FileBuilder {
55+
static final Token _part = new KeywordToken(Keyword.PART, 0);
56+
static final Token _of = new StringToken(TokenType.KEYWORD, 'of', 0);
57+
58+
/// Create a new `part of` source file.
59+
factory PartBuilder(String name) {
60+
var astNode = _emptyCompilationUnit();
61+
astNode.directives.add(new PartOfDirective(
6262
null,
6363
null,
64+
_part,
65+
_of,
66+
new LibraryIdentifier([_stringId(name)]),
6467
null,
65-
);
66-
67-
static bool _isPartOf(Directive d) => d is PartOfDirective;
68+
));
69+
return new PartBuilder._(astNode);
70+
}
6871

69-
/// Whether the file is `part of` another.
70-
bool get isPartOf => _astNode.directives.contains(_isPartOf);
72+
PartBuilder._(CompilationUnit astNode) : super._(astNode);
7173
}
72-
73-
/// An `export` directive in a [FileBuilder].
74+
/// An `export` directive in a [LibraryBuilder].
7475
class ExportBuilder extends _AbstractCodeBuilder<ExportDirective> {
7576
/// Create a new `export` directive exporting [uri].
7677
factory ExportBuilder(String uri) {
@@ -81,13 +82,13 @@ class ExportBuilder extends _AbstractCodeBuilder<ExportDirective> {
8182
ExportBuilder._(ExportDirective astNode) : super._(astNode);
8283

8384
static ExportDirective _createExportDirective() => new ExportDirective(
84-
null,
85-
null,
86-
null,
87-
null,
88-
null,
89-
null,
90-
null,
85+
null,
86+
null,
87+
null,
88+
null,
89+
null,
90+
null,
91+
null,
9192
);
9293
}
9394

@@ -111,23 +112,15 @@ class ImportBuilder extends _AbstractCodeBuilder<ImportDirective> {
111112
ImportBuilder._(ImportDirective astNode) : super._(astNode);
112113

113114
static ImportDirective _createImportDirective() => new ImportDirective(
114-
null,
115-
null,
116-
null,
117-
null,
118-
null,
119-
null,
120-
null,
121-
null,
122-
null,
123-
null,
115+
null,
116+
null,
117+
null,
118+
null,
119+
null,
120+
null,
121+
null,
122+
null,
123+
null,
124+
null,
124125
);
125126
}
126-
127-
/// Thrown when an invalid operation is attempted on a [FileBuilder] instance.
128-
class DartPartFileException implements Exception {
129-
const DartPartFileException._();
130-
131-
@override
132-
String toString() => 'Not a valid operation for a `part of` file.';
133-
}

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: code_builder
2-
version: 0.1.0-dev
2+
version: 0.1.0-dev+1
33
description: A fluent API for generating Dart code
44
author: Dart Team <[email protected]>
55
homepage: https://github.com/dart-lang/code_builder

test/builders/file_builder_test.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ import 'package:test/test.dart';
88

99
void main() {
1010
test('should emit a blank file', () {
11-
expect(new FileBuilder(), equalsSource(''));
11+
expect(new LibraryBuilder(), equalsSource(''));
1212
});
1313

1414
test('should emit a file with a library directive', () {
1515
expect(
16-
new FileBuilder('code_builder'), equalsSource('library code_builder;'));
16+
new LibraryBuilder('code_builder'), equalsSource('library code_builder;'));
1717
});
1818

1919
test('should emit a file with a part of directive', () {
2020
expect(
21-
new FileBuilder.partOf('code_builder'),
21+
new PartBuilder('code_builder'),
2222
equalsSource('part of code_builder;'),
2323
);
2424
});

test/integration_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ void main() {
3232
],
3333
)),
3434
);
35-
var lib = new FileBuilder()
35+
var lib = new LibraryBuilder()
3636
..addDirective(
3737
new ImportBuilder('app.dart'),
3838
)
39-
..addClass(clazz);
39+
..addDeclaration(clazz);
4040
expect(
4141
lib,
4242
equalsSource(

0 commit comments

Comments
 (0)