Skip to content

Commit 6d6e9ee

Browse files
committed
style(tools): code cleanup
1 parent 3be8dbd commit 6d6e9ee

17 files changed

+218
-286
lines changed

lib/tools/common.dart

+7-17
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,11 @@ library angular.tools.common;
33
class DirectiveInfo {
44
String selector;
55
String template;
6-
List<String> expressionAttrs = <String>[];
7-
List<String> expressions = <String>[];
6+
List<String> expressionAttrs;
7+
List<String> expressions;
88
DirectiveInfo([this.selector, this.expressionAttrs, this.expressions]) {
9-
if (expressionAttrs == null) {
10-
expressionAttrs = <String>[];
11-
}
12-
if (expressions == null) {
13-
expressions = <String>[];
14-
}
9+
if (expressionAttrs == null) expressionAttrs = <String>[];
10+
if (expressions == null) expressions = <String>[];
1511
}
1612
}
1713

@@ -30,15 +26,9 @@ class DirectiveMetadata {
3026
DirectiveMetadata([this.className, this.type, this.selector,
3127
this.attributeMappings, this.exportExpressionAttrs,
3228
this.exportExpressions]) {
33-
if (attributeMappings == null) {
34-
attributeMappings = <String, String>{};
35-
}
36-
if (exportExpressions == null) {
37-
exportExpressions = <String>[];
38-
}
39-
if (exportExpressionAttrs == null) {
40-
exportExpressionAttrs = <String>[];
41-
}
29+
if (attributeMappings == null) attributeMappings = <String, String>{};
30+
if (exportExpressions == null) exportExpressions = <String>[];
31+
if (exportExpressionAttrs == null) exportExpressionAttrs = <String>[];
4232
}
4333
}
4434

lib/tools/expression_extractor.dart

+4-14
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ main(args) {
2323
}
2424
IoService ioService = new IoServiceImpl();
2525

