Skip to content

Commit e19bc57

Browse files
pqCommit Queue
authored andcommitted
[element model] migrate flutter_utils
Bug: https://github.com/dart-lang/linter/issues/5099 Change-Id: I5d37fb4923025d15b8ff67c936355c46957d6a76 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/395042 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Phil Quitslund <[email protected]>
1 parent b02a116 commit e19bc57

File tree

3 files changed

+26
-39
lines changed

3 files changed

+26
-39
lines changed

pkg/linter/analyzer_use_new_elements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,4 @@ lib/src/rules/unnecessary_overrides.dart
1010
lib/src/rules/use_build_context_synchronously.dart
1111
lib/src/rules/use_late_for_private_fields_and_variables.dart
1212
lib/src/util/dart_type_utilities.dart
13-
lib/src/util/flutter_utils.dart
1413
test/rules/use_build_context_synchronously_test.dart

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import 'package:analyzer/dart/element/element.dart';
1010
import 'package:analyzer/src/dart/resolver/exit_detector.dart';
1111
// ignore: implementation_imports
1212
import 'package:analyzer/src/lint/constants.dart';
13+
// ignore: implementation_imports
14+
import 'package:analyzer/src/utilities/extensions/element.dart';
1315
import 'package:collection/collection.dart';
1416
import 'package:meta/meta.dart';
1517
import 'package:pub_semver/pub_semver.dart';
@@ -1338,7 +1340,8 @@ extension ElementExtension on Element {
13381340

13391341
if (self is PropertyAccessorElement) {
13401342
var enclosingElement = self.enclosingElement3;
1341-
if (enclosingElement is InterfaceElement && isState(enclosingElement)) {
1343+
if (enclosingElement is InterfaceElement &&
1344+
isState(enclosingElement.asElement2)) {
13421345
// The BuildContext object is the field on Flutter's State class.
13431346
// This object can only be guarded by async gaps with a mounted
13441347
// check on the State.

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

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import 'package:analyzer/dart/element/element.dart';
65
import 'package:analyzer/dart/element/element2.dart';
76
import 'package:analyzer/dart/element/nullability_suffix.dart';
87
import 'package:analyzer/dart/element/type.dart';
@@ -33,13 +32,13 @@ final Uri _uriFramework = Uri.parse(
3332

3433
_Flutter get _flutter => _flutterInstance;
3534

36-
bool hasWidgetAsAscendant(ClassElement element) =>
35+
bool hasWidgetAsAscendant(ClassElement2 element) =>
3736
_flutter.hasWidgetAsAscendant(element);
3837

3938
bool isBuildContext(DartType? type, {bool skipNullable = false}) =>
4039
_flutter.isBuildContext(type, skipNullable: skipNullable);
4140

42-
bool isExactWidget(ClassElement element) => _flutter.isExactWidget(element);
41+
bool isExactWidget(ClassElement2 element) => _flutter.isExactWidget(element);
4342

4443
bool isExactWidgetTypeContainer(DartType? type) =>
4544
_flutter.isExactWidgetTypeContainer(type);
@@ -49,9 +48,7 @@ bool isExactWidgetTypeSizedBox(DartType? type) =>
4948

5049
bool isKDebugMode(Element2? element) => _flutter.isKDebugMode(element);
5150

52-
bool isState(InterfaceElement element) => _flutter.isState(element);
53-
54-
bool isState2(InterfaceElement2 element) => _flutter.isState2(element);
51+
bool isState(InterfaceElement2 element) => _flutter.isState(element);
5552

5653
bool isStatefulWidget(ClassElement2? element) =>
5754
element != null && _flutter.isStatefulWidget(element);
@@ -62,7 +59,7 @@ bool isWidgetProperty(DartType? type) {
6259
}
6360
if (type is InterfaceType &&
6461
type.implementsAnyInterface(_collectionInterfaces)) {
65-
return type.element.typeParameters.length == 1 &&
62+
return type.element3.typeParameters2.length == 1 &&
6663
isWidgetProperty(type.typeArguments.first);
6764
}
6865
return false;
@@ -90,18 +87,19 @@ class _Flutter {
9087
_uriFramework = Uri.parse('$uriPrefix/src/widgets/framework.dart'),
9188
_uriFoundation = Uri.parse('$uriPrefix/src/foundation/constants.dart');
9289

93-
bool hasWidgetAsAscendant(InterfaceElement? element,
94-
[Set<InterfaceElement>? alreadySeen]) {
90+
bool hasWidgetAsAscendant(InterfaceElement2? element,
91+
[Set<InterfaceElement2>? alreadySeen]) {
9592
if (element == null) return false;
9693

9794
if (isExactly(element, _nameWidget, _uriFramework)) return true;
9895

9996
alreadySeen ??= {};
10097
if (!alreadySeen.add(element)) return false;
10198

102-
var type =
103-
element.isAugmentation ? element.augmented.thisType : element.supertype;
104-
return hasWidgetAsAscendant(type?.element, alreadySeen);
99+
var type = element.firstFragment.isAugmentation
100+
? element.thisType
101+
: element.supertype;
102+
return hasWidgetAsAscendant(type?.element3, alreadySeen);
105103
}
106104

107105
bool isBuildContext(DartType? type, {bool skipNullable = false}) {
@@ -111,58 +109,45 @@ class _Flutter {
111109
if (skipNullable && type.nullabilitySuffix == NullabilitySuffix.question) {
112110
return false;
113111
}
114-
return isExactly(type.element, _nameBuildContext, _uriFramework);
112+
return isExactly(type.element3, _nameBuildContext, _uriFramework);
115113
}
116114

117115
/// Whether [element] is exactly the element named [type], from Flutter.
118-
bool isExactly(InterfaceElement element, String type, Uri uri) =>
119-
element.name == type && element.source.uri == uri;
120-
121-
/// Whether [element] is exactly the element named [type], from Flutter.
122-
bool isExactly2(InterfaceElement2 element, String type, Uri uri) =>
123-
element.name3 == type &&
124-
element.firstFragment.libraryFragment.source.uri == uri;
116+
bool isExactly(InterfaceElement2 element, String type, Uri uri) =>
117+
element.name3 == type && element.library2.firstFragment.source.uri == uri;
125118

126-
bool isExactWidget(ClassElement element) =>
119+
bool isExactWidget(ClassElement2 element) =>
127120
isExactly(element, _nameWidget, _uriFramework);
128121

129-
bool isExactWidget2(ClassElement2 element) =>
130-
isExactly2(element, _nameWidget, _uriFramework);
131-
132122
bool isExactWidgetTypeContainer(DartType? type) =>
133123
type is InterfaceType &&
134-
isExactly(type.element, _nameContainer, _uriContainer);
124+
isExactly(type.element3, _nameContainer, _uriContainer);
135125

136126
bool isExactWidgetTypeSizedBox(DartType? type) =>
137127
type is InterfaceType &&
138-
isExactly(type.element, _nameSizedBox, _uriBasic);
128+
isExactly(type.element3, _nameSizedBox, _uriBasic);
139129

140130
bool isKDebugMode(Element2? element) =>
141131
element != null &&
142132
element.name3 == 'kDebugMode' &&
143133
element.library2?.uri == _uriFoundation;
144134

145-
bool isState(InterfaceElement element) =>
135+
bool isState(InterfaceElement2 element) =>
146136
isExactly(element, _nameState, _uriFramework) ||
147137
element.allSupertypes
148-
.any((type) => isExactly(type.element, _nameState, _uriFramework));
149-
150-
bool isState2(InterfaceElement2 element) =>
151-
isExactly2(element, _nameState, _uriFramework) ||
152-
element.allSupertypes
153-
.any((type) => isExactly2(type.element3, _nameState, _uriFramework));
138+
.any((type) => isExactly(type.element3, _nameState, _uriFramework));
154139

155140
bool isStatefulWidget(ClassElement2 element) =>
156-
isExactly2(element, _nameStatefulWidget, _uriFramework) ||
141+
isExactly(element, _nameStatefulWidget, _uriFramework) ||
157142
element.allSupertypes.any((type) =>
158-
isExactly(type.element, _nameStatefulWidget, _uriFramework));
143+
isExactly(type.element3, _nameStatefulWidget, _uriFramework));
159144

160145
bool isWidget(InterfaceElement2 element) {
161-
if (isExactly2(element, _nameWidget, _uriFramework)) {
146+
if (isExactly(element, _nameWidget, _uriFramework)) {
162147
return true;
163148
}
164149
for (var type in element.allSupertypes) {
165-
if (isExactly2(type.element3, _nameWidget, _uriFramework)) {
150+
if (isExactly(type.element3, _nameWidget, _uriFramework)) {
166151
return true;
167152
}
168153
}

0 commit comments

Comments
 (0)