refactor(dart/transform): Rename parser.dart > ng_deps.dart

- Rename the file that defines NgDeps from parser.dart to ng_deps.dart.
- Remove the `Parser` class and replace with the static `NgDeps.parse`
  method.
This commit is contained in:
Tim Blasi 2015-06-10 12:46:43 -07:00
parent f2371487a1
commit 950f2a38cd
6 changed files with 30 additions and 73 deletions

View File

@ -3,15 +3,14 @@ library angular2.transform.bind_generator.generator;
import 'dart:async';
import 'package:angular2/src/transform/common/asset_reader.dart';
import 'package:angular2/src/transform/common/parser.dart';
import 'package:angular2/src/transform/common/ng_deps.dart';
import 'package:angular2/src/transform/common/property_utils.dart' as prop;
import 'package:barback/barback.dart';
import 'visitor.dart';
Future<String> createNgSetters(AssetReader reader, AssetId entryPoint) async {
var parser = new Parser(reader);
NgDeps ngDeps = await parser.parse(entryPoint);
NgDeps ngDeps = await NgDeps.parse(reader, entryPoint);
String code = ngDeps.code;
var setters = _generateSetters(_createBindMap(ngDeps));
@ -41,7 +40,7 @@ List<String> _generateSetters(Map<String, String> bindMap) {
return setters;
}
/// Collapses all `bindProperties` in {@link ngDeps} into a map where the keys are
/// Collapses all `properties` in {@link ngDeps} into a map where the keys are
/// the bind properties and the values are either the one and only type
/// binding to that property or the empty string.
Map<String, String> _createBindMap(NgDeps ngDeps) {
@ -51,6 +50,8 @@ Map<String, String> _createBindMap(NgDeps ngDeps) {
visitor.bindConfig.clear();
t.annotations.accept(visitor);
visitor.bindConfig.forEach((String config) {
// See comments for `Directive` in annotations_impl/annotations.ts for
// details on how `properties` is specified.
var prop;
var idx = config.indexOf(':');
if (idx > 0) {

View File

@ -4,64 +4,13 @@ import 'dart:async';
import 'package:analyzer/analyzer.dart';
import 'package:angular2/src/transform/common/asset_reader.dart';
import 'package:angular2/src/transform/common/logging.dart';
import 'package:angular2/src/transform/common/names.dart';
import 'package:barback/barback.dart';
import 'package:code_transformers/assets.dart';
import 'registered_type.dart';
export 'registered_type.dart';
/// A parser that reads `.ng_deps.dart` files (represented by {@link AssetId}s into
/// easier to manage {@link NgDeps} files.
class Parser {
final AssetReader _reader;
final _ParseNgDepsVisitor _visitor = new _ParseNgDepsVisitor();
Parser(AssetReader this._reader);
/// Parses the `.ng_deps.dart` file represented by `id` and all of the `
/// .ng_deps.dart` files that it imports.
Future<List<NgDeps>> parseRecursive(AssetId id) async {
return _recurse(id);
}
/// Parses only the `.ng_deps.dart` file represented by `id`.
/// See also {@link parseRecursive}.
Future<NgDeps> parse(AssetId id) async {
if (!(await _reader.hasInput(id))) return null;
var ngDeps = new NgDeps(await _reader.readAsString(id));
_visitor.ngDeps = ngDeps;
parseCompilationUnit(ngDeps.code, name: id.path).accept(_visitor);
return ngDeps;
}
/// Parses the `.ng_deps.dart` file represented by {@link id} into an {@link NgDeps}
/// object. All `.ng_deps.dart` files imported by {@link id} are then parsed. The
/// results are added to {@link allDeps}.
Future<List<NgDeps>> _recurse(AssetId id,
[List<NgDeps> allDeps, Set<AssetId> seen]) async {
if (seen == null) seen = new Set<AssetId>();
if (seen.contains(id)) return null;
seen.add(id);
if (allDeps == null) allDeps = [];
var ngDeps = await parse(id);
allDeps.add(ngDeps);
var toWait = [];
ngDeps.imports.forEach((ImportDirective node) {
var uri = stringLiteralToString(node.uri);
if (uri.endsWith(DEPS_EXTENSION)) {
var importId = uriToAssetId(id, uri, logger, null);
toWait.add(_recurse(importId, allDeps, seen));
}
});
return Future.wait(toWait).then((_) => allDeps);
}
}
/// The contents of a `.ng_deps.dart` file.
class NgDeps {
final String code;
@ -71,6 +20,17 @@ class NgDeps {
LibraryDirective lib = null;
FunctionDeclaration setupMethod = null;
static _ParseNgDepsVisitor _visitor = new _ParseNgDepsVisitor();
/// Parses only the `.ng_deps.dart` file represented by `id`.
static Future<NgDeps> parse(AssetReader reader, AssetId id) async {
if (!(await reader.hasInput(id))) return null;
var ngDeps = new NgDeps(await reader.readAsString(id));
_visitor.ngDeps = ngDeps;
parseCompilationUnit(ngDeps.code, name: id.path).accept(_visitor);
return ngDeps;
}
NgDeps(this.code);
}

View File

@ -6,7 +6,7 @@ import 'package:analyzer/analyzer.dart';
import 'package:angular2/src/transform/common/asset_reader.dart';
import 'package:angular2/src/transform/common/logging.dart';
import 'package:angular2/src/transform/common/names.dart';
import 'package:angular2/src/transform/common/parser.dart';
import 'package:angular2/src/transform/common/ng_deps.dart';
import 'package:barback/barback.dart';
import 'package:code_transformers/assets.dart';
import 'package:path/path.dart' as path;
@ -23,8 +23,7 @@ import 'package:path/path.dart' as path;
/// Since `@Directive` and `@Component` inherit from `@Injectable`, we know
/// we will not miss processing any classes annotated with those tags.
Future<bool> isNecessary(AssetReader reader, AssetId entryPoint) async {
var parser = new Parser(reader);
NgDeps ngDeps = await parser.parse(entryPoint);
NgDeps ngDeps = await NgDeps.parse(reader, entryPoint);
if (ngDeps.registeredTypes.isNotEmpty) return true;
@ -49,8 +48,7 @@ Future<bool> isNecessary(AssetReader reader, AssetId entryPoint) async {
/// }
/// ```
Future<String> linkNgDeps(AssetReader reader, AssetId entryPoint) async {
var parser = new Parser(reader);
NgDeps ngDeps = await parser.parse(entryPoint);
NgDeps ngDeps = await NgDeps.parse(reader, entryPoint);
if (ngDeps == null) return null;

View File

@ -7,7 +7,7 @@ import 'package:angular2/src/render/api.dart';
import 'package:angular2/src/transform/common/asset_reader.dart';
import 'package:angular2/src/transform/common/logging.dart';
import 'package:angular2/src/transform/common/names.dart';
import 'package:angular2/src/transform/common/parser.dart';
import 'package:angular2/src/transform/common/ng_deps.dart';
import 'package:barback/barback.dart';
import 'package:code_transformers/assets.dart';
@ -20,17 +20,16 @@ import 'package:code_transformers/assets.dart';
/// `entryPoint`.
Future<Map<String, DirectiveMetadata>> extractDirectiveMetadata(
AssetReader reader, AssetId entryPoint) async {
return _extractDirectiveMetadataRecursive(
reader, new Parser(reader), entryPoint);
return _extractDirectiveMetadataRecursive(reader, entryPoint);
}
var _nullFuture = new Future.value(null);
Future<Map<String, DirectiveMetadata>> _extractDirectiveMetadataRecursive(
AssetReader reader, Parser parser, AssetId entryPoint) async {
AssetReader reader, AssetId entryPoint) async {
if (!(await reader.hasInput(entryPoint))) return null;
var ngDeps = await parser.parse(entryPoint);
var ngDeps = await NgDeps.parse(reader, entryPoint);
var baseMap = _metadataMapFromNgDeps(ngDeps);
return Future.wait(ngDeps.exports.map((export) {
@ -40,7 +39,7 @@ Future<Map<String, DirectiveMetadata>> _extractDirectiveMetadataRecursive(
uri = toDepsExtension(uri);
var assetId = uriToAssetId(entryPoint, uri, logger, null /* span */);
if (assetId == entryPoint) return _nullFuture;
return _extractDirectiveMetadataRecursive(reader, parser, assetId)
return _extractDirectiveMetadataRecursive(reader, assetId)
.then((exportMap) {
if (exportMap != null) {
if (baseMap == null) {

View File

@ -9,7 +9,7 @@ import 'package:angular2/src/render/dom/convert.dart';
import 'package:angular2/src/transform/common/asset_reader.dart';
import 'package:angular2/src/transform/common/logging.dart';
import 'package:angular2/src/transform/common/names.dart';
import 'package:angular2/src/transform/common/parser.dart';
import 'package:angular2/src/transform/common/ng_deps.dart';
import 'package:barback/barback.dart';
import 'package:code_transformers/assets.dart';
@ -50,7 +50,7 @@ class _ViewDefinitionCreator {
_ViewDefinitionCreator(AssetReader reader, AssetId entryPoint)
: this.reader = reader,
this.entryPoint = entryPoint,
ngDepsFuture = new Parser(reader).parse(entryPoint);
ngDepsFuture = NgDeps.parse(reader, entryPoint);
Future<ViewDefinitionResults> createViewDefs() async {
var ngDeps = await ngDepsFuture;

View File

@ -5,7 +5,7 @@ import 'package:angular2/src/render/api.dart';
import 'package:angular2/src/render/dom/convert.dart';
import 'package:angular2/src/transform/common/directive_metadata_reader.dart';
import 'package:angular2/src/transform/common/logging.dart';
import 'package:angular2/src/transform/common/parser.dart';
import 'package:angular2/src/transform/common/ng_deps.dart';
import 'package:angular2/src/transform/directive_metadata_extractor/'
'extractor.dart';
import 'package:barback/barback.dart';
@ -18,12 +18,11 @@ var formatter = new DartFormatter();
void allTests() {
var reader = new TestAssetReader();
var parser = new Parser(reader);
beforeEach(() => setLogger(new PrintLogger()));
Future<DirectiveMetadata> readMetadata(inputPath) async {
var ngDeps = await parser.parse(new AssetId('a', inputPath));
var ngDeps = await NgDeps.parse(reader, new AssetId('a', inputPath));
return ngDeps.registeredTypes.first.directiveMetadata;
}
@ -36,7 +35,7 @@ void allTests() {
});
it('should parse compile children values', () async {
var ngDeps = await parser.parse(new AssetId('a',
var ngDeps = await NgDeps.parse(reader, new AssetId('a',
'directive_metadata_extractor/'
'directive_metadata_files/compile_children.ng_deps.dart'));
var it = ngDeps.registeredTypes.iterator;
@ -96,7 +95,7 @@ void allTests() {
it('should fail when a class is annotated with multiple Directives.',
() async {
var ngDeps = await parser.parse(new AssetId('a',
var ngDeps = await NgDeps.parse(reader, new AssetId('a',
'directive_metadata_extractor/'
'directive_metadata_files/too_many_directives.ng_deps.dart'));
expect(() => ngDeps.registeredTypes.first.directiveMetadata).toThrowWith(