|
| 1 | +library angular2.src.transform.common.parser; |
| 2 | + |
| 3 | +import 'dart:async'; |
| 4 | + |
| 5 | +import 'package:analyzer/analyzer.dart'; |
| 6 | +import 'package:angular2/src/transform/common/asset_reader.dart'; |
| 7 | +import 'package:angular2/src/transform/common/logging.dart'; |
| 8 | +import 'package:angular2/src/transform/common/names.dart'; |
| 9 | +import 'package:barback/barback.dart'; |
| 10 | +import 'package:code_transformers/assets.dart'; |
| 11 | + |
| 12 | +import 'registered_type.dart'; |
| 13 | + |
| 14 | +export 'registered_type.dart'; |
| 15 | + |
| 16 | +class Parser { |
| 17 | + final AssetReader _reader; |
| 18 | + final _ParseNgDepsVisitor _visitor = new _ParseNgDepsVisitor(); |
| 19 | + |
| 20 | + Parser(AssetReader this._reader); |
| 21 | + |
| 22 | + Future<List<NgDeps>> parseRecursive(AssetId id) async { |
| 23 | + return _recurse(id); |
| 24 | + } |
| 25 | + |
| 26 | + Future<NgDeps> parse(AssetId id) async { |
| 27 | + if (!(await _reader.hasInput(id))) return null; |
| 28 | + var ngDeps = new NgDeps(await _reader.readAsString(id)); |
| 29 | + _visitor.ngDeps = ngDeps; |
| 30 | + parseCompilationUnit(ngDeps.code, name: id.path).accept(_visitor); |
| 31 | + return ngDeps; |
| 32 | + } |
| 33 | + |
| 34 | + /// Parses the `.ngDeps.dart` file represented by [id] into an [NgDeps] |
| 35 | + /// object. All `.ngDeps.dart` files imported by [id] are then parsed. The |
| 36 | + /// results are added to [allDeps]. |
| 37 | + Future<List<NgDeps>> _recurse(AssetId id, |
| 38 | + [List<NgDeps> allDeps, Set<AssetId> seen]) async { |
| 39 | + if (seen == null) seen = new Set<AssetId>(); |
| 40 | + if (seen.contains(id)) return; |
| 41 | + seen.add(id); |
| 42 | + |
| 43 | + if (allDeps == null) allDeps = []; |
| 44 | + var ngDeps = await parse(id); |
| 45 | + allDeps.add(ngDeps); |
| 46 | + |
| 47 | + var toWait = []; |
| 48 | + ngDeps.imports.forEach((ImportDirective node) { |
| 49 | + var uri = stringLiteralToString(node.uri); |
| 50 | + if (uri.endsWith(DEPS_EXTENSION)) { |
| 51 | + var importId = uriToAssetId(id, uri, logger, null); |
| 52 | + toWait.add(_recurse(importId, allDeps, seen)); |
| 53 | + } |
| 54 | + }); |
| 55 | + return Future.wait(toWait).then((_) => allDeps); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +class NgDeps { |
| 60 | + final String code; |
| 61 | + final List<ImportDirective> imports = []; |
| 62 | + final List<ExportDirective> exports = []; |
| 63 | + final List<RegisteredType> registeredTypes = []; |
| 64 | + FunctionDeclaration setupMethod; |
| 65 | + |
| 66 | + NgDeps(this.code); |
| 67 | +} |
| 68 | + |
| 69 | +class _ParseNgDepsVisitor extends Object with RecursiveAstVisitor<Object> { |
| 70 | + NgDeps ngDeps = null; |
| 71 | + _RegisteredTypeBuilder current = null; |
| 72 | + |
| 73 | + @override |
| 74 | + Object visitImportDirective(ImportDirective node) { |
| 75 | + ngDeps.imports.add(node); |
| 76 | + return super.visitImportDirective(node); |
| 77 | + } |
| 78 | + |
| 79 | + @override |
| 80 | + Object visitExportDirective(ExportDirective node) { |
| 81 | + ngDeps.exports.add(node); |
| 82 | + return super.visitExportDirective(node); |
| 83 | + } |
| 84 | + |
| 85 | + @override |
| 86 | + Object visitFunctionDeclaration(FunctionDeclaration node) { |
| 87 | + if ('${node.name}' == SETUP_METHOD_NAME) { |
| 88 | + ngDeps.setupMethod = node; |
| 89 | + } |
| 90 | + return super.visitFunctionDeclaration(node); |
| 91 | + } |
| 92 | + |
| 93 | + @override |
| 94 | + Object visitMethodInvocation(MethodInvocation node) { |
| 95 | + var isRegisterType = '${node.methodName}' == REGISTER_TYPE_METHOD_NAME; |
| 96 | + |
| 97 | + if (isRegisterType) { |
| 98 | + ngDeps.registeredTypes.add(new RegisteredType.fromMethodInvocation(node)); |
| 99 | + } |
| 100 | + |
| 101 | + return super.visitMethodInvocation(node); |
| 102 | + } |
| 103 | +} |
0 commit comments