Skip to content

Commit

Permalink
fix(transformers): dedup directives
Browse files Browse the repository at this point in the history
  • Loading branch information
vsavkin committed Nov 24, 2015
1 parent 230aad4 commit 81dfeea
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class _CompileDataCreator {

NgDepsModel get ngDeps => ngMeta.ngDeps;


Future<CompileDataResults> createCompileData() async {
if (ngDeps == null || ngDeps.reflectables == null) {
return new CompileDataResults._(ngMeta, const {});
Expand All @@ -75,36 +76,50 @@ class _CompileDataCreator {
if (compileDirectiveMetadata.template != null) {
final compileDatum = new NormalizedComponentWithViewDirectives(
compileDirectiveMetadata, <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 '
'reusable, pre-defined lists of Directives (aka '
'"directive aliases"). See https://goo.gl/d8XPt0 for details.');
}
}
compileDatum.directives.addAll(removeDuplicates(directives));
compileData[reflectable] = compileDatum;
}
}
}
return new CompileDataResults._(ngMeta, compileData);
}

List<CompileDirectiveMetadata> removeDuplicates(List<CompileDirectiveMetadata> 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<List<CompileDirectiveMetadata>> _readPlatformDirectives() async {
if (platformDirectives == null) return const [];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,31 @@ void allTests() {
final ngDeps = outputs.ngDeps;
expect(ngDeps).toBeNotNull();
});

it('should dedup directvies.',
() async {
fooComponentMeta.template = new CompileTemplateMetadata(template: '<bar/>');

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) {
Expand Down

0 comments on commit 81dfeea

Please sign in to comment.