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;
Map<String, String> _host;
List<LifecycleHooks> _lifecycleHooks;
CompileTemplateMetadata _template;
CompileTemplateMetadata _cmpTemplate;
CompileTemplateMetadata _viewTemplate;
void reset(AssetId assetId) {
_lifecycleVisitor.reset(assetId);
@ -157,23 +158,29 @@ class _DirectiveMetadataVisitor extends Object
_outputs = <String>[];
_host = <String, String>{};
_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.

View File

@ -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<CompileDataResults> createCompileData(
AssetReader reader, AssetId entryPoint) async {
Future<CompileDataResults> 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, <CompileDirectiveMetadata>[]);
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)) {

View File

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

View File

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