Skip to content
This repository has been archived by the owner on Nov 1, 2024. It is now read-only.

Commit

Permalink
Split FileBuilder into LibraryBuilder and PartBuilder (#3)
Browse files Browse the repository at this point in the history
* Remove unused dependency.

* Split FileBuilder into Library/PartBuilder.

* Move FileBuilder#addDirectove to LibraryBuilder.
  • Loading branch information
matanlurey authored Sep 14, 2016
1 parent 8d023e6 commit c2beebd
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 79 deletions.
1 change: 0 additions & 1 deletion lib/code_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ library code_builder;
import 'package:analyzer/analyzer.dart';
import 'package:analyzer/dart/ast/token.dart';
import 'package:analyzer/src/dart/ast/token.dart';
import 'package:analyzer/src/generated/java_core.dart';
import 'package:dart_style/dart_style.dart';
import 'package:meta/meta.dart';

Expand Down
137 changes: 65 additions & 72 deletions lib/src/file_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,73 +4,74 @@

part of code_builder;

CompilationUnit _emptyCompilationUnit() => new CompilationUnit(
null,
null,
null,
null,
null,
);

/// Builds files of Dart source code.
///
/// Files may either be a standalone library or `part of` another library.
class FileBuilder extends _AbstractCodeBuilder<CompilationUnit> {
static Token _library = new KeywordToken(Keyword.LIBRARY, 0);
static Token _part = new KeywordToken(Keyword.PART, 0);
static Token _of = new StringToken(TokenType.KEYWORD, 'of', 0);
/// See [LibraryBuilder] and [PartBuilder] for concrete implementations.
abstract class FileBuilder extends _AbstractCodeBuilder<CompilationUnit> {
FileBuilder._(CompilationUnit astNode) : super._(astNode);

/// Adds [declaration]'s resulting AST to the source.
void addDeclaration(CodeBuilder<Declaration> declaration) {
_astNode.declarations.add(declaration.toAst());
}
}

/// Builds a standalone Dart library [CompilationUnit] AST.
class LibraryBuilder extends FileBuilder {
static final Token _library = new KeywordToken(Keyword.LIBRARY, 0);

/// Create a new standalone Dart library, optionally with a [name].
factory FileBuilder([String name]) {
factory LibraryBuilder([String name]) {
var astNode = _emptyCompilationUnit();
if (name != null) {
astNode.directives.add(new LibraryDirective(
null,
null,
_library,
new LibraryIdentifier([_stringId(name)]),
null,
));
null,
null,
_library,
new LibraryIdentifier([_stringId(name)]),
null,));
}
return new FileBuilder._(astNode);
return new LibraryBuilder._(astNode);
}

/// Create a new `part of` the dart library with [name].
factory FileBuilder.partOf(String name) {
var astNode = _emptyCompilationUnit();
astNode.directives.add(new PartOfDirective(
null,
null,
_part,
_of,
new LibraryIdentifier([_stringId(name)]),
null,
));
return new FileBuilder._(astNode);
}

FileBuilder._(CompilationUnit astNode) : super._(astNode);
LibraryBuilder._(CompilationUnit astNode) : super._(astNode);

/// Add a copy of [clazz] as a declaration in this file.
void addClass(ClassBuilder clazz) {
_astNode.declarations.add(clazz.toAst());
}

/// Adds an `import` or `export` [directive].
/// Adds [directive]'s resulting AST to the source.
void addDirective(CodeBuilder<Directive> directive) {
if (isPartOf) {
throw const DartPartFileException._();
}
_astNode.directives.add(directive.toAst());
}
}

static CompilationUnit _emptyCompilationUnit() => new CompilationUnit(
null,
null,
/// Builds a `part of` [CompilationUnit] AST for an existing Dart library.
class PartBuilder extends FileBuilder {
static final Token _part = new KeywordToken(Keyword.PART, 0);
static final Token _of = new StringToken(TokenType.KEYWORD, 'of', 0);

/// Create a new `part of` source file.
factory PartBuilder(String name) {
var astNode = _emptyCompilationUnit();
astNode.directives.add(new PartOfDirective(
null,
null,
_part,
_of,
new LibraryIdentifier([_stringId(name)]),
null,
);

static bool _isPartOf(Directive d) => d is PartOfDirective;
));
return new PartBuilder._(astNode);
}

/// Whether the file is `part of` another.
bool get isPartOf => _astNode.directives.contains(_isPartOf);
PartBuilder._(CompilationUnit astNode) : super._(astNode);
}

/// An `export` directive in a [FileBuilder].
/// An `export` directive in a [LibraryBuilder].
class ExportBuilder extends _AbstractCodeBuilder<ExportDirective> {
/// Create a new `export` directive exporting [uri].
factory ExportBuilder(String uri) {
Expand All @@ -81,13 +82,13 @@ class ExportBuilder extends _AbstractCodeBuilder<ExportDirective> {
ExportBuilder._(ExportDirective astNode) : super._(astNode);

static ExportDirective _createExportDirective() => new ExportDirective(
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
);
}

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

static ImportDirective _createImportDirective() => new ImportDirective(
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
);
}

/// Thrown when an invalid operation is attempted on a [FileBuilder] instance.
class DartPartFileException implements Exception {
const DartPartFileException._();

@override
String toString() => 'Not a valid operation for a `part of` file.';
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: code_builder
version: 0.1.0-dev
version: 0.1.0-dev+1
description: A fluent API for generating Dart code
author: Dart Team <misc@dartlang.org>
homepage: https://github.com/dart-lang/code_builder
Expand Down
6 changes: 3 additions & 3 deletions test/builders/file_builder_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ import 'package:test/test.dart';

void main() {
test('should emit a blank file', () {
expect(new FileBuilder(), equalsSource(''));
expect(new LibraryBuilder(), equalsSource(''));
});

test('should emit a file with a library directive', () {
expect(
new FileBuilder('code_builder'), equalsSource('library code_builder;'));
new LibraryBuilder('code_builder'), equalsSource('library code_builder;'));
});

test('should emit a file with a part of directive', () {
expect(
new FileBuilder.partOf('code_builder'),
new PartBuilder('code_builder'),
equalsSource('part of code_builder;'),
);
});
Expand Down
4 changes: 2 additions & 2 deletions test/integration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ void main() {
],
)),
);
var lib = new FileBuilder()
var lib = new LibraryBuilder()
..addDirective(
new ImportBuilder('app.dart'),
)
..addClass(clazz);
..addDeclaration(clazz);
expect(
lib,
equalsSource(
Expand Down

0 comments on commit c2beebd

Please sign in to comment.