Skip to content

Commit 6ce015e

Browse files
scheglovCommit Queue
authored andcommitted
Fine. Update PreferIsNotEmpty to use getGetter() instead of requesting all children.
Replace child enumeration with a targeted member lookup in the `prefer_is_not_empty` rule. Instead of requesting all children to check for `isNotEmpty`, the rule now calls `InterfaceElement.getGetter()` on the declaring interface of `isEmpty`. This avoids recording an "opaque API use" requirement and reduces unnecessary dependency tracking and overhead. Additionally, remove generic child-walking utilities from `ast.dart` that were only used for this purpose. Changes: - In `prefer_is_not_empty.dart`, use `InterfaceElement.getGetter('isNotEmpty')` instead of `getChildren`. - Drop `getChildren`, `_visitChildren`, `ElementProcessor`, and the visitor adapter from `ast.dart`, along with their imports. - Remove the unused `../ast.dart` import from the rule and add the precise `element.dart` import. No functional changes to the lint’s intent: it still recommends `isNotEmpty` for `Iterable` and `Map` types, but does so without walking children, improving precision and performance of fine-grained tracking. Found with https://dart-review.googlesource.com/c/sdk/+/446480 Change-Id: I1e0e9991aeba924b672d1216bed2669215ab20cd Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/446580 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent 9b3df8f commit 6ce015e

File tree

2 files changed

+3
-41
lines changed

2 files changed

+3
-41
lines changed

pkg/linter/lib/src/ast.dart

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,10 @@ import 'package:analyzer/dart/ast/ast.dart';
1010
import 'package:analyzer/dart/ast/syntactic_entity.dart';
1111
import 'package:analyzer/dart/ast/token.dart';
1212
import 'package:analyzer/dart/element/element.dart';
13-
import 'package:analyzer/dart/element/visitor2.dart';
1413
import 'package:analyzer/file_system/file_system.dart';
1514
import 'package:analyzer/workspace/workspace.dart';
1615
import 'package:path/path.dart' as path;
1716

18-
/// Returns direct children of [parent].
19-
List<Element> getChildren(Element parent, [String? name]) {
20-
var children = <Element>[];
21-
_visitChildren(parent, (Element element) {
22-
if (name == null || element.displayName == name) {
23-
children.add(element);
24-
}
25-
return false;
26-
});
27-
return children;
28-
}
29-
3017
/// Return the compilation unit of a node
3118
CompilationUnit? getCompilationUnit(AstNode node) =>
3219
node.thisOrAncestorOfType<CompilationUnit>();
@@ -343,31 +330,6 @@ bool _hasFieldOrMethod(ClassMember element, String name) =>
343330
(element is MethodDeclaration && element.name.lexeme == name) ||
344331
(element is FieldDeclaration && getFieldName(element, name) != null);
345332

346-
/// Uses [processor] to visit all of the children of [element].
347-
/// If [processor] returns `true`, then children of a child are visited too.
348-
void _visitChildren(Element element, ElementProcessor processor) {
349-
element.visitChildren(_ElementVisitorAdapter(processor));
350-
}
351-
352-
/// An [Element] processor function type.
353-
/// If `true` is returned, children of [element] will be visited.
354-
typedef ElementProcessor = bool Function(Element element);
355-
356-
/// A [GeneralizingElementVisitor2] adapter for [ElementProcessor].
357-
class _ElementVisitorAdapter extends GeneralizingElementVisitor2<void> {
358-
final ElementProcessor processor;
359-
360-
_ElementVisitorAdapter(this.processor);
361-
362-
@override
363-
void visitElement(Element element) {
364-
var visitChildren = processor(element);
365-
if (visitChildren) {
366-
element.visitChildren(this);
367-
}
368-
}
369-
}
370-
371333
extension AstNodeExtension on AstNode {
372334
bool get isToStringInvocationWithArguments {
373335
var self = this;

pkg/linter/lib/src/rules/prefer_is_not_empty.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import 'package:analyzer/dart/ast/ast.dart'
88
show PrefixExpression, PrefixedIdentifier, PropertyAccess, SimpleIdentifier;
99
import 'package:analyzer/dart/ast/token.dart';
1010
import 'package:analyzer/dart/ast/visitor.dart';
11+
import 'package:analyzer/dart/element/element.dart';
1112
import 'package:analyzer/error/error.dart';
1213

1314
import '../analyzer.dart';
14-
import '../ast.dart';
1515

1616
const _desc = r'Use `isNotEmpty` for `Iterable`s and `Map`s.';
1717

@@ -66,8 +66,8 @@ class _Visitor extends SimpleAstVisitor<void> {
6666

6767
// Element should also support "isNotEmpty".
6868
var propertyTarget = propertyElement.enclosingElement;
69-
if (propertyTarget == null ||
70-
getChildren(propertyTarget, 'isNotEmpty').isEmpty) {
69+
if (propertyTarget is! InterfaceElement ||
70+
propertyTarget.getGetter('isNotEmpty') == null) {
7171
return;
7272
}
7373

0 commit comments

Comments
 (0)