@@ -9,7 +9,9 @@ import 'package:analyzer/dart/ast/ast.dart';
99import 'package:analyzer/dart/ast/syntactic_entity.dart' ;
1010import 'package:analyzer/dart/ast/token.dart' ;
1111import 'package:analyzer/dart/element/element.dart' ;
12+ import 'package:analyzer/dart/element/element2.dart' ;
1213import 'package:analyzer/dart/element/visitor.dart' ;
14+ import 'package:analyzer/dart/element/visitor2.dart' ;
1315import 'package:analyzer/file_system/file_system.dart' ;
1416import 'package:analyzer/src/lint/linter.dart' ; // ignore: implementation_imports
1517import 'package:analyzer/src/workspace/workspace.dart' ; // ignore: implementation_imports
@@ -32,6 +34,18 @@ List<Element> getChildren(Element parent, [String? name]) {
3234 return children;
3335}
3436
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+
3549/// Return the compilation unit of a node
3650CompilationUnit ? getCompilationUnit (AstNode node) =>
3751 node.thisOrAncestorOfType <CompilationUnit >();
@@ -307,6 +321,12 @@ void visitChildren(Element element, ElementProcessor processor) {
307321 element.visitChildren (_ElementVisitorAdapter (processor));
308322}
309323
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+
310330bool _checkForSimpleGetter (MethodDeclaration getter, Expression ? expression) {
311331 if (expression is SimpleIdentifier ) {
312332 var staticElement = expression.staticElement;
@@ -418,6 +438,10 @@ bool _hasFieldOrMethod(ClassMember element, String name) =>
418438/// If `true` is returned, children of [element] will be visited.
419439typedef ElementProcessor = bool Function (Element element);
420440
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+
421445/// A [GeneralizingElementVisitor] adapter for [ElementProcessor] .
422446class _ElementVisitorAdapter extends GeneralizingElementVisitor <void > {
423447 final ElementProcessor processor;
@@ -433,6 +457,21 @@ class _ElementVisitorAdapter extends GeneralizingElementVisitor<void> {
433457 }
434458}
435459
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+
436475extension AstNodeExtension on AstNode {
437476 bool get isToStringInvocationWithArguments {
438477 var self = this ;
0 commit comments