From db3d5d494124b5012f65205fbb4c4b4da894db45 Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Fri, 12 Jun 2015 09:14:59 +0200 Subject: [PATCH] refactor(_DirectiveMetadataVisitor): simplify the code fixes #2493 --- modules/angular2/src/render/api.ts | 49 ++++---- .../common/directive_metadata_reader.dart | 112 +++++++++++------- 2 files changed, 90 insertions(+), 71 deletions(-) diff --git a/modules/angular2/src/render/api.ts b/modules/angular2/src/render/api.ts index bf6af639af..6f92c5263a 100644 --- a/modules/angular2/src/render/api.ts +++ b/modules/angular2/src/render/api.ts @@ -211,31 +211,6 @@ export class DirectiveMetadata { changeDetection?: string, exportAs?: string }) { - let hostConfig = DirectiveMetadata.parseHostConfig(host); - - return new DirectiveMetadata({ - id: id, - selector: selector, - compileChildren: compileChildren, - events: events, - hostListeners: StringMapWrapper.get(hostConfig, 'hostListeners'), - hostProperties: StringMapWrapper.get(hostConfig, 'hostProperties'), - hostAttributes: StringMapWrapper.get(hostConfig, 'hostAttributes'), - hostActions: StringMapWrapper.get(hostConfig, 'hostActions'), - properties: properties, - readAttributes: readAttributes, - type: type, - callOnDestroy: callOnDestroy, - callOnChange: callOnChange, - callOnCheck: callOnCheck, - callOnInit: callOnInit, - callOnAllChangesDone: callOnAllChangesDone, - changeDetection: changeDetection, - exportAs: exportAs - }); - } - - static parseHostConfig(host?: Map): StringMap> { let hostListeners = MapWrapper.create(); let hostProperties = MapWrapper.create(); let hostAttributes = MapWrapper.create(); @@ -256,10 +231,26 @@ export class DirectiveMetadata { }); } - return { - hostListeners: hostListeners, hostProperties: hostProperties, hostAttributes: hostAttributes, - hostActions: hostActions - } + return new DirectiveMetadata({ + id: id, + selector: selector, + compileChildren: compileChildren, + events: events, + hostListeners: hostListeners, + hostProperties: hostProperties, + hostAttributes: hostAttributes, + hostActions: hostActions, + properties: properties, + readAttributes: readAttributes, + type: type, + callOnDestroy: callOnDestroy, + callOnChange: callOnChange, + callOnCheck: callOnCheck, + callOnInit: callOnInit, + callOnAllChangesDone: callOnAllChangesDone, + changeDetection: changeDetection, + exportAs: exportAs + }); } } diff --git a/modules/angular2/src/transform/common/directive_metadata_reader.dart b/modules/angular2/src/transform/common/directive_metadata_reader.dart index c3621942ae..877454d5d9 100644 --- a/modules/angular2/src/transform/common/directive_metadata_reader.dart +++ b/modules/angular2/src/transform/common/directive_metadata_reader.dart @@ -48,35 +48,71 @@ num _getDirectiveType(String annotationName, Element element) { /// [RegisterType] object and pulling out [DirectiveMetadata]. class _DirectiveMetadataVisitor extends Object with RecursiveAstVisitor { - DirectiveMetadata meta; + bool get _hasMeta => _type != null; + + // Annotation fields + num _type; + String _selector; + bool _compileChildren; + List _properties; + Map _host; + List _readAttributes; + String _exportAs; + bool _callOnDestroy; + bool _callOnChange; + bool _callOnCheck; + bool _callOnInit; + bool _callOnAllChangesDone; + String changeDetection; + List events; + final ConstantEvaluator _evaluator = new ConstantEvaluator(); - void _createEmptyMetadata(num type) { - assert(type >= 0); - meta = DirectiveMetadata.create( - type: type, - compileChildren: true, - properties: [], - host: {}, - readAttributes: [], - exportAs: null, - callOnDestroy: false, - callOnChange: false, - callOnCheck: false, - callOnInit: false, - callOnAllChangesDone: false); + void _initializeMetadata(num directiveType) { + assert(directiveType >= 0); + + _type = directiveType; + _selector = ''; + _compileChildren = true; + _properties = []; + _host = {}; + _readAttributes = []; + _exportAs = null; + _callOnDestroy = false; + _callOnChange = false; + _callOnCheck = false; + _callOnInit = false; + _callOnAllChangesDone = false; + changeDetection = null; + events = []; } + DirectiveMetadata get meta => DirectiveMetadata.create( + type: _type, + selector: _selector, + compileChildren: _compileChildren, + properties: _properties, + host: _host, + readAttributes: _readAttributes, + exportAs: _exportAs, + callOnDestroy: _callOnDestroy, + callOnChange: _callOnChange, + callOnCheck: _callOnCheck, + callOnInit: _callOnInit, + callOnAllChangesDone: _callOnAllChangesDone, + changeDetection: changeDetection, + events: events); + @override Object visitAnnotation(Annotation node) { var directiveType = _getDirectiveType('${node.name}', node.element); if (directiveType >= 0) { - if (meta != null) { + if (_hasMeta) { throw new FormatException('Only one Directive is allowed per class. ' 'Found "$node" but already processed "$meta".', '$node' /* source */); } - _createEmptyMetadata(directiveType); + _initializeMetadata(directiveType); super.visitAnnotation(node); } // Annotation we do not recognize - no need to visit. @@ -88,12 +124,12 @@ class _DirectiveMetadataVisitor extends Object var directiveType = _getDirectiveType( '${node.constructorName.type.name}', node.staticElement); if (directiveType >= 0) { - if (meta != null) { + if (_hasMeta) { throw new FormatException('Only one Directive is allowed per class. ' 'Found "$node" but already processed "$meta".', '$node' /* source */); } - _createEmptyMetadata(directiveType); + _initializeMetadata(directiveType); super.visitInstanceCreationExpression(node); } // Annotation we do not recognize - no need to visit. @@ -109,9 +145,7 @@ class _DirectiveMetadataVisitor extends Object '$node' /* source */); } var keyString = '${node.name.label}'; - // TODO(kegluneq): Populate the other values in [DirectiveMetadata] once - // they are specified as `hostAttributes` and `hostSetters`. - // See [https://github.com/angular/angular/issues/1244] + // TODO(kegluneq): Populate the other values in [DirectiveMetadata] switch (keyString) { case 'selector': _populateSelector(node.expression); @@ -145,14 +179,16 @@ class _DirectiveMetadataVisitor extends Object } void _populateSelector(Expression selectorValue) { - meta.selector = _expressionToString(selectorValue, 'Directive#selector'); + _checkMeta(); + _selector = _expressionToString(selectorValue, 'Directive#selector'); } void _checkMeta() { - if (meta == null) { + if (!_hasMeta) { throw new ArgumentError( 'Incorrect value passed to readDirectiveMetadata. ' - 'Expected types are Annotation and InstanceCreationExpression'); + 'Expected types are Annotation, InstanceCreationExpression, ' + 'NodeList or ListLiteral'); } } @@ -164,7 +200,7 @@ class _DirectiveMetadataVisitor extends Object 'Angular 2 expects a bool but could not understand the value for ' 'Directive#compileChildren.', '$compileChildrenValue' /* source */); } - meta.compileChildren = evaluated; + _compileChildren = evaluated; } /// Evaluates the [Map] represented by `expression` and adds all `key`, @@ -199,25 +235,17 @@ class _DirectiveMetadataVisitor extends Object void _populateProperties(Expression propertiesValue) { _checkMeta(); - _populateList(propertiesValue, meta.properties, 'Directive#properties'); + _populateList(propertiesValue, _properties, 'Directive#properties'); } void _populateHost(Expression hostValue) { _checkMeta(); - var host = new Map(); - _populateMap(hostValue, host, 'Directive#host'); - - var hostConfig = DirectiveMetadata.parseHostConfig(host); - - meta.hostListeners = hostConfig['hostListeners']; - meta.hostProperties = hostConfig['hostProperties']; - meta.hostActions = hostConfig['hostActions']; - meta.hostAttributes = hostConfig['hostAttributes']; + _populateMap(hostValue, _host, 'Directive#host'); } void _populateExportAs(Expression exportAsValue) { _checkMeta(); - meta.exportAs = _expressionToString(exportAsValue, 'Directive#exportAs'); + _exportAs = _expressionToString(exportAsValue, 'Directive#exportAs'); } void _populateLifecycle(Expression lifecycleValue) { @@ -229,10 +257,10 @@ class _DirectiveMetadataVisitor extends Object } ListLiteral l = lifecycleValue; var lifecycleEvents = l.elements.map((s) => s.toSource()); - meta.callOnDestroy = lifecycleEvents.contains("onDestroy"); - meta.callOnChange = lifecycleEvents.contains("onChange"); - meta.callOnCheck = lifecycleEvents.contains("onCheck"); - meta.callOnInit = lifecycleEvents.contains("onInit"); - meta.callOnAllChangesDone = lifecycleEvents.contains("onAllChangesDone"); + _callOnDestroy = lifecycleEvents.contains("onDestroy"); + _callOnChange = lifecycleEvents.contains("onChange"); + _callOnCheck = lifecycleEvents.contains("onCheck"); + _callOnInit = lifecycleEvents.contains("onInit"); + _callOnAllChangesDone = lifecycleEvents.contains("onAllChangesDone"); } }