Skip to content

Commit

Permalink
style(tools): code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
vicb committed Apr 15, 2014
1 parent 3be8dbd commit 6d6e9ee
Show file tree
Hide file tree
Showing 17 changed files with 218 additions and 286 deletions.
24 changes: 7 additions & 17 deletions lib/tools/common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@ library angular.tools.common;
class DirectiveInfo {
String selector;
String template;
List<String> expressionAttrs = <String>[];
List<String> expressions = <String>[];
List<String> expressionAttrs;
List<String> expressions;
DirectiveInfo([this.selector, this.expressionAttrs, this.expressions]) {
if (expressionAttrs == null) {
expressionAttrs = <String>[];
}
if (expressions == null) {
expressions = <String>[];
}
if (expressionAttrs == null) expressionAttrs = <String>[];

This comment has been minimized.

Copy link
@pavelgj

pavelgj Apr 17, 2014

This is not a good style and even if acceptable it's not worth the code churn. Please avoid making these unnecessary changes.

This comment has been minimized.

Copy link
@vicb

vicb Apr 17, 2014

Author Owner

got it.

if (expressions == null) expressions = <String>[];
}
}

Expand All @@ -30,15 +26,9 @@ class DirectiveMetadata {
DirectiveMetadata([this.className, this.type, this.selector,
this.attributeMappings, this.exportExpressionAttrs,
this.exportExpressions]) {
if (attributeMappings == null) {
attributeMappings = <String, String>{};
}
if (exportExpressions == null) {
exportExpressions = <String>[];
}
if (exportExpressionAttrs == null) {
exportExpressionAttrs = <String>[];
}
if (attributeMappings == null) attributeMappings = <String, String>{};
if (exportExpressions == null) exportExpressions = <String>[];
if (exportExpressionAttrs == null) exportExpressionAttrs = <String>[];
}
}

18 changes: 4 additions & 14 deletions lib/tools/expression_extractor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ main(args) {
}
IoService ioService = new IoServiceImpl();

var packageRoots =
(args.length < 6) ? [Platform.packageRoot] : args.sublist(5);
var packageRoots = args.length < 6 ? [Platform.packageRoot] : args.sublist(5);
var sourceCrawler = new SourceCrawlerImpl(packageRoots);
var sourceMetadataExtractor = new SourceMetadataExtractor();
List<DirectiveInfo> directives =
Expand All @@ -38,17 +37,10 @@ main(args) {
var headerFile = args[2];
var footerFile = args[3];
var outputFile = args[4];
var printer;
if (outputFile == '--') {
printer = stdout;
} else {
printer = new File(outputFile).openWrite();
}
var printer = outputFile == '--' ? stdout : new File(outputFile).openWrite();

// Output the header file first.
if (headerFile != '') {
printer.write(_readFile(headerFile));
}
if (headerFile != '') printer.write(_readFile(headerFile));

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


// Output footer last.
if (footerFile != '') {
printer.write(_readFile(footerFile));
}
if (footerFile != '') printer.write(_readFile(footerFile));
}

