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,23 +158,29 @@ 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;
type: _type,
isComponent: _isComponent, CompileDirectiveMetadata createMetadata() {
dynamicLoadable: true, // NOTE(kegluneq): For future optimization. return CompileDirectiveMetadata.create(
selector: _selector, type: _type,
exportAs: _exportAs, isComponent: _isComponent,
changeDetection: _changeDetection, dynamicLoadable: true,
inputs: _inputs, // NOTE(kegluneq): For future optimization.
outputs: _outputs, selector: _selector,
host: _host, exportAs: _exportAs,
lifecycleHooks: _lifecycleHooks, changeDetection: _changeDetection,
template: _template); inputs: _inputs,
outputs: _outputs,
host: _host,
lifecycleHooks: _lifecycleHooks,
template: _template);
}
@override @override
Object visitAnnotation(Annotation node) { Object visitAnnotation(Annotation node) {
@ -183,20 +190,26 @@ class _DirectiveMetadataVisitor extends Object
if (_hasMetadata) { if (_hasMetadata) {
throw new FormatException( throw new FormatException(
'Only one Directive is allowed per class. ' 'Only one Directive is allowed per class. '
'Found unexpected "$node".', 'Found unexpected "$node".',
'$node' /* source */); '$node' /* source */);
} }
_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);
@ -202,7 +211,7 @@ class _DirectiveDependenciesVisitor extends Object
if (node.name is! Label || node.name.label is! SimpleIdentifier) { if (node.name is! Label || node.name.label is! SimpleIdentifier) {
logger.error( logger.error(
'Angular 2 currently only supports simple identifiers in directives.' 'Angular 2 currently only supports simple identifiers in directives.'
' Source: ${node}'); ' Source: ${node}');
return null; return null;
} }
if ('${node.name.label}' == 'directives') { if ('${node.name.label}' == 'directives') {
@ -234,7 +243,7 @@ class _DirectiveDependenciesVisitor extends Object
} else { } else {
logger.error( logger.error(
'Angular 2 currently only supports simple and prefixed identifiers ' 'Angular 2 currently only supports simple and prefixed identifiers '
'as values for "directives". Source: $node'); 'as values for "directives". Source: $node');
return; return;
} }
if (ngMeta.types.containsKey(name)) { if (ngMeta.types.containsKey(name)) {

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();