Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(283)

Unified Diff: lib/src/ast.dart

Issue 1419373009: Lint to prefer x.isNotEmpty vs. !x.isEmpty (#143). (Closed) Base URL: https://github.com/dart-lang/linter.git@master
Patch Set: doc fix. Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | lib/src/linter.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/src/ast.dart
diff --git a/lib/src/ast.dart b/lib/src/ast.dart
index 1a79dc17a6c55425848ff23a83bf4dd86c4cadbb..5bd3bdf9392224cbc0b1f79b8d2529964896b288 100644
--- a/lib/src/ast.dart
+++ b/lib/src/ast.dart
@@ -31,18 +31,30 @@ import 'package:analyzer/src/generated/ast.dart'
TypeParameter,
VariableDeclaration;
import 'package:analyzer/src/generated/element.dart'
- show Element, ParameterElement, PropertyAccessorElement;
+ show
+ Element,
+ GeneralizingElementVisitor,
+ ParameterElement,
+ PropertyAccessorElement;
import 'package:analyzer/src/generated/scanner.dart'
show Keyword, KeywordToken, KeywordTokenWithComment, Token;
import 'package:linter/src/util.dart';
+/// Returns direct children of [parent].
+List<Element> getChildren(Element parent, [String name]) {
+ List<Element> children = <Element>[];
+ visitChildren(parent, (Element element) {
+ if (name == null || element.displayName == name) {
+ children.add(element);
+ }
+ });
+ return children;
+}
+
/// Returns the most specific AST node appropriate for associating errors.
AstNode getNodeToAnnotate(Declaration node) {
AstNode mostSpecific = _getNodeToAnnotate(node);
- if (mostSpecific != null) {
- return mostSpecific;
- }
- return node;
+ return mostSpecific ?? node;
}
/// Returns `true` if the keyword associated with this token is `final` or
@@ -131,6 +143,12 @@ bool isValidDartIdentifier(String id) => !isKeyWord(id) && isIdentifier(id);
/// Returns `true` if the keyword associated with this token is `var`.
bool isVar(Token token) => isKeyword(token, Keyword.VAR);
+/// Uses [processor] to visit all of the children of [element].
+/// If [processor] returns `true`, then children of a child are visited too.
+void visitChildren(Element element, ElementProcessor processor) {
+ element.visitChildren(new _ElementVisitorAdapter(processor));
+}
+
bool _checkForSimpleGetter(MethodDeclaration getter, Expression expression) {
if (expression is SimpleIdentifier) {
var staticElement = expression.staticElement;
@@ -224,3 +242,21 @@ AstNode _getNodeToAnnotate(Declaration node) {
}
return null;
}
+
+/// An [Element] processor function type.
+/// If `true` is returned, children of [element] will be visited.
+typedef bool ElementProcessor(Element element);
+
+/// A [GeneralizingElementVisitor] adapter for [ElementProcessor].
+class _ElementVisitorAdapter extends GeneralizingElementVisitor {
+ final ElementProcessor processor;
+ _ElementVisitorAdapter(this.processor);
+
+ @override
+ void visitElement(Element element) {
+ bool visitChildren = processor(element);
+ if (visitChildren == true) {
+ element.visitChildren(this);
+ }
+ }
+}
« no previous file with comments | « no previous file | lib/src/linter.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698