fix(dart/transform): Handle `hostAttributes` in DirectiveMetadata

Handle `hostAttributes` in the transformer.
`hostAttributes` was introduced in 51839ca677

Closes #1742
This commit is contained in:
Tim Blasi 2015-05-08 09:35:44 -07:00 committed by Misko Hevery
parent 44f829dbc6
commit 200e190f70
4 changed files with 54 additions and 6 deletions

View File

@ -47,6 +47,7 @@ class _DirectiveMetadataVisitor extends Object
properties: {}, properties: {},
hostListeners: {}, hostListeners: {},
hostProperties: {}, hostProperties: {},
hostAttributes: {},
readAttributes: []); readAttributes: []);
super.visitInstanceCreationExpression(node); super.visitInstanceCreationExpression(node);
} }
@ -80,6 +81,9 @@ class _DirectiveMetadataVisitor extends Object
case 'hostProperties': case 'hostProperties':
_populateHostProperties(node.expression); _populateHostProperties(node.expression);
break; break;
case 'hostAttributes':
_populateHostAttributes(node.expression);
break;
case 'hostListeners': case 'hostListeners':
_populateHostListeners(node.expression); _populateHostListeners(node.expression);
} }
@ -155,4 +159,20 @@ class _DirectiveMetadataVisitor extends Object
meta.hostProperties[sKey] = sVal; meta.hostProperties[sKey] = sVal;
} }
} }
void _populateHostAttributes(Expression hostAttributeValue) {
if (hostAttributeValue is! MapLiteral) {
logger.error('Angular 2 currently only supports map literal values for '
'Directive#hostAttributes.'
' Source: ${hostAttributeValue}');
return;
}
for (MapLiteralEntry entry in (hostAttributeValue as MapLiteral).entries) {
var sKey =
_expressionToString(entry.key, 'Directive#hostAttributes keys');
var sVal =
_expressionToString(entry.value, 'Directive#hostAttributes values');
meta.hostAttributes[sKey] = sVal;
}
}
} }

View File

@ -16,6 +16,8 @@ import 'extractor.dart';
/// These files contain commented Json-formatted representations of all /// These files contain commented Json-formatted representations of all
/// `Directive`s in the associated file. /// `Directive`s in the associated file.
class DirectiveMetadataExtractor extends Transformer { class DirectiveMetadataExtractor extends Transformer {
final _encoder = const JsonEncoder.withIndent(' ');
DirectiveMetadataExtractor(); DirectiveMetadataExtractor();
@override @override
@ -36,7 +38,7 @@ class DirectiveMetadataExtractor extends Transformer {
jsonMap[k] = directiveMetadataToMap(v); jsonMap[k] = directiveMetadataToMap(v);
}); });
transform.addOutput(new Asset.fromString( transform.addOutput(new Asset.fromString(
_outputAssetId(fromAssetId), JSON.encode(jsonMap))); _outputAssetId(fromAssetId), _encoder.convert(jsonMap)));
} }
} catch (ex, stackTrace) { } catch (ex, stackTrace) {
log.logger.error('Extracting ng metadata failed.\n' log.logger.error('Extracting ng metadata failed.\n'

View File

@ -29,6 +29,10 @@ class ViewDefinitionResults {
String _getComponentId(AssetId assetId, String className) => String _getComponentId(AssetId assetId, String className) =>
'$assetId:$className'; '$assetId:$className';
// TODO(kegluenq): Improve this test.
bool _isViewAnnotation(InstanceCreationExpression node) =>
'${node.constructorName.type}' == 'View';
/// Creates [ViewDefinition] objects for all `View` `Directive`s defined in /// Creates [ViewDefinition] objects for all `View` `Directive`s defined in
/// `entryPoint`. /// `entryPoint`.
class _ViewDefinitionCreator { class _ViewDefinitionCreator {
@ -108,8 +112,9 @@ class _ViewDefinitionCreator {
/// ``` /// ```
/// ///
/// This method will look for `component.ng_meta.json`to contain the /// This method will look for `component.ng_meta.json`to contain the
/// serialized [DirectiveMetadata] `MyComponent` and any other `Directive`s /// serialized [DirectiveMetadata] for `MyComponent` and any other
/// declared in `component.dart`. We use this information to build a map: /// `Directive`s declared in `component.dart`. We use this information to
/// build a map:
/// ///
/// ``` /// ```
/// { /// {
@ -146,7 +151,7 @@ class _ViewDefinitionCreator {
} }
/// Visitor responsible for processing the `annotations` property of a /// Visitor responsible for processing the `annotations` property of a
/// {@link RegisterType} object and pulling out [ViewDefinition] information. /// [RegisterType] object and pulling out [ViewDefinition] information.
class _TemplateExtractVisitor extends Object with RecursiveAstVisitor<Object> { class _TemplateExtractVisitor extends Object with RecursiveAstVisitor<Object> {
ViewDefinition viewDef = null; ViewDefinition viewDef = null;
final Map<String, DirectiveMetadata> _metadataMap; final Map<String, DirectiveMetadata> _metadataMap;
@ -160,7 +165,7 @@ class _TemplateExtractVisitor extends Object with RecursiveAstVisitor<Object> {
/// These correspond to the annotations themselves. /// These correspond to the annotations themselves.
@override @override
Object visitInstanceCreationExpression(InstanceCreationExpression node) { Object visitInstanceCreationExpression(InstanceCreationExpression node) {
if ('${node.constructorName.type}' == 'View') { if (_isViewAnnotation(node)) {
viewDef = new ViewDefinition(directives: <DirectiveMetadata>[]); viewDef = new ViewDefinition(directives: <DirectiveMetadata>[]);
node.visitChildren(this); node.visitChildren(this);
} }
@ -182,6 +187,10 @@ class _TemplateExtractVisitor extends Object with RecursiveAstVisitor<Object> {
_readDirectives(node.expression); _readDirectives(node.expression);
} }
if (keyString == 'template' || keyString == 'templateUrl') { if (keyString == 'template' || keyString == 'templateUrl') {
// This could happen in a non-View annotation with a `template` or
// `templateUrl` property.
if (viewDef == null) return null;
if (node.expression is! SimpleStringLiteral) { if (node.expression is! SimpleStringLiteral) {
logger.error( logger.error(
'Angular 2 currently only supports string literals in directives.' 'Angular 2 currently only supports string literals in directives.'
@ -207,6 +216,10 @@ class _TemplateExtractVisitor extends Object with RecursiveAstVisitor<Object> {
} }
void _readDirectives(Expression node) { void _readDirectives(Expression node) {
// This could happen in a non-View annotation with a `directives`
// parameter.
if (viewDef == null) return;
if (node is! ListLiteral) { if (node is! ListLiteral) {
logger.error( logger.error(
'Angular 2 currently only supports list literals as values for' 'Angular 2 currently only supports list literals as values for'

View File

@ -1 +1,14 @@
{"MyComponent":{"id":"MyComponent","selector":"[soup]","compileChildren":true,"hostListeners":{},"hostProperties":{},"properties":{},"readAttributes":[],"type":1,"version":1}} {
"MyComponent": {
"id": "MyComponent",
"selector": "[soup]",
"compileChildren": true,
"hostListeners": {},
"hostProperties": {},
"hostAttributes": {},
"properties": {},
"readAttributes": [],
"type": 1,
"version": 1
}
}