From a2e7ae568ead21e05d7c2cbdbf79a6980971653b Mon Sep 17 00:00:00 2001 From: vsavkin Date: Tue, 6 Oct 2015 17:04:07 -0700 Subject: [PATCH] feat(transformers): update transformers to handle components without @View --- .../common/directive_metadata_reader.dart | 47 ++++++++++++------- .../compile_data_creator.dart | 21 ++++++--- .../simple_annotation_files/bar.dart | 3 +- .../expected/bar.ng_deps.dart | 3 +- 4 files changed, 47 insertions(+), 27 deletions(-) diff --git a/modules_dart/transform/lib/src/transform/common/directive_metadata_reader.dart b/modules_dart/transform/lib/src/transform/common/directive_metadata_reader.dart index 9ef3440769..4126061ab6 100644 --- a/modules_dart/transform/lib/src/transform/common/directive_metadata_reader.dart +++ b/modules_dart/transform/lib/src/transform/common/directive_metadata_reader.dart @@ -141,7 +141,8 @@ class _DirectiveMetadataVisitor extends Object List _outputs; Map _host; List _lifecycleHooks; - CompileTemplateMetadata _template; + CompileTemplateMetadata _cmpTemplate; + CompileTemplateMetadata _viewTemplate; void reset(AssetId assetId) { _lifecycleVisitor.reset(assetId); @@ -157,23 +158,29 @@ class _DirectiveMetadataVisitor extends Object _outputs = []; _host = {}; _lifecycleHooks = null; - _template = null; + _cmpTemplate = null; + _viewTemplate = null; } bool get hasMetadata => _hasMetadata; - CompileDirectiveMetadata createMetadata() => CompileDirectiveMetadata.create( - type: _type, - isComponent: _isComponent, - dynamicLoadable: true, // NOTE(kegluneq): For future optimization. - selector: _selector, - exportAs: _exportAs, - changeDetection: _changeDetection, - inputs: _inputs, - outputs: _outputs, - host: _host, - lifecycleHooks: _lifecycleHooks, - template: _template); + get _template => _viewTemplate != null ? _viewTemplate : _cmpTemplate; + + CompileDirectiveMetadata createMetadata() { + return CompileDirectiveMetadata.create( + type: _type, + isComponent: _isComponent, + dynamicLoadable: true, + // NOTE(kegluneq): For future optimization. + selector: _selector, + exportAs: _exportAs, + changeDetection: _changeDetection, + inputs: _inputs, + outputs: _outputs, + host: _host, + lifecycleHooks: _lifecycleHooks, + template: _template); + } @override Object visitAnnotation(Annotation node) { @@ -183,20 +190,26 @@ class _DirectiveMetadataVisitor extends Object if (_hasMetadata) { throw new FormatException( 'Only one Directive is allowed per class. ' - 'Found unexpected "$node".', + 'Found unexpected "$node".', '$node' /* source */); } _isComponent = isComponent; _hasMetadata = true; + if (isComponent) { + var t = new _CompileTemplateMetadataVisitor().visitAnnotation(node); + if (t.template != null || t.templateUrl != null) { + _cmpTemplate = t; + } + } super.visitAnnotation(node); } else if (_annotationMatcher.isView(node, _assetId)) { - if (_template != null) { + if (_viewTemplate!= null) { throw new FormatException( 'Only one View is allowed per class. ' 'Found unexpected "$node".', '$node' /* source */); } - _template = new _CompileTemplateMetadataVisitor().visitAnnotation(node); + _viewTemplate = new _CompileTemplateMetadataVisitor().visitAnnotation(node); } // Annotation we do not recognize - no need to visit. diff --git a/modules_dart/transform/lib/src/transform/template_compiler/compile_data_creator.dart b/modules_dart/transform/lib/src/transform/template_compiler/compile_data_creator.dart index 4668d34470..2f091b346e 100644 --- a/modules_dart/transform/lib/src/transform/template_compiler/compile_data_creator.dart +++ b/modules_dart/transform/lib/src/transform/template_compiler/compile_data_creator.dart @@ -19,8 +19,8 @@ import 'package:code_transformers/assets.dart'; /// /// The returned value wraps the [NgDeps] at `entryPoint` as well as these /// created objects. -Future createCompileData( - AssetReader reader, AssetId entryPoint) async { +Future createCompileData(AssetReader reader, + AssetId entryPoint) async { return new _CompileDataCreator(reader, entryPoint).createCompileData(); } @@ -43,6 +43,14 @@ bool _isViewAnnotation(InstanceCreationExpression node) { 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 /// `entryPoint`. class _CompileDataCreator { @@ -68,6 +76,7 @@ class _CompileDataCreator { // Note: we use '' because the current file maps to the default prefix. var ngMeta = visitor._metadataMap['']; var typeName = '${rType.typeName}'; + if (ngMeta.types.containsKey(typeName)) { visitor.compileData.component = ngMeta.types[typeName]; } else { @@ -161,7 +170,6 @@ class _CompileDataCreator { return retVal; } } - /// Visitor responsible for processing the `annotations` property of a /// [RegisterType] object, extracting the `directives` dependencies, and adding /// their associated [CompileDirectiveMetadata] to the `directives` of a @@ -187,7 +195,8 @@ class _DirectiveDependenciesVisitor extends Object /// reflector. @override Object visitInstanceCreationExpression(InstanceCreationExpression node) { - if (_isViewAnnotation(node)) { +// if (_isViewAnnotation(node)) { + if (_isViewAnnotation(node) || _isComponentAnnotation(node)) { compileData = new NormalizedComponentWithViewDirectives( null, []); node.visitChildren(this); @@ -202,7 +211,7 @@ class _DirectiveDependenciesVisitor extends Object if (node.name is! Label || node.name.label is! SimpleIdentifier) { logger.error( 'Angular 2 currently only supports simple identifiers in directives.' - ' Source: ${node}'); + ' Source: ${node}'); return null; } if ('${node.name.label}' == 'directives') { @@ -234,7 +243,7 @@ class _DirectiveDependenciesVisitor extends Object } else { logger.error( 'Angular 2 currently only supports simple and prefixed identifiers ' - 'as values for "directives". Source: $node'); + 'as values for "directives". Source: $node'); return; } if (ngMeta.types.containsKey(name)) { diff --git a/modules_dart/transform/test/transform/integration/simple_annotation_files/bar.dart b/modules_dart/transform/test/transform/integration/simple_annotation_files/bar.dart index 1883d5c3d3..afd8ab284e 100644 --- a/modules_dart/transform/test/transform/integration/simple_annotation_files/bar.dart +++ b/modules_dart/transform/test/transform/integration/simple_annotation_files/bar.dart @@ -2,8 +2,7 @@ library bar; import 'package:angular2/src/core/metadata.dart'; -@Component(selector: '[soup]') -@View(template: '') +@Component(selector: '[soup]', template: 'aa') class MyComponent { MyComponent(); } diff --git a/modules_dart/transform/test/transform/integration/simple_annotation_files/expected/bar.ng_deps.dart b/modules_dart/transform/test/transform/integration/simple_annotation_files/expected/bar.ng_deps.dart index b207b8263a..2b62eb98df 100644 --- a/modules_dart/transform/test/transform/integration/simple_annotation_files/expected/bar.ng_deps.dart +++ b/modules_dart/transform/test/transform/integration/simple_annotation_files/expected/bar.ng_deps.dart @@ -16,8 +16,7 @@ void initReflector() { ..registerType( MyComponent, new _ngRef.ReflectionInfo(const [ - const Component(selector: '[soup]'), - const View(template: ''), + const Component(selector: '[soup]', template: 'aa'), _templates.HostMyComponentTemplate ], const [], () => new MyComponent())); i0.initReflector();