@@ -9,7 +9,9 @@ import 'package:analyzer/dart/ast/ast.dart';
9
9
import 'package:analyzer/dart/ast/syntactic_entity.dart' ;
10
10
import 'package:analyzer/dart/ast/token.dart' ;
11
11
import 'package:analyzer/dart/element/element.dart' ;
12
+ import 'package:analyzer/dart/element/element2.dart' ;
12
13
import 'package:analyzer/dart/element/visitor.dart' ;
14
+ import 'package:analyzer/dart/element/visitor2.dart' ;
13
15
import 'package:analyzer/file_system/file_system.dart' ;
14
16
import 'package:analyzer/src/lint/linter.dart' ; // ignore: implementation_imports
15
17
import 'package:analyzer/src/workspace/workspace.dart' ; // ignore: implementation_imports
@@ -32,6 +34,18 @@ List<Element> getChildren(Element parent, [String? name]) {
32
34
return children;
33
35
}
34
36
37
+ /// Returns direct children of [parent] .
38
+ List <Element2 > getChildren2 (Element2 parent, [String ? name]) {
39
+ var children = < Element2 > [];
40
+ visitChildren2 (parent, (Element2 element) {
41
+ if (name == null || element.displayName == name) {
42
+ children.add (element);
43
+ }
44
+ return false ;
45
+ });
46
+ return children;
47
+ }
48
+
35
49
/// Return the compilation unit of a node
36
50
CompilationUnit ? getCompilationUnit (AstNode node) =>
37
51
node.thisOrAncestorOfType <CompilationUnit >();
@@ -307,6 +321,12 @@ void visitChildren(Element element, ElementProcessor processor) {
307
321
element.visitChildren (_ElementVisitorAdapter (processor));
308
322
}
309
323
324
+ /// Uses [processor] to visit all of the children of [element] .
325
+ /// If [processor] returns `true` , then children of a child are visited too.
326
+ void visitChildren2 (Element2 element, ElementProcessor2 processor) {
327
+ element.visitChildren2 (_ElementVisitorAdapter2 (processor));
328
+ }
329
+
310
330
bool _checkForSimpleGetter (MethodDeclaration getter, Expression ? expression) {
311
331
if (expression is SimpleIdentifier ) {
312
332
var staticElement = expression.staticElement;
@@ -418,6 +438,10 @@ bool _hasFieldOrMethod(ClassMember element, String name) =>
418
438
/// If `true` is returned, children of [element] will be visited.
419
439
typedef ElementProcessor = bool Function (Element element);
420
440
441
+ /// An [Element] processor function type.
442
+ /// If `true` is returned, children of [element] will be visited.
443
+ typedef ElementProcessor2 = bool Function (Element2 element);
444
+
421
445
/// A [GeneralizingElementVisitor] adapter for [ElementProcessor] .
422
446
class _ElementVisitorAdapter extends GeneralizingElementVisitor <void > {
423
447
final ElementProcessor processor;
@@ -433,6 +457,21 @@ class _ElementVisitorAdapter extends GeneralizingElementVisitor<void> {
433
457
}
434
458
}
435
459
460
+ /// A [GeneralizingElementVisitor] adapter for [ElementProcessor] .
461
+ class _ElementVisitorAdapter2 extends GeneralizingElementVisitor2 <void > {
462
+ final ElementProcessor2 processor;
463
+
464
+ _ElementVisitorAdapter2 (this .processor);
465
+
466
+ @override
467
+ void visitElement (Element2 element) {
468
+ var visitChildren = processor (element);
469
+ if (visitChildren) {
470
+ element.visitChildren2 (this );
471
+ }
472
+ }
473
+ }
474
+
436
475
extension AstNodeExtension on AstNode {
437
476
bool get isToStringInvocationWithArguments {
438
477
var self = this ;
0 commit comments