feat(transformers): collect information about di dependencies and providers

This commit is contained in:
vsavkin
2016-02-04 11:56:39 -08:00
committed by Victor Savkin
parent 16b521794c
commit 86c40f8474
34 changed files with 1002 additions and 179 deletions

View File

@ -67,6 +67,7 @@ class ReflectionInfoVisitor extends RecursiveAstVisitor<ReflectionInfoModel> {
@override
ReflectionInfoModel visitClassDeclaration(ClassDeclaration node) {
if (node.isAbstract) return null;
if (!node.metadata
.any((a) => _annotationMatcher.hasMatch(a.name, assetId))) {
return null;

View File

@ -33,9 +33,9 @@ class NgMeta {
static const _TYPE_VALUE = 'type';
static const _VALUE_KEY = 'value';
/// Metadata for each type annotated as a directive/pipe.
/// Type: [CompileDirectiveMetadata]/[CompilePipeMetadata]
final Map<String, dynamic> types;
/// Metadata for each identifier
/// Type: [CompileDirectiveMetadata]|[CompilePipeMetadata]|[CompileTypeMetadata]|[CompileIdentifierMetadata]
final Map<String, dynamic> identifiers;
/// List of other types and names associated with a given name.
final Map<String, List<String>> aliases;
@ -43,12 +43,11 @@ class NgMeta {
// The NgDeps generated from
final NgDepsModel ngDeps;
NgMeta(
{Map<String, dynamic> types,
Map<String, List<String>> aliases,
NgMeta({Map<String, List<String>> aliases,
Map<String, dynamic> identifiers,
this.ngDeps: null})
: this.types = types != null ? types : {},
this.aliases = aliases != null ? aliases : {};
:this.aliases = aliases != null ? aliases : {},
this.identifiers = identifiers != null ? identifiers : {};
NgMeta.empty() : this();
@ -69,13 +68,13 @@ class NgMeta {
return false;
}
bool get isEmpty => types.isEmpty && aliases.isEmpty && isNgDepsEmpty;
bool get isEmpty => identifiers.isEmpty && aliases.isEmpty && isNgDepsEmpty;
/// Parse from the serialized form produced by [toJson].
factory NgMeta.fromJson(Map json) {
var ngDeps = null;
final types = {};
final aliases = {};
final identifiers = {};
for (var key in json.keys) {
if (key == _NG_DEPS_KEY) {
var ngDepsJsonMap = json[key];
@ -85,7 +84,9 @@ class NgMeta {
'Unexpected value $ngDepsJsonMap for key "$key" in NgMeta.');
continue;
}
ngDeps = new NgDepsModel()..mergeFromJsonMap(ngDepsJsonMap);
ngDeps = new NgDepsModel()
..mergeFromJsonMap(ngDepsJsonMap);
} else {
var entry = json[key];
if (entry is! Map) {
@ -93,13 +94,13 @@ class NgMeta {
continue;
}
if (entry[_KIND_KEY] == _TYPE_VALUE) {
types[key] = CompileMetadataWithType.fromJson(entry[_VALUE_KEY]);
identifiers[key] = CompileMetadataWithIdentifier.fromJson(entry[_VALUE_KEY]);
} else if (entry[_KIND_KEY] == _ALIAS_VALUE) {
aliases[key] = entry[_VALUE_KEY];
}
}
}
return new NgMeta(types: types, aliases: aliases, ngDeps: ngDeps);
return new NgMeta(identifiers: identifiers, aliases: aliases, ngDeps: ngDeps);
}
/// Serialized representation of this instance.
@ -107,7 +108,7 @@ class NgMeta {
var result = {};
result[_NG_DEPS_KEY] = isNgDepsEmpty ? null : ngDeps.writeToJsonMap();
types.forEach((k, v) {
identifiers.forEach((k, v) {
result[k] = {_KIND_KEY: _TYPE_VALUE, _VALUE_KEY: v.toJson()};
});
@ -120,8 +121,8 @@ class NgMeta {
/// Merge into this instance all information from [other].
/// This does not include `ngDeps`.
void addAll(NgMeta other) {
types.addAll(other.types);
aliases.addAll(other.aliases);
identifiers.addAll(other.identifiers);
}
/// Returns the metadata for every type associated with the given [alias].
@ -133,8 +134,8 @@ class NgMeta {
log.warning('Circular alias dependency for "$name".');
return;
}
if (types.containsKey(name)) {
result.add(types[name]);
if (identifiers.containsKey(name)) {
result.add(identifiers[name]);
} else if (aliases.containsKey(name)) {
aliases[name].forEach(helper);
} else {

View File

@ -21,22 +21,24 @@ import 'url_resolver.dart';
class TypeMetadataReader {
final _DirectiveMetadataVisitor _directiveVisitor;
final _PipeMetadataVisitor _pipeVisitor;
final _CompileTypeMetadataVisitor _typeVisitor;
final TemplateCompiler _templateCompiler;
TypeMetadataReader._(
this._directiveVisitor, this._pipeVisitor, this._templateCompiler);
this._directiveVisitor, this._pipeVisitor, this._templateCompiler, this._typeVisitor);
/// Accepts an [AnnotationMatcher] which tests that an [Annotation]
/// is a [Directive], [Component], or [View].
factory TypeMetadataReader(AnnotationMatcher annotationMatcher,
InterfaceMatcher interfaceMatcher, TemplateCompiler templateCompiler) {
var lifecycleVisitor = new _LifecycleHookVisitor(interfaceMatcher);
var typeVisitor = new _CompileTypeMetadataVisitor(annotationMatcher);
var directiveVisitor =
new _DirectiveMetadataVisitor(annotationMatcher, lifecycleVisitor);
new _DirectiveMetadataVisitor(annotationMatcher, lifecycleVisitor, typeVisitor);
var pipeVisitor = new _PipeMetadataVisitor(annotationMatcher);
return new TypeMetadataReader._(
directiveVisitor, pipeVisitor, templateCompiler);
directiveVisitor, pipeVisitor, templateCompiler, typeVisitor);
}
/// Reads *un-normalized* [CompileDirectiveMetadata]/[CompilePipeMetadata] from the
@ -52,13 +54,19 @@ class TypeMetadataReader {
Future<dynamic> readTypeMetadata(ClassDeclaration node, AssetId assetId) {
_directiveVisitor.reset(assetId);
_pipeVisitor.reset(assetId);
_typeVisitor.reset(assetId);
node.accept(_directiveVisitor);
node.accept(_pipeVisitor);
node.accept(_typeVisitor);
if (_directiveVisitor.hasMetadata) {
final metadata = _directiveVisitor.createMetadata();
return _templateCompiler.normalizeDirectiveMetadata(metadata);
} else if (_pipeVisitor.hasMetadata) {
return new Future.value(_pipeVisitor.createMetadata());
} else if (_typeVisitor.isInjectable) {
return new Future.value(_typeVisitor.type);
} else {
return new Future.value(null);
}
@ -124,6 +132,67 @@ bool _expressionToBool(Expression node, String nodeDescription) {
return value;
}
class _CompileTypeMetadataVisitor extends Object
with RecursiveAstVisitor<CompileTypeMetadata> {
bool _isInjectable = false;
CompileTypeMetadata _type;
AssetId _assetId;
final AnnotationMatcher _annotationMatcher;
_CompileTypeMetadataVisitor(this._annotationMatcher);
bool get isInjectable => _isInjectable;
CompileTypeMetadata get type => _type;
void reset(AssetId assetId) {
this._assetId = assetId;
this._isInjectable = false;
this._type = null;
}
@override
Object visitAnnotation(Annotation node) {
final isComponent = _annotationMatcher.isComponent(node, _assetId);
final isDirective = _annotationMatcher.isDirective(node, _assetId);
final isInjectable = _annotationMatcher.isInjectable(node, _assetId);
_isInjectable = _isInjectable || isComponent || isDirective || isInjectable;
return null;
}
@override
Object visitClassDeclaration(ClassDeclaration node) {
node.metadata.accept(this);
if (this._isInjectable) {
_type = new CompileTypeMetadata(
moduleUrl: toAssetUri(_assetId),
name: node.name.toString(),
diDeps: _getCompileDiDependencyMetadata(node),
runtime: null // Intentionally `null`, cannot be provided here.
);
}
return null;
}
List<CompileDiDependencyMetadata> _getCompileDiDependencyMetadata(ClassDeclaration node) {
final constructor = node.getConstructor(null);
if (constructor == null) return [];
return constructor.parameters.parameters.map((p) {
final typeToken = p is SimpleFormalParameter && p.type != null ? _readIdentifier(p.type.name) : null;
final injectTokens = p.metadata.where((m) => m.name.toString() == "Inject").map((m) => _readIdentifier(m.arguments.arguments[0]));
final token = injectTokens.isNotEmpty ? injectTokens.first : typeToken;
return new CompileDiDependencyMetadata(token: token);
}).toList();
}
}
/// Visitor responsible for processing a [Directive] annotated
/// [ClassDeclaration] and creating a [CompileDirectiveMetadata] object.
class _DirectiveMetadataVisitor extends Object
@ -134,10 +203,12 @@ class _DirectiveMetadataVisitor extends Object
final _LifecycleHookVisitor _lifecycleVisitor;
final _CompileTypeMetadataVisitor _typeVisitor;
/// The [AssetId] we are currently processing.
AssetId _assetId;
_DirectiveMetadataVisitor(this._annotationMatcher, this._lifecycleVisitor) {
_DirectiveMetadataVisitor(this._annotationMatcher, this._lifecycleVisitor, this._typeVisitor) {
reset(null);
}
@ -154,12 +225,14 @@ class _DirectiveMetadataVisitor extends Object
List<String> _inputs;
List<String> _outputs;
Map<String, String> _host;
List<CompileProviderMetadata> _providers;
List<LifecycleHooks> _lifecycleHooks;
CompileTemplateMetadata _cmpTemplate;
CompileTemplateMetadata _viewTemplate;
void reset(AssetId assetId) {
_lifecycleVisitor.reset(assetId);
_typeVisitor.reset(assetId);
_assetId = assetId;
_type = null;
@ -171,6 +244,7 @@ class _DirectiveMetadataVisitor extends Object
_inputs = <String>[];
_outputs = <String>[];
_host = <String, String>{};
_providers = <CompileProviderMetadata>[];
_lifecycleHooks = null;
_cmpTemplate = null;
_viewTemplate = null;
@ -192,6 +266,7 @@ class _DirectiveMetadataVisitor extends Object
inputs: _inputs,
outputs: _outputs,
host: _host,
providers: _providers,
lifecycleHooks: _lifecycleHooks,
template: _template);
}
@ -316,6 +391,34 @@ class _DirectiveMetadataVisitor extends Object
}
}
void _populateProviders(Expression providerValues) {
_checkMeta();
final providers = (providerValues as ListLiteral).elements.map((el) {
if (el is PrefixedIdentifier || el is SimpleIdentifier) {
return new CompileProviderMetadata(token: _readIdentifier(el));
} else if (el is InstanceCreationExpression && el.constructorName.toString() == "Provider") {
final token = el.argumentList.arguments.first;
var useClass;
el.argumentList.arguments.skip(1).forEach((arg) {
if (arg.name.toString() == "useClass:") {
final id = _readIdentifier(arg.expression);
useClass = new CompileTypeMetadata(prefix: id.prefix, name: id.name);
}
});
return new CompileProviderMetadata(token: _readIdentifier(token), useClass: useClass);
} else {
throw new ArgumentError(
'Incorrect value. Expected a Provider or a String, but got "${el}".');
}
});
_providers.addAll(providers);
}
//TODO Use AnnotationMatcher instead of string matching
bool _isAnnotation(Annotation node, String annotationName) {
var id = node.name;
@ -356,12 +459,10 @@ class _DirectiveMetadataVisitor extends Object
@override
Object visitClassDeclaration(ClassDeclaration node) {
node.metadata.accept(this);
node.accept(_typeVisitor);
_type = _typeVisitor.type;
if (this._hasMetadata) {
_type = new CompileTypeMetadata(
moduleUrl: toAssetUri(_assetId),
name: node.name.toString(),
runtime: null // Intentionally `null`, cannot be provided here.
);
_lifecycleHooks = node.implementsClause != null
? node.implementsClause.accept(_lifecycleVisitor)
: const [];
@ -405,6 +506,9 @@ class _DirectiveMetadataVisitor extends Object
case 'events':
_populateEvents(node.expression);
break;
case 'providers':
_populateProviders(node.expression);
break;
}
return null;
}
@ -687,3 +791,19 @@ class _PipeMetadataVisitor extends Object with RecursiveAstVisitor<Object> {
_pure = _expressionToBool(pureValue, 'Pipe#pure');
}
}
dynamic _readIdentifier(dynamic el) {
if (el is PrefixedIdentifier) {
return new CompileIdentifierMetadata(name: '${el.identifier}', prefix: '${el.prefix}');
} else if (el is SimpleIdentifier) {
return new CompileIdentifierMetadata(name: '$el');
} else if (el is SimpleStringLiteral){
return el.value;
} else {
throw new ArgumentError('Incorrect identifier "${el}".');
}
}

View File

@ -5,6 +5,7 @@ import 'dart:async';
import 'package:analyzer/analyzer.dart';
import 'package:barback/barback.dart' show AssetId;
import 'package:angular2/src/compiler/directive_metadata.dart' show CompileIdentifierMetadata;
import 'package:angular2/src/compiler/template_compiler.dart';
import 'package:angular2/src/transform/common/annotation_matcher.dart';
import 'package:angular2/src/transform/common/asset_reader.dart';
@ -14,6 +15,7 @@ import 'package:angular2/src/transform/common/interface_matcher.dart';
import 'package:angular2/src/transform/common/logging.dart';
import 'package:angular2/src/transform/common/ng_compiler.dart';
import 'package:angular2/src/transform/common/ng_meta.dart';
import 'package:angular2/src/transform/common/url_resolver.dart';
import 'package:angular2/src/transform/common/zone.dart' as zone;
import 'inliner.dart';
@ -89,14 +91,15 @@ class _NgMetaVisitor extends Object with SimpleAstVisitor<Object> {
@override
Object visitClassDeclaration(ClassDeclaration node) {
_normalizations.add(
_reader.readTypeMetadata(node, assetId).then((compileMetadataWithType) {
if (compileMetadataWithType != null) {
ngMeta.types[compileMetadataWithType.type.name] =
compileMetadataWithType;
_reader.readTypeMetadata(node, assetId).then((compileMetadataWithIdentifier) {
if (compileMetadataWithIdentifier!= null) {
ngMeta.identifiers[compileMetadataWithIdentifier.identifier.name] =
compileMetadataWithIdentifier;
}
}).catchError((err) {
log.error('ERROR: $err', asset: assetId);
}));
return null;
}
@ -108,6 +111,11 @@ class _NgMetaVisitor extends Object with SimpleAstVisitor<Object> {
// doesn't support decorators on variable declarations (see
// angular/angular#1747 and angular/ts2dart#249 for context).
outer: for (var variable in node.variables.variables) {
if (variable.isConst) {
ngMeta.identifiers[variable.name.name] =
new CompileIdentifierMetadata(name: variable.name.name, moduleUrl: toAssetUri(assetId));
}
var initializer = variable.initializer;
if (initializer != null && initializer is ListLiteral) {
var otherNames = [];

View File

@ -74,8 +74,8 @@ class _CompileDataCreator {
var hasTemplate = ngDeps != null &&
ngDeps.reflectables != null &&
ngDeps.reflectables.any((reflectable) {
if (ngMeta.types.containsKey(reflectable.name)) {
final metadata = ngMeta.types[reflectable.name];
if (ngMeta.identifiers.containsKey(reflectable.name)) {
final metadata = ngMeta.identifiers[reflectable.name];
return metadata is CompileDirectiveMetadata &&
metadata.template != null;
}
@ -91,8 +91,8 @@ class _CompileDataCreator {
final platformPipes = await _readPlatformTypes(this.platformPipes, 'pipes');
for (var reflectable in ngDeps.reflectables) {
if (ngMeta.types.containsKey(reflectable.name)) {
final compileDirectiveMetadata = ngMeta.types[reflectable.name];
if (ngMeta.identifiers.containsKey(reflectable.name)) {
final compileDirectiveMetadata = ngMeta.identifiers[reflectable.name];
if (compileDirectiveMetadata is CompileDirectiveMetadata &&
compileDirectiveMetadata.template != null) {
final compileDatum = new NormalizedComponentWithViewDirectives(
@ -106,6 +106,9 @@ class _CompileDataCreator {
compileDatum.pipes
.addAll(_resolveTypeMetadata(ngMetaMap, reflectable.pipes));
compileData[reflectable] = compileDatum;
_resolveDiDependencyMetadata(ngMetaMap, compileDirectiveMetadata.type, compileDirectiveMetadata.type.diDeps);
_resolveProviderMetadata(ngMetaMap, compileDirectiveMetadata);
}
}
}
@ -117,16 +120,16 @@ class _CompileDataCreator {
var resolvedMetadata = [];
for (var dep in prefixedTypes) {
if (!ngMetaMap.containsKey(dep.prefix)) {
log.warning(
log.error(
'Missing prefix "${dep.prefix}" '
'needed by "${dep}" from metadata map',
'needed by "${dep}" from metadata map,',
asset: entryPoint);
continue;
return null;
}
final depNgMeta = ngMetaMap[dep.prefix];
if (depNgMeta.types.containsKey(dep.name)) {
resolvedMetadata.add(depNgMeta.types[dep.name]);
if (depNgMeta.identifiers.containsKey(dep.name)) {
resolvedMetadata.add(depNgMeta.identifiers[dep.name]);
} else if (depNgMeta.aliases.containsKey(dep.name)) {
resolvedMetadata.addAll(depNgMeta.flatten(dep.name));
} else {
@ -141,6 +144,86 @@ class _CompileDataCreator {
return resolvedMetadata;
}
void _resolveProviderMetadata(Map<String, NgMeta> ngMetaMap, CompileDirectiveMetadata dirMeta) {
final neededBy = dirMeta.type;
if (dirMeta.providers == null) return;
final resolvedProviders = [];
for (var provider in dirMeta.providers) {
final alias = _resolveAlias(ngMetaMap, neededBy, provider.token);
if (alias != null) {
resolvedProviders.addAll(alias.map((a) => new CompileProviderMetadata(token:a)));
} else {
provider.token = _resolveIdentifier(ngMetaMap, neededBy, provider.token);
if (provider.useClass != null) {
provider.useClass = _resolveIdentifier(ngMetaMap, neededBy, provider.useClass);
}
resolvedProviders.add(provider);
}
}
dirMeta.providers = resolvedProviders;
}
void _resolveDiDependencyMetadata(
Map<String, NgMeta> ngMetaMap,CompileTypeMetadata neededBy, List<CompileDiDependencyMetadata> deps) {
if (deps == null) return;
for (var dep in deps) {
dep.token = _resolveIdentifier(ngMetaMap, neededBy, dep.token);
}
}
dynamic _resolveAlias(Map<String, NgMeta> ngMetaMap, CompileTypeMetadata neededBy, dynamic id) {
if (id is String || id == null) return null;
final prefix = id.prefix == null ? "" : id.prefix;
if (!ngMetaMap.containsKey(prefix)) {
log.error(
'Missing prefix "${prefix}" '
'needed by "${neededBy.name}" from metadata map',
asset: entryPoint);
return null;
}
final depNgMeta = ngMetaMap[prefix];
if (depNgMeta.aliases.containsKey(id.name)) {
return depNgMeta.flatten(id.name);
} else {
return null;
}
}
dynamic _resolveIdentifier(Map<String, NgMeta> ngMetaMap, CompileTypeMetadata neededBy, dynamic id) {
if (id is String || id == null) return id;
final prefix = id.prefix == null ? "" : id.prefix;
if (!ngMetaMap.containsKey(prefix)) {
log.error(
'Missing prefix "${prefix}" '
'needed by "${neededBy.name}" from metadata map',
asset: entryPoint);
return null;
}
final depNgMeta = ngMetaMap[prefix];
if (depNgMeta.identifiers.containsKey(id.name)) {
return depNgMeta.identifiers[id.name];
} else if (_isPrimitive(id.name)) {
return id;
} else {
log.error(
'Missing identifier "${id.name}" '
'needed by "${neededBy.name}" from metadata map',
asset: entryPoint);
return null;
}
}
bool _isPrimitive(String typeName) =>
typeName == "String" || typeName == "Object" || typeName == "num" || typeName == "int" || typeName == "double" || typeName == "bool";
Future<List<dynamic>> _readPlatformTypes(
List<String> inputPlatformTypes, String configOption) async {
if (inputPlatformTypes == null) return const [];
@ -168,8 +251,8 @@ class _CompileDataCreator {
if (jsonString != null && jsonString.isNotEmpty) {
var newMetadata = new NgMeta.fromJson(JSON.decode(jsonString));
if (newMetadata.types.containsKey(token)) {
return [newMetadata.types[token]];
if (newMetadata.identifiers.containsKey(token)) {
return [newMetadata.identifiers[token]];
} else if (newMetadata.aliases.containsKey(token)) {
return newMetadata.flatten(token);
} else {
@ -194,7 +277,6 @@ class _CompileDataCreator {
return map;
}
final resolver = const TransformerUrlResolver();
ngMeta.ngDeps.imports
.where((model) => !isDartCoreUri(model.uri))
.forEach((model) {
@ -242,6 +324,7 @@ class _CompileDataCreator {
var ngMeta = retVal[prefix] = new NgMeta.empty();
for (var importAssetUri in prefixToImports[prefix]) {
var metaAssetId = fromUri(toMetaExtension(importAssetUri));
if (await reader.hasInput(metaAssetId)) {
try {
var jsonString = await reader.readAsString(metaAssetId);

View File

@ -42,16 +42,18 @@ Future<Outputs> processTemplates(AssetReader reader, AssetId assetId,
var viewDefResults = await createCompileData(
reader, assetId, platformDirectives, platformPipes);
if (viewDefResults == null) return null;
final compileTypeMetadatas = viewDefResults.ngMeta.types.values;
final compileTypeMetadatas = viewDefResults.ngMeta.identifiers.values;
if (compileTypeMetadatas.isNotEmpty) {
var processor = new reg.Processor();
compileTypeMetadatas.forEach(processor.process);
viewDefResults.ngMeta.ngDeps.getters
.addAll(processor.getterNames.map((e) => e.sanitizedName));
viewDefResults.ngMeta.ngDeps.setters
.addAll(processor.setterNames.map((e) => e.sanitizedName));
viewDefResults.ngMeta.ngDeps.methods
.addAll(processor.methodNames.map((e) => e.sanitizedName));
if (viewDefResults.ngMeta.ngDeps != null) {
viewDefResults.ngMeta.ngDeps.getters
.addAll(processor.getterNames.map((e) => e.sanitizedName));
viewDefResults.ngMeta.ngDeps.setters
.addAll(processor.setterNames.map((e) => e.sanitizedName));
viewDefResults.ngMeta.ngDeps.methods
.addAll(processor.methodNames.map((e) => e.sanitizedName));
}
}
var templateCompiler = zone.templateCompiler;
if (templateCompiler == null) {
@ -98,4 +100,4 @@ class Outputs {
final SourceModule templatesSource;
Outputs._(this.ngDeps, this.templatesSource);
}
}