Skip to content

Commit

Permalink
feat(transformers): update transformers to handle components without @…
Browse files Browse the repository at this point in the history
  • Loading branch information
vsavkin committed Oct 7, 2015
1 parent bd31b01 commit a2e7ae5
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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') {
Expand Down Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit a2e7ae5

Please sign in to comment.