Skip to content
This repository has been archived by the owner on Feb 22, 2018. It is now read-only.

Commit

Permalink
fix(expression_extractor): correctly support @ mapping spec, remove o…
Browse files Browse the repository at this point in the history
…ld specs
  • Loading branch information
pavelgj committed Nov 15, 2013
1 parent eb6ba0d commit 33e30db
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 43 deletions.
12 changes: 7 additions & 5 deletions lib/tools/source_metadata_extractor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const String _COMPONENT = '-component';
const String _DIRECTIVE = '-directive';
String _ATTR_DIRECTIVE = '-attr' + _DIRECTIVE;
RegExp _ATTR_SELECTOR_REGEXP = new RegExp(r'\[([^\]]+)\]');
const List<String> _specs = const ['=>!', '=>', '<=>', '=', '!', '@', '&', '#', '%'];
const List<String> _specs = const ['=>!', '=>', '<=>', '@', '&'];

class SourceMetadataExtractor {
SourceCrawler sourceCrawler;
Expand All @@ -31,17 +31,19 @@ class SourceMetadataExtractor {
DirectiveInfo dirInfo = new DirectiveInfo();
dirInfo.selector = meta.selector;
meta.attributeMappings.forEach((attrName, mappingSpec) {
dirInfo.expressionAttrs.add(snakecase(attrName));
var spec = _specs
.firstWhere((specPrefix) => mappingSpec.startsWith(specPrefix),
orElse: () => throw '$mappingSpec no matching spec');
if (spec != '@') {
dirInfo.expressionAttrs.add(snakecase(attrName));
}
if (mappingSpec.length == 1) { // Shorthand. Remove.
// TODO(pavelgj): Figure out if short-hand LHS should be expanded
// and added to the expressions list.
if (attrName != '.') {
dirInfo.expressions.add(_maybeCamelCase(attrName));
}
} else {
var spec = _specs
.firstWhere((specPrefix) => mappingSpec.startsWith(specPrefix),
orElse: () => throw '$mappingSpec no matching spec');
mappingSpec = mappingSpec.substring(spec.length);
if (mappingSpec.startsWith('.')) {
mappingSpec = mappingSpec.substring(1);
Expand Down
60 changes: 22 additions & 38 deletions test/tools/source_metadata_extractor_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,47 +12,31 @@ main() => describe('SourceMetadataExtractor', () {
it('should extract expressions and attribute names with expressions', () {
var info = extractDirectiveInfo([
new DirectiveMetadata('FooComponent', COMPONENT, null, {
'fooVal': '@',
'barVal': '@ctrl.bar',
'bazVal': '@.baz',
'fooExpr': '=',
'barExpr': '=ctrl.bar',
'bazExpr': '=.baz',
'fooCallback': '&',
'barCallback': '&ctrl.bar',
'bazCallback': '&.baz',
'oneTime': '!'
'barVal': '@bar',
'bazExpr1': '<=>baz1',
'bazExpr2': '=>baz2',
'bazExpr3': '=>!baz3',
'bazCallback': '&aux',
})
]);

expect(flattenList(info, (DirectiveInfo i) => i.expressionAttrs),
equals(['foo-val',
'bar-val',
'baz-val',
'foo-expr',
'bar-expr',
'baz-expr',
'foo-callback',
'bar-callback',
'baz-callback',
'one-time']));
equals(['baz-expr1',
'baz-expr2',
'baz-expr3',
'baz-callback']));
expect(flattenList(info, (DirectiveInfo i) => i.expressions),
equals(['fooVal',
'ctrl.bar',
'baz',
'fooExpr',
'ctrl.bar',
'baz',
'fooCallback',
'ctrl.bar',
'baz',
'oneTime']));
equals(['bar',
'baz1',
'baz2',
'baz3',
'aux']));
});

it('should build a component selector if one is not explicitly specified', () {
var info = extractDirectiveInfo([
new DirectiveMetadata('MyFooComponent', COMPONENT, null, {
'fooExpr': '='
'foo-expr': '=>fooExpr'
})
]);

Expand All @@ -63,7 +47,7 @@ main() => describe('SourceMetadataExtractor', () {
it('should build an element directive selector if one is not explicitly specified', () {
var info = extractDirectiveInfo([
new DirectiveMetadata('MyFooDirective', DIRECTIVE, null, {
'fooExpr': '='
'foo-expr': '=>fooExpr'
})
]);

Expand All @@ -74,7 +58,7 @@ main() => describe('SourceMetadataExtractor', () {
it('should build an attr directive selector if one is not explicitly specified', () {
var info = extractDirectiveInfo([
new DirectiveMetadata('MyFooAttrDirective', DIRECTIVE, null, {
'fooExpr': '='
'foo-expr': '=>fooExpr'
})
]);

Expand All @@ -85,7 +69,7 @@ main() => describe('SourceMetadataExtractor', () {
it('should figure out attribute name if dot(.) is used', () {
var info = extractDirectiveInfo([
new DirectiveMetadata('MyFooAttrDirective', DIRECTIVE, null, {
'.': '='
'.': '=>fooExpr'
})
]);

Expand All @@ -96,7 +80,7 @@ main() => describe('SourceMetadataExtractor', () {
it('should figure out attribute name from selector if dot(.) is used', () {
var info = extractDirectiveInfo([
new DirectiveMetadata('MyFooAttrDirective', DIRECTIVE, '[blah][foo]', {
'.': '='
'.': '=>fooExpr'
})
]);

Expand All @@ -107,7 +91,7 @@ main() => describe('SourceMetadataExtractor', () {
it('should include exported expression attributes', () {
var info = extractDirectiveInfo([
new DirectiveMetadata('MyFooAttrDirective', DIRECTIVE, '[blah][foo]', {
'.': '='
'.': '=>fooExpr'
}, ['baz'])
]);

Expand All @@ -118,12 +102,12 @@ main() => describe('SourceMetadataExtractor', () {
it('should include exported expressions', () {
var info = extractDirectiveInfo([
new DirectiveMetadata('MyFooAttrDirective', DIRECTIVE, '[blah][foo]', {
'.': '='
'.': '=>fooExpr'
}, null, ['ctrl.baz'])
]);

expect(flattenList(info, (DirectiveInfo i) => i.expressions),
equals(['ctrl.baz']));
equals(['fooExpr', 'ctrl.baz']));
});

});
Expand Down

0 comments on commit 33e30db

Please sign in to comment.