From cf103de4a7f4a77a94c753be6dab9c0b628ca93f Mon Sep 17 00:00:00 2001 From: Jacob MacDonald Date: Mon, 13 Jul 2015 08:36:44 -0700 Subject: [PATCH] fix(transformer): Event getters now use property name not event name --- .../transform/bind_generator/generator.dart | 33 ++++++++++--------- .../reflection_capabilities.dart | 4 ++- .../events_files/bar.ng_deps.dart | 2 +- .../events_files/expected/bar.ng_deps.dart | 4 +-- 4 files changed, 23 insertions(+), 20 deletions(-) diff --git a/modules/angular2/src/transform/bind_generator/generator.dart b/modules/angular2/src/transform/bind_generator/generator.dart index 602b5e7eac..b3607c3d1d 100644 --- a/modules/angular2/src/transform/bind_generator/generator.dart +++ b/modules/angular2/src/transform/bind_generator/generator.dart @@ -15,7 +15,7 @@ Future createNgSettersAndGetters( String code = ngDeps.code; var setters = _generateSetters(_createPropertiesMap(ngDeps)); - var getters = _generateGetters(_createEventsMap(ngDeps)); + var getters = _generateGetters( _createEventPropertiesList(ngDeps)); if (setters.isEmpty && getters.isEmpty) return code; var out = new StringBuffer(); @@ -79,35 +79,36 @@ Map _createPropertiesMap(NgDeps ngDeps) { return bindMap; } -/// Consumes the map generated by {@link _createEventsMap} to codegen getters. -List _generateGetters(Map bindMap) { +/// Consumes the list generated by {@link _createEventPropertiesList} to codegen +/// getters. +List _generateGetters(List eventProperties) { var getters = []; // TODO(kegluneq): Include types for receivers. See #886. - bindMap.forEach((getterName, eventName) { - if (!prop.isValid(eventName)) { + for (var property in eventProperties) { + if (!prop.isValid(property)) { // TODO(kegluenq): Eagerly throw here once #1295 is addressed. - getters.add(prop.lazyInvalidGetter(eventName)); + getters.add(prop.lazyInvalidGetter(property)); } else { - getters.add(''' '${prop.sanitize(eventName)}': (o) => o.$getterName'''); + getters.add(''' '${prop.sanitize(property)}': (o) => o.$property'''); } - }); + } return getters; } -/// Collapses all `events` in {@link ngDeps} into a map where the keys are -/// the property names for the event emitters and the values are the event name. -Map _createEventsMap(NgDeps ngDeps) { +/// Collapses all `events` in {@link ngDeps} into a list of corresponding +/// property names. +List _createEventPropertiesList(NgDeps ngDeps) { var visitor = new ExtractNamedExpressionVisitor('events'); - var bindMap = {}; + var propertyNames = []; ngDeps.registeredTypes.forEach((RegisteredType t) { visitor.bindConfig.clear(); t.annotations.accept(visitor); visitor.bindConfig.forEach((String config) { // See comments for `Directive` in annotations_impl/annotations.ts for - // details on how `events` is specified. - var parts = config.split(':').map((p) => p.trim()).toList(); - bindMap[parts[0]] = parts.length > 1 ? parts[1] : parts[0]; + // details on how `events` is specified. We are pulling out the property + // name only (everything before the first `:`). + propertyNames.add(config.split(':').first.trim()); }); }); - return bindMap; + return propertyNames; } diff --git a/modules/angular2/src/transform/template_compiler/reflection_capabilities.dart b/modules/angular2/src/transform/template_compiler/reflection_capabilities.dart index f43613bb7d..9c24e6fe3c 100644 --- a/modules/angular2/src/transform/template_compiler/reflection_capabilities.dart +++ b/modules/angular2/src/transform/template_compiler/reflection_capabilities.dart @@ -10,7 +10,9 @@ class NullReflectionCapabilities implements ReflectionCapabilities { _notImplemented(String name) => throw 'Not implemented: $name'; - bool isReflectionEnabled() { return false; } + bool isReflectionEnabled() { + return false; + } Function factory(Type type) => _notImplemented('factory'); diff --git a/modules/angular2/test/transform/bind_generator/events_files/bar.ng_deps.dart b/modules/angular2/test/transform/bind_generator/events_files/bar.ng_deps.dart index 4fee5dcb61..b3fd71ee9e 100644 --- a/modules/angular2/test/transform/bind_generator/events_files/bar.ng_deps.dart +++ b/modules/angular2/test/transform/bind_generator/events_files/bar.ng_deps.dart @@ -13,7 +13,7 @@ void initReflector(reflector) { 'parameters': const [], 'annotations': const [ const Directive( - selector: '[tool-tip]', events: ['onOpen', 'close: onClose']) + selector: '[tool-tip]', events: const ['onOpen', 'close: onClose']) ] }); } diff --git a/modules/angular2/test/transform/bind_generator/events_files/expected/bar.ng_deps.dart b/modules/angular2/test/transform/bind_generator/events_files/expected/bar.ng_deps.dart index 1eb7260695..1c0c88f6c4 100644 --- a/modules/angular2/test/transform/bind_generator/events_files/expected/bar.ng_deps.dart +++ b/modules/angular2/test/transform/bind_generator/events_files/expected/bar.ng_deps.dart @@ -13,8 +13,8 @@ void initReflector(reflector) { 'parameters': const [], 'annotations': const [ const Directive( - selector: '[tool-tip]', events: ['onOpen', 'close: onClose']) + selector: '[tool-tip]', events: const ['onOpen', 'close: onClose']) ] }) - ..registerGetters({'onOpen': (o) => o.onOpen, 'onClose': (o) => o.close}); + ..registerGetters({'onOpen': (o) => o.onOpen, 'close': (o) => o.close}); }