String _readFile(String filePath) => new File(filePath).readAsStringSync();
8 changes: 2 additions & 6 deletions lib/tools/html_extractor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ class HtmlExpressionExtractor {
HtmlExpressionExtractor(this.directiveInfos) {
for (DirectiveInfo directiveInfo in directiveInfos) {
expressions.addAll(directiveInfo.expressions);
if (directiveInfo.template != null) {
parseHtml(directiveInfo.template);
}
if (directiveInfo.template != null) parseHtml(directiveInfo.template);
}
}

Expand Down Expand Up @@ -69,9 +67,7 @@ class HtmlExpressionExtractor {
visitNodes(List<Node> nodes, NodeVisitor visitor) {
for (Node node in nodes) {
visitor(node);
if (node.nodes.length > 0) {
visitNodes(node.nodes, visitor);
}
if (node.nodes.isNotEmpty) visitNodes(node.nodes, visitor);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/tools/io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ library angular.io;
typedef FsVisitor(String file);

/**
* A simple mockabe wrapper around dart:io that can be used without introducing
* A simple mockable wrapper around dart:io that can be used without introducing
* direct dependencies on dart:io.
*/
abstract class IoService {
Expand Down
2 changes: 1 addition & 1 deletion lib/tools/io_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class IoServiceImpl implements IoService {
new File(filePath).readAsStringSync();

void visitFs(String rootDir, FsVisitor visitor) {
Directory root = new Directory(rootDir);
var root = new Directory(rootDir);
if (!FileSystemEntity.isDirectorySync(rootDir)) {
throw 'Expected $rootDir to be a directory!';
}
Expand Down
30 changes: 11 additions & 19 deletions lib/tools/selector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class ContainsSelector {
}
}

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

Expand All @@ -22,17 +22,17 @@ class _SelectorPart {
final String attrName;
final String attrValue;

const _SelectorPart.fromElement(String this.element)
const _SelectorPart.fromElement(this.element)
: className = null, attrName = null, attrValue = null;

const _SelectorPart.fromClass(String this.className)
const _SelectorPart.fromClass(this.className)
: element = null, attrName = null, attrValue = null;


const _SelectorPart.fromAttribute(String this.attrName, String this.attrValue)
const _SelectorPart.fromAttribute(this.attrName, this.attrValue)
: element = null, className = null;

toString() =>
String toString() =>
element == null
? (className == null
? (attrValue == '' ? '[$attrName]' : '[$attrName=$attrValue]')
Expand Down Expand Up @@ -68,19 +68,13 @@ List<_SelectorPart> _splitCss(String selector) {
bool matchesNode(Node node, String selector) {
var match, selectorParts;
if ((match = _CONTAINS_REGEXP.firstMatch(selector)) != null) {
if (node is! Text) {
return false;
}
if (node is! Text) return false;
return new RegExp(match.group(1)).hasMatch((node as Text).text);
} else if ((match = _ATTR_CONTAINS_REGEXP.firstMatch(selector)) != null) {
if (node is! Element) {
return false;
}
if (node is! Element) return false;
var regexp = new RegExp(match.group(1));
for (String attrName in node.attributes.keys) {
if (regexp.hasMatch(node.attributes[attrName])) {
return true;
}
if (regexp.hasMatch(node.attributes[attrName])) return true;
}
return false;
} else if ((selectorParts = _splitCss(selector)) != null) {
Expand All @@ -90,9 +84,7 @@ bool matchesNode(Node node, String selector) {
bool stillGood = true;
selectorParts.forEach((_SelectorPart part) {
if (part.element != null) {
if (nodeName != part.element) {
stillGood = false;
}
if (nodeName != part.element) stillGood = false;
} else if (part.className != null) {
if (node.attributes['class'] == null ||
!node.attributes['class'].split(' ').contains(part.className)) {
Expand All @@ -116,5 +108,5 @@ bool matchesNode(Node node, String selector) {

String _matchingKey(Iterable keys, String attrName) =>
keys.firstWhere(
(key) => new RegExp('^${attrName.replaceAll('*', r'[\w\-]+')}\$').hasMatch(key.toString()),
(key) => new RegExp('^${attrName.replaceAll('*', r'[-\w]+')}\$').hasMatch(key.toString()),
orElse: () => null);
36 changes: 12 additions & 24 deletions lib/tools/source_crawler_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,19 @@ class SourceCrawlerImpl implements SourceCrawler {
SourceCrawlerImpl(this.packageRoots);

void crawl(String entryPoint, CompilationUnitVisitor visitor) {
List<String> visited = <String>[];
List<String> toVisit = <String>[];
final visited = new Set<String>();
final toVisit = new Set<String>();
if (entryPoint.startsWith(PACKAGE_PREFIX)) {
var path = resolvePackagePath(entryPoint);
if (path == null) {
throw 'Unable to resolve $entryPoint';
}
if (path == null) throw 'Unable to resolve $entryPoint';
toVisit.add(path);
} else {
toVisit.add(entryPoint);
}

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

void processImports(CompilationUnit cu, String currentDir,
String currentFile, List<String> visited,
List<String> toVisit) {
String currentFile, Set<String> visited,
Set<String> toVisit) {
cu.directives.forEach((Directive directive) {
if (directive is ImportDirective ||
directive is PartDirective ||
Expand All @@ -52,10 +51,7 @@ class SourceCrawlerImpl implements SourceCrawler {
String canonicalFile = canonicalizeImportPath(
currentDir, currentFile, import.uri.stringValue);
if (canonicalFile == null) return;
if (!visited.contains(canonicalFile) &&
!toVisit.contains(canonicalFile)) {
toVisit.add(canonicalFile);
}
if (!visited.contains(canonicalFile)) toVisit.add(canonicalFile);
}
});
}
Expand All @@ -64,12 +60,8 @@ class SourceCrawlerImpl implements SourceCrawler {
String currentFile,
String uri) {
// ignore core libraries
if (uri.startsWith('dart:')) {
return null;
}
if (uri.startsWith(PACKAGE_PREFIX)) {
return resolvePackagePath(uri);
}
if (uri.startsWith('dart:')) return null;
if (uri.startsWith(PACKAGE_PREFIX)) return resolvePackagePath(uri);
// relative import.
if (uri.startsWith('../')) {
while (uri.startsWith('../')) {
Expand All @@ -83,18 +75,14 @@ class SourceCrawlerImpl implements SourceCrawler {
String resolvePackagePath(String uri) {
for (String packageRoot in packageRoots) {
var resolvedPath = _packageUriResolver(uri, packageRoot);
if (new File(resolvedPath).existsSync()) {
return resolvedPath;
}
if (new File(resolvedPath).existsSync()) return resolvedPath;
}
return null;
}

String _packageUriResolver(String uri, String packageRoot) {
var packagePath = uri.substring(PACKAGE_PREFIX.length);
if (!packageRoot.endsWith('/')) {
packageRoot = packageRoot + '/';
}
if (!packageRoot.endsWith('/')) packageRoot = packageRoot + '/';
return packageRoot + packagePath;
}
}
Loading

1 comment on commit 6d6e9ee

@mhevery
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not merging this SHA

Please sign in to comment.