26-
var packageRoots =
27-
(args.length < 6) ? [Platform.packageRoot] : args.sublist(5);
26+
var packageRoots = args.length < 6 ? [Platform.packageRoot] : args.sublist(5);
2827
var sourceCrawler = new SourceCrawlerImpl(packageRoots);
2928
var sourceMetadataExtractor = new SourceMetadataExtractor();
3029
List<DirectiveInfo> directives =
@@ -38,17 +37,10 @@ main(args) {
3837
var headerFile = args[2];
3938
var footerFile = args[3];
4039
var outputFile = args[4];
41-
var printer;
42-
if (outputFile == '--') {
43-
printer = stdout;
44-
} else {
45-
printer = new File(outputFile).openWrite();
46-
}
40+
var printer = outputFile == '--' ? stdout : new File(outputFile).openWrite();
4741

4842
// Output the header file first.
49-
if (headerFile != '') {
50-
printer.write(_readFile(headerFile));
51-
}
43+
if (headerFile != '') printer.write(_readFile(headerFile));
5244

5345
printer.write('// Found ${expressions.length} expressions\n');
5446
Module module = new Module()
@@ -67,9 +59,7 @@ main(args) {
6759

6860

6961
// Output footer last.
70-
if (footerFile != '') {
71-
printer.write(_readFile(footerFile));
72-
}
62+
if (footerFile != '') printer.write(_readFile(footerFile));
7363
}
7464

7565
String _readFile(String filePath) => new File(filePath).readAsStringSync();

lib/tools/html_extractor.dart

+2-6
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ class HtmlExpressionExtractor {
1818
HtmlExpressionExtractor(this.directiveInfos) {
1919
for (DirectiveInfo directiveInfo in directiveInfos) {
2020
expressions.addAll(directiveInfo.expressions);
21-
if (directiveInfo.template != null) {
22-
parseHtml(directiveInfo.template);
23-
}
21+
if (directiveInfo.template != null) parseHtml(directiveInfo.template);
2422
}
2523
}
2624

@@ -69,9 +67,7 @@ class HtmlExpressionExtractor {
6967
visitNodes(List<Node> nodes, NodeVisitor visitor) {
7068
for (Node node in nodes) {
7169
visitor(node);
72-
if (node.nodes.length > 0) {
73-
visitNodes(node.nodes, visitor);
74-
}
70+
if (node.nodes.isNotEmpty) visitNodes(node.nodes, visitor);
7571
}
7672
}
7773
}

lib/tools/io.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ library angular.io;
33
typedef FsVisitor(String file);
44

55
/**
6-
* A simple mockabe wrapper around dart:io that can be used without introducing
6+
* A simple mockable wrapper around dart:io that can be used without introducing
77
* direct dependencies on dart:io.
88
*/
99
abstract class IoService {

lib/tools/io_impl.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class IoServiceImpl implements IoService {
99
new File(filePath).readAsStringSync();
1010

1111
void visitFs(String rootDir, FsVisitor visitor) {
12-
Directory root = new Directory(rootDir);
12+
var root = new Directory(rootDir);
1313
if (!FileSystemEntity.isDirectorySync(rootDir)) {
1414
throw 'Expected $rootDir to be a directory!';
1515
}

lib/tools/selector.dart

+11-19
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ class ContainsSelector {
1111
}
1212
}
1313

14-
RegExp _SELECTOR_REGEXP = new RegExp(r'^(?:([\w\-]+)|(?:\.([\w\-]+))|(?:\[([\w\-\*]+)(?:=([^\]]*))?\]))');
15-
RegExp _COMMENT_COMPONENT_REGEXP = new RegExp(r'^\[([\w\-]+)(?:\=(.*))?\]$');
14+
RegExp _SELECTOR_REGEXP = new RegExp(r'^(?:([-\w]+)|(?:\.([-\w]+))|(?:\[([-\w*]+)(?:=([^\]]*))?\]))');
15+
RegExp _COMMENT_COMPONENT_REGEXP = new RegExp(r'^\[([-\w]+)(?:\=(.*))?\]$');
1616
RegExp _CONTAINS_REGEXP = new RegExp(r'^:contains\(\/(.+)\/\)$'); //
1717
RegExp _ATTR_CONTAINS_REGEXP = new RegExp(r'^\[\*=\/(.+)\/\]$'); //
1818

@@ -22,17 +22,17 @@ class _SelectorPart {
2222
final String attrName;
2323
final String attrValue;
2424

25-
const _SelectorPart.fromElement(String this.element)
25+
const _SelectorPart.fromElement(this.element)
2626
: className = null, attrName = null, attrValue = null;
2727

28-
const _SelectorPart.fromClass(String this.className)
28+
const _SelectorPart.fromClass(this.className)
2929
: element = null, attrName = null, attrValue = null;
3030

3131

32-
const _SelectorPart.fromAttribute(String this.attrName, String this.attrValue)
32+
const _SelectorPart.fromAttribute(this.attrName, this.attrValue)
3333
: element = null, className = null;
3434

35-
toString() =>
35+
String toString() =>
3636
element == null
3737
? (className == null
3838
? (attrValue == '' ? '[$attrName]' : '[$attrName=$attrValue]')
@@ -68,19 +68,13 @@ List<_SelectorPart> _splitCss(String selector) {
6868
bool matchesNode(Node node, String selector) {
6969
var match, selectorParts;
7070
if ((match = _CONTAINS_REGEXP.firstMatch(selector)) != null) {
71-
if (node is! Text) {
72-
return false;
73-
}
71+
if (node is! Text) return false;
7472
return new RegExp(match.group(1)).hasMatch((node as Text).text);
7573
} else if ((match = _ATTR_CONTAINS_REGEXP.firstMatch(selector)) != null) {
76-
if (node is! Element) {
77-
return false;
78-
}
74+
if (node is! Element) return false;
7975
var regexp = new RegExp(match.group(1));
8076
for (String attrName in node.attributes.keys) {
81-
if (regexp.hasMatch(node.attributes[attrName])) {
82-
return true;
83-
}
77+
if (regexp.hasMatch(node.attributes[attrName])) return true;
8478
}
8579
return false;
8680
} else if ((selectorParts = _splitCss(selector)) != null) {
@@ -90,9 +84,7 @@ bool matchesNode(Node node, String selector) {
9084
bool stillGood = true;
9185
selectorParts.forEach((_SelectorPart part) {
9286
if (part.element != null) {
93-
if (nodeName != part.element) {
94-
stillGood = false;
95-
}
87+
if (nodeName != part.element) stillGood = false;
9688
} else if (part.className != null) {
9789
if (node.attributes['class'] == null ||
9890
!node.attributes['class'].split(' ').contains(part.className)) {
@@ -116,5 +108,5 @@ bool matchesNode(Node node, String selector) {
116108

117109
String _matchingKey(Iterable keys, String attrName) =>
118110
keys.firstWhere(
119-
(key) => new RegExp('^${attrName.replaceAll('*', r'[\w\-]+')}\$').hasMatch(key.toString()),
111+
(key) => new RegExp('^${attrName.replaceAll('*', r'[-\w]+')}\$').hasMatch(key.toString()),
120112
orElse: () => null);

lib/tools/source_crawler_impl.dart

+12-24
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,19 @@ class SourceCrawlerImpl implements SourceCrawler {
1616
SourceCrawlerImpl(this.packageRoots);
1717

1818
void crawl(String entryPoint, CompilationUnitVisitor visitor) {
19-
List<String> visited = <String>[];
20-
List<String> toVisit = <String>[];
19+
final visited = new Set<String>();
20+
final toVisit = new Set<String>();
2121
if (entryPoint.startsWith(PACKAGE_PREFIX)) {
2222
var path = resolvePackagePath(entryPoint);
23-
if (path == null) {
24-
throw 'Unable to resolve $entryPoint';
25-
}
23+
if (path == null) throw 'Unable to resolve $entryPoint';
2624
toVisit.add(path);
2725
} else {
2826
toVisit.add(entryPoint);
2927
}
3028

3129
while (toVisit.isNotEmpty) {
32-
var currentFile = toVisit.removeAt(0);
30+
var currentFile = toVisit.first;
31+
toVisit.remove(currentFile);
3332
visited.add(currentFile);
3433
var file = new File(currentFile);
3534
// Possible source file doesn't exist. For example if it is generated.
@@ -42,8 +41,8 @@ class SourceCrawlerImpl implements SourceCrawler {
4241
}
4342

4443
void processImports(CompilationUnit cu, String currentDir,
45-
String currentFile, List<String> visited,
46-
List<String> toVisit) {
44+
String currentFile, Set<String> visited,
45+
Set<String> toVisit) {
4746
cu.directives.forEach((Directive directive) {
4847
if (directive is ImportDirective ||
4948
directive is PartDirective ||
@@ -52,10 +51,7 @@ class SourceCrawlerImpl implements SourceCrawler {
5251
String canonicalFile = canonicalizeImportPath(
5352
currentDir, currentFile, import.uri.stringValue);
5453
if (canonicalFile == null) return;
55-
if (!visited.contains(canonicalFile) &&
56-
!toVisit.contains(canonicalFile)) {
57-
toVisit.add(canonicalFile);
58-
}
54+
if (!visited.contains(canonicalFile)) toVisit.add(canonicalFile);
5955
}
6056
});
6157
}
@@ -64,12 +60,8 @@ class SourceCrawlerImpl implements SourceCrawler {
6460
String currentFile,
6561
String uri) {
6662
// ignore core libraries
67-
if (uri.startsWith('dart:')) {
68-
return null;
69-
}
70-
if (uri.startsWith(PACKAGE_PREFIX)) {
71-
return resolvePackagePath(uri);
72-
}
63+
if (uri.startsWith('dart:')) return null;
64+
if (uri.startsWith(PACKAGE_PREFIX)) return resolvePackagePath(uri);
7365
// relative import.
7466
if (uri.startsWith('../')) {
7567
while (uri.startsWith('../')) {
@@ -83,18 +75,14 @@ class SourceCrawlerImpl implements SourceCrawler {
8375
String resolvePackagePath(String uri) {
8476
for (String packageRoot in packageRoots) {
8577
var resolvedPath = _packageUriResolver(uri, packageRoot);
86-
if (new File(resolvedPath).existsSync()) {
87-
return resolvedPath;
88-
}
78+
if (new File(resolvedPath).existsSync()) return resolvedPath;
8979
}
9080
return null;
9181
}
9282

9383
String _packageUriResolver(String uri, String packageRoot) {
9484
var packagePath = uri.substring(PACKAGE_PREFIX.length);
95-
if (!packageRoot.endsWith('/')) {
96-
packageRoot = packageRoot + '/';
97-
}
85+
if (!packageRoot.endsWith('/')) packageRoot = packageRoot + '/';
9886
return packageRoot + packagePath;
9987
}
10088
}

0 commit comments

Comments
 (0)