Skip to content

Commit a8b8d2f

Browse files
pqCommit Queue
authored andcommitted
[element model] utility method migration/cleanup
Some prep work for `use_build_context_synchronously` and some dead code removal. Change-Id: I36dd07adc3b8e58eb4b858e891c146bc75f53703 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/392080 Reviewed-by: Brian Wilkerson <[email protected]> Auto-Submit: Phil Quitslund <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]>
1 parent 6cd60f8 commit a8b8d2f

File tree

2 files changed

+25
-37
lines changed

2 files changed

+25
-37
lines changed

pkg/linter/lib/src/extensions.dart

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,6 @@ class EnumLikeClassDescription {
2727
Map<DartObject, Set<FieldElement2>> get enumConstants => {..._enumConstants};
2828
}
2929

30-
extension on SetterElement {
31-
/// Return name in a format suitable for string comparison.
32-
String? get canonicalName {
33-
var name = name3;
34-
if (name == null) return null;
35-
// TODO(pq): remove when `name3` consistently does not include a trailing `=`.
36-
return name.endsWith('=') ? name.substring(0, name.length - 1) : name;
37-
}
38-
}
39-
4030
extension AstNodeExtension on AstNode {
4131
Iterable<AstNode> get childNodes => childEntities.whereType<AstNode>();
4232

@@ -289,20 +279,7 @@ extension ClassMemberListExtension on List<ClassMember> {
289279
.firstWhereOrNull((node) => node.name.lexeme == name);
290280
}
291281

292-
extension ConstructorElementExtension on ConstructorElement {
293-
/// Returns whether `this` is the same element as the [className] constructor
294-
/// named [constructorName] declared in [uri].
295-
bool isSameAs({
296-
required String uri,
297-
required String className,
298-
required String constructorName,
299-
}) =>
300-
library.name == uri &&
301-
enclosingElement3.name == className &&
302-
name == constructorName;
303-
}
304-
305-
extension ConstructorElementExtension2 on ConstructorElement2 {
282+
extension ConstructorElementExtension on ConstructorElement2 {
306283
/// Returns whether `this` is the same element as the [className] constructor
307284
/// named [constructorName] declared in [uri].
308285
bool isSameAs({
@@ -581,13 +558,6 @@ extension InhertanceManager3Extension on InheritanceManager3 {
581558
}
582559
}
583560

584-
extension InterfaceElementExtension on InterfaceElement {
585-
/// Returns whether this element is exactly [otherName] declared in
586-
/// [otherLibrary].
587-
bool isClass(String otherName, String otherLibrary) =>
588-
name == otherName && library.name == otherLibrary;
589-
}
590-
591561
extension InterfaceTypeExtension on InterfaceType {
592562
/// Returns the collection of all interfaces that this type implements,
593563
/// including itself.
@@ -614,8 +584,9 @@ extension InterfaceTypeExtension on InterfaceType {
614584
return interfaceTypes;
615585
}
616586

617-
GetterElement? getGetter2(String name) =>
618-
getters.firstWhereOrNull((s) => s.name3 == name);
587+
GetterElement? getGetter2(String name, {LibraryElement2? library}) =>
588+
getters.firstWhereOrNull((s) =>
589+
s.name3 == name && (library == null || (s.library2 == library)));
619590

620591
SetterElement? getSetter2(String name) =>
621592
setters.firstWhereOrNull((s) => s.canonicalName == name);
@@ -730,6 +701,16 @@ extension MethodDeclarationExtension on MethodDeclaration {
730701
}
731702
}
732703

704+
extension SetterElementExtension on SetterElement {
705+
/// Return name in a format suitable for string comparison.
706+
String? get canonicalName {
707+
var name = name3;
708+
if (name == null) return null;
709+
// TODO(pq): remove when `name3` consistently does not include a trailing `=`.
710+
return name.endsWith('=') ? name.substring(0, name.length - 1) : name;
711+
}
712+
}
713+
733714
extension StringExtension on String {
734715
String toAbsoluteNormalizedPath() {
735716
var pathContext = PhysicalResourceProvider.INSTANCE.pathContext;

pkg/linter/lib/src/util/flutter_utils.dart

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ bool isKDebugMode(Element2? element) => _flutter.isKDebugMode(element);
4040

4141
bool isState(InterfaceElement element) => _flutter.isState(element);
4242

43+
bool isState2(InterfaceElement2 element) => _flutter.isState2(element);
44+
4345
bool isStatefulWidget(ClassElement2? element) =>
4446
element != null && _flutter.isStatefulWidget(element);
4547

@@ -138,23 +140,28 @@ class _Flutter {
138140
element.allSupertypes
139141
.any((type) => isExactly(type.element, _nameState, _uriFramework));
140142

143+
bool isState2(InterfaceElement2 element) =>
144+
isExactly2(element, _nameState, _uriFramework) ||
145+
element.allSupertypes
146+
.any((type) => isExactly2(type.element3, _nameState, _uriFramework));
147+
141148
bool isStatefulWidget(ClassElement2 element) =>
142149
isExactly2(element, _nameStatefulWidget, _uriFramework) ||
143150
element.allSupertypes.any((type) =>
144151
isExactly(type.element, _nameStatefulWidget, _uriFramework));
145152

146-
bool isWidget(InterfaceElement element) {
147-
if (isExactly(element, _nameWidget, _uriFramework)) {
153+
bool isWidget(InterfaceElement2 element) {
154+
if (isExactly2(element, _nameWidget, _uriFramework)) {
148155
return true;
149156
}
150157
for (var type in element.allSupertypes) {
151-
if (isExactly(type.element, _nameWidget, _uriFramework)) {
158+
if (isExactly2(type.element3, _nameWidget, _uriFramework)) {
152159
return true;
153160
}
154161
}
155162
return false;
156163
}
157164

158165
bool isWidgetType(DartType? type) =>
159-
type is InterfaceType && isWidget(type.element);
166+
type is InterfaceType && isWidget(type.element3);
160167
}

0 commit comments

Comments
 (0)