feat(transformers): update transformers to handle components without @View

This commit is contained in:
vsavkin 2015-10-06 17:04:07 -07:00 committed by Victor Savkin
parent bd31b01690
commit a2e7ae568e
4 changed files with 47 additions and 27 deletions

View File

@ -141,7 +141,8 @@ class _DirectiveMetadataVisitor extends Object
List<String> _outputs; List<String> _outputs;
Map<String, String> _host; Map<String, String> _host;
List<LifecycleHooks> _lifecycleHooks; List<LifecycleHooks> _lifecycleHooks;
CompileTemplateMetadata _template; CompileTemplateMetadata _cmpTemplate;
CompileTemplateMetadata _viewTemplate;
void reset(AssetId assetId) { void reset(AssetId assetId) {
_lifecycleVisitor.reset(assetId); _lifecycleVisitor.reset(assetId);
@ -157,15 +158,20 @@ class _DirectiveMetadataVisitor extends Object
_outputs = <String>[]; _outputs = <String>[];
_host = <String, String>{}; _host = <String, String>{};
_lifecycleHooks = null; _lifecycleHooks = null;
_template = null; _cmpTemplate = null;
_viewTemplate = null;
} }
bool get hasMetadata => _hasMetadata; bool get hasMetadata => _hasMetadata;
CompileDirectiveMetadata createMetadata() => CompileDirectiveMetadata.create( get _template => _viewTemplate != null ? _viewTemplate : _cmpTemplate;
CompileDirectiveMetadata createMetadata() {
return CompileDirectiveMetadata.create(
type: _type, type: _type,
isComponent: _isComponent, isComponent: _isComponent,
dynamicLoadable: true, // NOTE(kegluneq): For future optimization. dynamicLoadable: true,
// NOTE(kegluneq): For future optimization.
selector: _selector, selector: _selector,
exportAs: _exportAs, exportAs: _exportAs,
changeDetection: _changeDetection, changeDetection: _changeDetection,
@ -174,6 +180,7 @@ class _DirectiveMetadataVisitor extends Object
host: _host, host: _host,
lifecycleHooks: _lifecycleHooks, lifecycleHooks: _lifecycleHooks,
template: _template); template: _template);
}
@override @override
Object visitAnnotation(Annotation node) { Object visitAnnotation(Annotation node) {
@ -188,15 +195,21 @@ class _DirectiveMetadataVisitor extends Object
} }
_isComponent = isComponent; _isComponent = isComponent;
_hasMetadata = true; _hasMetadata = true;
if (isComponent) {
var t = new _CompileTemplateMetadataVisitor().visitAnnotation(node);
if (t.template != null || t.templateUrl != null) {
_cmpTemplate = t;
}
}
super.visitAnnotation(node); super.visitAnnotation(node);
} else if (_annotationMatcher.isView(node, _assetId)) { } else if (_annotationMatcher.isView(node, _assetId)) {
if (_template != null) { if (_viewTemplate!= null) {
throw new FormatException( throw new FormatException(
'Only one View is allowed per class. ' 'Only one View is allowed per class. '
'Found unexpected "$node".', 'Found unexpected "$node".',
'$node' /* source */); '$node' /* source */);
} }
_template = new _CompileTemplateMetadataVisitor().visitAnnotation(node); _viewTemplate = new _CompileTemplateMetadataVisitor().visitAnnotation(node);
} }
// Annotation we do not recognize - no need to visit. // Annotation we do not recognize - no need to visit.

View File

@ -19,8 +19,8 @@ import 'package:code_transformers/assets.dart';
/// ///
/// The returned value wraps the [NgDeps] at `entryPoint` as well as these /// The returned value wraps the [NgDeps] at `entryPoint` as well as these
/// created objects. /// created objects.
Future<CompileDataResults> createCompileData( Future<CompileDataResults> createCompileData(AssetReader reader,
AssetReader reader, AssetId entryPoint) async { AssetId entryPoint) async {
return new _CompileDataCreator(reader, entryPoint).createCompileData(); return new _CompileDataCreator(reader, entryPoint).createCompileData();
} }
@ -43,6 +43,14 @@ bool _isViewAnnotation(InstanceCreationExpression node) {
return constructorName.name == 'View'; return constructorName.name == 'View';
} }
bool _isComponentAnnotation(InstanceCreationExpression node) {
var constructorName = node.constructorName.type.name;
if (constructorName is PrefixedIdentifier) {
constructorName = constructorName.identifier;
}
return constructorName.name == 'Component';
}
/// Creates [ViewDefinition] objects for all `View` `Directive`s defined in /// Creates [ViewDefinition] objects for all `View` `Directive`s defined in
/// `entryPoint`. /// `entryPoint`.
class _CompileDataCreator { class _CompileDataCreator {
@ -68,6 +76,7 @@ class _CompileDataCreator {
// Note: we use '' because the current file maps to the default prefix. // Note: we use '' because the current file maps to the default prefix.
var ngMeta = visitor._metadataMap['']; var ngMeta = visitor._metadataMap[''];
var typeName = '${rType.typeName}'; var typeName = '${rType.typeName}';
if (ngMeta.types.containsKey(typeName)) { if (ngMeta.types.containsKey(typeName)) {
visitor.compileData.component = ngMeta.types[typeName]; visitor.compileData.component = ngMeta.types[typeName];
} else { } else {
@ -161,7 +170,6 @@ class _CompileDataCreator {
return retVal; return retVal;
} }
} }
/// Visitor responsible for processing the `annotations` property of a /// Visitor responsible for processing the `annotations` property of a
/// [RegisterType] object, extracting the `directives` dependencies, and adding /// [RegisterType] object, extracting the `directives` dependencies, and adding
/// their associated [CompileDirectiveMetadata] to the `directives` of a /// their associated [CompileDirectiveMetadata] to the `directives` of a
@ -187,7 +195,8 @@ class _DirectiveDependenciesVisitor extends Object
/// reflector. /// reflector.
@override @override
Object visitInstanceCreationExpression(InstanceCreationExpression node) { Object visitInstanceCreationExpression(InstanceCreationExpression node) {
if (_isViewAnnotation(node)) { // if (_isViewAnnotation(node)) {
if (_isViewAnnotation(node) || _isComponentAnnotation(node)) {
compileData = new NormalizedComponentWithViewDirectives( compileData = new NormalizedComponentWithViewDirectives(
null, <CompileDirectiveMetadata>[]); null, <CompileDirectiveMetadata>[]);
node.visitChildren(this); node.visitChildren(this);

View File

@ -2,8 +2,7 @@ library bar;
import 'package:angular2/src/core/metadata.dart'; import 'package:angular2/src/core/metadata.dart';
@Component(selector: '[soup]') @Component(selector: '[soup]', template: 'aa')
@View(template: '')
class MyComponent { class MyComponent {
MyComponent(); MyComponent();
} }

View File

@ -16,8 +16,7 @@ void initReflector() {
..registerType( ..registerType(
MyComponent, MyComponent,
new _ngRef.ReflectionInfo(const [ new _ngRef.ReflectionInfo(const [
const Component(selector: '[soup]'), const Component(selector: '[soup]', template: 'aa'),
const View(template: ''),
_templates.HostMyComponentTemplate _templates.HostMyComponentTemplate
], const [], () => new MyComponent())); ], const [], () => new MyComponent()));
i0.initReflector(); i0.initReflector();