From 81dfeeace6401c2bb481305cc88086feae5c77ed Mon Sep 17 00:00:00 2001 From: vsavkin Date: Mon, 23 Nov 2015 16:09:27 -0800 Subject: [PATCH] fix(transformers): dedup directives Closes #5311 --- .../compile_data_creator.dart | 23 ++++++++++++++--- .../template_compiler/all_tests.dart | 25 +++++++++++++++++++ 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/modules_dart/transform/lib/src/transform/template_compiler/compile_data_creator.dart b/modules_dart/transform/lib/src/transform/template_compiler/compile_data_creator.dart index d011116c49c3c..b322b97c6d0f5 100644 --- a/modules_dart/transform/lib/src/transform/template_compiler/compile_data_creator.dart +++ b/modules_dart/transform/lib/src/transform/template_compiler/compile_data_creator.dart @@ -59,6 +59,7 @@ class _CompileDataCreator { NgDepsModel get ngDeps => ngMeta.ngDeps; + Future createCompileData() async { if (ngDeps == null || ngDeps.reflectables == null) { return new CompileDataResults._(ngMeta, const {}); @@ -75,22 +76,23 @@ class _CompileDataCreator { if (compileDirectiveMetadata.template != null) { final compileDatum = new NormalizedComponentWithViewDirectives( compileDirectiveMetadata, []); - compileDatum.directives.addAll(platformDirectives); + final directives = []; + directives.addAll(platformDirectives); for (var dep in reflectable.directives) { if (!ngMetaMap.containsKey(dep.prefix)) { log.warning( 'Missing prefix "${dep.prefix}" ' - 'needed by "${dep}" from metadata map', + 'needed by "${dep}" from metadata map', asset: entryPoint); continue; } final depNgMeta = ngMetaMap[dep.prefix]; if (depNgMeta.types.containsKey(dep.name)) { - compileDatum.directives.add(depNgMeta.types[dep.name]); + directives.add(depNgMeta.types[dep.name]); } else if (depNgMeta.aliases.containsKey(dep.name)) { - compileDatum.directives.addAll(depNgMeta.flatten(dep.name)); + directives.addAll(depNgMeta.flatten(dep.name)); } else { log.warning('Could not find Directive entry for $dep. ' 'Please be aware that Dart transformers have limited support for ' @@ -98,6 +100,7 @@ class _CompileDataCreator { '"directive aliases"). See https://goo.gl/d8XPt0 for details.'); } } + compileDatum.directives.addAll(removeDuplicates(directives)); compileData[reflectable] = compileDatum; } } @@ -105,6 +108,18 @@ class _CompileDataCreator { return new CompileDataResults._(ngMeta, compileData); } + List removeDuplicates(List directives) { + final res = []; + directives.forEach((dir) { + if (! res.any((v) => v.type.name == dir.type.name && + v.type.moduleUrl == dir.type.moduleUrl && + v.type.isHost == dir.type.isHost)) { + res.add(dir); + } + }); + return res; + } + Future> _readPlatformDirectives() async { if (platformDirectives == null) return const []; diff --git a/modules_dart/transform/test/transform/template_compiler/all_tests.dart b/modules_dart/transform/test/transform/template_compiler/all_tests.dart index 57a0fcbab92b6..71d2b00d9fa51 100644 --- a/modules_dart/transform/test/transform/template_compiler/all_tests.dart +++ b/modules_dart/transform/test/transform/template_compiler/all_tests.dart @@ -415,6 +415,31 @@ void allTests() { final ngDeps = outputs.ngDeps; expect(ngDeps).toBeNotNull(); }); + + it('should dedup directvies.', + () async { + fooComponentMeta.template = new CompileTemplateMetadata(template: ''); + + final viewAnnotation = new AnnotationModel() + ..name = 'View' + ..isView = true; + viewAnnotation.namedParameters.add(new NamedParameter() + ..name = 'directives' + ..value = 'const [${barComponentMeta.type.name}]'); + fooNgMeta.ngDeps.reflectables.first.directives + .add(new PrefixedDirective()..name = barComponentMeta.type.name); + fooNgMeta.ngDeps.imports.add(new ImportModel()..uri = 'bar.dart'); + fooNgMeta.ngDeps.reflectables.first.annotations.add(viewAnnotation); + + barNgMeta.aliases['PLATFORM'] = [barComponentMeta.type.name]; + updateReader(); + + final outputs = await process(fooAssetId, + platformDirectives: ['package:a/bar.dart#PLATFORM']); + final oneBar = outputs.templatesCode.indexOf("BeginComponentCmd('bar'") == + outputs.templatesCode.lastIndexOf("BeginComponentCmd('bar'"); + expect(oneBar).toBe(true); + }); } void _formatThenExpectEquals(String actual, String expected) {