Skip to content

Commit 200e190

Browse files
Tim Blasimhevery
authored andcommitted
fix(dart/transform): Handle hostAttributes in DirectiveMetadata
Handle `hostAttributes` in the transformer. `hostAttributes` was introduced in 51839ca Closes angular#1742
1 parent 44f829d commit 200e190

4 files changed

Lines changed: 54 additions & 6 deletions

File tree

modules/angular2/src/transform/common/directive_metadata_reader.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class _DirectiveMetadataVisitor extends Object
4747
properties: {},
4848
hostListeners: {},
4949
hostProperties: {},
50+
hostAttributes: {},
5051
readAttributes: []);
5152
super.visitInstanceCreationExpression(node);
5253
}
@@ -80,6 +81,9 @@ class _DirectiveMetadataVisitor extends Object
8081
case 'hostProperties':
8182
_populateHostProperties(node.expression);
8283
break;
84+
case 'hostAttributes':
85+
_populateHostAttributes(node.expression);
86+
break;
8387
case 'hostListeners':
8488
_populateHostListeners(node.expression);
8589
}
@@ -155,4 +159,20 @@ class _DirectiveMetadataVisitor extends Object
155159
meta.hostProperties[sKey] = sVal;
156160
}
157161
}
162+
163+
void _populateHostAttributes(Expression hostAttributeValue) {
164+
if (hostAttributeValue is! MapLiteral) {
165+
logger.error('Angular 2 currently only supports map literal values for '
166+
'Directive#hostAttributes.'
167+
' Source: ${hostAttributeValue}');
168+
return;
169+
}
170+
for (MapLiteralEntry entry in (hostAttributeValue as MapLiteral).entries) {
171+
var sKey =
172+
_expressionToString(entry.key, 'Directive#hostAttributes keys');
173+
var sVal =
174+
_expressionToString(entry.value, 'Directive#hostAttributes values');
175+
meta.hostAttributes[sKey] = sVal;
176+
}
177+
}
158178
}

modules/angular2/src/transform/directive_metadata_extractor/transformer.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import 'extractor.dart';
1616
/// These files contain commented Json-formatted representations of all
1717
/// `Directive`s in the associated file.
1818
class DirectiveMetadataExtractor extends Transformer {
19+
final _encoder = const JsonEncoder.withIndent(' ');
20+
1921
DirectiveMetadataExtractor();
2022

2123
@override
@@ -36,7 +38,7 @@ class DirectiveMetadataExtractor extends Transformer {
3638
jsonMap[k] = directiveMetadataToMap(v);
3739
});
3840
transform.addOutput(new Asset.fromString(
39-
_outputAssetId(fromAssetId), JSON.encode(jsonMap)));
41+
_outputAssetId(fromAssetId), _encoder.convert(jsonMap)));
4042
}
4143
} catch (ex, stackTrace) {
4244
log.logger.error('Extracting ng metadata failed.\n'

modules/angular2/src/transform/template_compiler/view_definition_creator.dart

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ class ViewDefinitionResults {
2929
String _getComponentId(AssetId assetId, String className) =>
3030
'$assetId:$className';
3131

32+
// TODO(kegluenq): Improve this test.
33+
bool _isViewAnnotation(InstanceCreationExpression node) =>
34+
'${node.constructorName.type}' == 'View';
35+
3236
/// Creates [ViewDefinition] objects for all `View` `Directive`s defined in
3337
/// `entryPoint`.
3438
class _ViewDefinitionCreator {
@@ -108,8 +112,9 @@ class _ViewDefinitionCreator {
108112
/// ```
109113
///
110114
/// This method will look for `component.ng_meta.json`to contain the
111-
/// serialized [DirectiveMetadata] `MyComponent` and any other `Directive`s
112-
/// declared in `component.dart`. We use this information to build a map:
115+
/// serialized [DirectiveMetadata] for `MyComponent` and any other
116+
/// `Directive`s declared in `component.dart`. We use this information to
117+
/// build a map:
113118
///
114119
/// ```
115120
/// {
@@ -146,7 +151,7 @@ class _ViewDefinitionCreator {
146151
}
147152

148153
/// Visitor responsible for processing the `annotations` property of a
149-
/// {@link RegisterType} object and pulling out [ViewDefinition] information.
154+
/// [RegisterType] object and pulling out [ViewDefinition] information.
150155
class _TemplateExtractVisitor extends Object with RecursiveAstVisitor<Object> {
151156
ViewDefinition viewDef = null;
152157
final Map<String, DirectiveMetadata> _metadataMap;
@@ -160,7 +165,7 @@ class _TemplateExtractVisitor extends Object with RecursiveAstVisitor<Object> {
160165
/// These correspond to the annotations themselves.
161166
@override
162167
Object visitInstanceCreationExpression(InstanceCreationExpression node) {
163-
if ('${node.constructorName.type}' == 'View') {
168+
if (_isViewAnnotation(node)) {
164169
viewDef = new ViewDefinition(directives: <DirectiveMetadata>[]);
165170
node.visitChildren(this);
166171
}
@@ -182,6 +187,10 @@ class _TemplateExtractVisitor extends Object with RecursiveAstVisitor<Object> {
182187
_readDirectives(node.expression);
183188
}
184189
if (keyString == 'template' || keyString == 'templateUrl') {
190+
// This could happen in a non-View annotation with a `template` or
191+
// `templateUrl` property.
192+
if (viewDef == null) return null;
193+
185194
if (node.expression is! SimpleStringLiteral) {
186195
logger.error(
187196
'Angular 2 currently only supports string literals in directives.'
@@ -207,6 +216,10 @@ class _TemplateExtractVisitor extends Object with RecursiveAstVisitor<Object> {
207216
}
208217

209218
void _readDirectives(Expression node) {
219+
// This could happen in a non-View annotation with a `directives`
220+
// parameter.
221+
if (viewDef == null) return;
222+
210223
if (node is! ListLiteral) {
211224
logger.error(
212225
'Angular 2 currently only supports list literals as values for'
Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,14 @@
1-
{"MyComponent":{"id":"MyComponent","selector":"[soup]","compileChildren":true,"hostListeners":{},"hostProperties":{},"properties":{},"readAttributes":[],"type":1,"version":1}}
1+
{
2+
"MyComponent": {
3+
"id": "MyComponent",
4+
"selector": "[soup]",
5+
"compileChildren": true,
6+
"hostListeners": {},
7+
"hostProperties": {},
8+
"hostAttributes": {},
9+
"properties": {},
10+
"readAttributes": [],
11+
"type": 1,
12+
"version": 1
13+
}
14+
}

0 commit comments

Comments
 (0)