Skip to content

Commit

Permalink
[element model] migrate flutter_utils
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
pq authored and Commit Queue committed Nov 21, 2024
1 parent b02a116 commit e19bc57
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 39 deletions.
1 change: 0 additions & 1 deletion pkg/linter/analyzer_use_new_elements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ lib/src/rules/unnecessary_overrides.dart
lib/src/rules/use_build_context_synchronously.dart
lib/src/rules/use_late_for_private_fields_and_variables.dart
lib/src/util/dart_type_utilities.dart
lib/src/util/flutter_utils.dart
test/rules/use_build_context_synchronously_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/src/dart/resolver/exit_detector.dart';
// ignore: implementation_imports
import 'package:analyzer/src/lint/constants.dart';
// ignore: implementation_imports
import 'package:analyzer/src/utilities/extensions/element.dart';
import 'package:collection/collection.dart';
import 'package:meta/meta.dart';
import 'package:pub_semver/pub_semver.dart';
Expand Down Expand Up @@ -1338,7 +1340,8 @@ extension ElementExtension on Element {

if (self is PropertyAccessorElement) {
var enclosingElement = self.enclosingElement3;
if (enclosingElement is InterfaceElement && isState(enclosingElement)) {
if (enclosingElement is InterfaceElement &&
isState(enclosingElement.asElement2)) {
// The BuildContext object is the field on Flutter's State class.
// This object can only be guarded by async gaps with a mounted
// check on the State.
Expand Down
59 changes: 22 additions & 37 deletions pkg/linter/lib/src/util/flutter_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/dart/element/nullability_suffix.dart';
import 'package:analyzer/dart/element/type.dart';
Expand Down Expand Up @@ -33,13 +32,13 @@ final Uri _uriFramework = Uri.parse(

_Flutter get _flutter => _flutterInstance;

bool hasWidgetAsAscendant(ClassElement element) =>
bool hasWidgetAsAscendant(ClassElement2 element) =>
_flutter.hasWidgetAsAscendant(element);

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

bool isExactWidget(ClassElement element) => _flutter.isExactWidget(element);
bool isExactWidget(ClassElement2 element) => _flutter.isExactWidget(element);

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

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

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

bool isState2(InterfaceElement2 element) => _flutter.isState2(element);
bool isState(InterfaceElement2 element) => _flutter.isState(element);

bool isStatefulWidget(ClassElement2? element) =>
element != null && _flutter.isStatefulWidget(element);
Expand All @@ -62,7 +59,7 @@ bool isWidgetProperty(DartType? type) {
}
if (type is InterfaceType &&
type.implementsAnyInterface(_collectionInterfaces)) {
return type.element.typeParameters.length == 1 &&
return type.element3.typeParameters2.length == 1 &&
isWidgetProperty(type.typeArguments.first);
}
return false;
Expand Down Expand Up @@ -90,18 +87,19 @@ class _Flutter {
_uriFramework = Uri.parse('$uriPrefix/src/widgets/framework.dart'),
_uriFoundation = Uri.parse('$uriPrefix/src/foundation/constants.dart');

bool hasWidgetAsAscendant(InterfaceElement? element,
[Set<InterfaceElement>? alreadySeen]) {
bool hasWidgetAsAscendant(InterfaceElement2? element,
[Set<InterfaceElement2>? alreadySeen]) {
if (element == null) return false;

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

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

var type =
element.isAugmentation ? element.augmented.thisType : element.supertype;
return hasWidgetAsAscendant(type?.element, alreadySeen);
var type = element.firstFragment.isAugmentation
? element.thisType
: element.supertype;
return hasWidgetAsAscendant(type?.element3, alreadySeen);
}

bool isBuildContext(DartType? type, {bool skipNullable = false}) {
Expand All @@ -111,58 +109,45 @@ class _Flutter {
if (skipNullable && type.nullabilitySuffix == NullabilitySuffix.question) {
return false;
}
return isExactly(type.element, _nameBuildContext, _uriFramework);
return isExactly(type.element3, _nameBuildContext, _uriFramework);
}

/// Whether [element] is exactly the element named [type], from Flutter.
bool isExactly(InterfaceElement element, String type, Uri uri) =>
element.name == type && element.source.uri == uri;

/// Whether [element] is exactly the element named [type], from Flutter.
bool isExactly2(InterfaceElement2 element, String type, Uri uri) =>
element.name3 == type &&
element.firstFragment.libraryFragment.source.uri == uri;
bool isExactly(InterfaceElement2 element, String type, Uri uri) =>
element.name3 == type && element.library2.firstFragment.source.uri == uri;

bool isExactWidget(ClassElement element) =>
bool isExactWidget(ClassElement2 element) =>
isExactly(element, _nameWidget, _uriFramework);

bool isExactWidget2(ClassElement2 element) =>
isExactly2(element, _nameWidget, _uriFramework);

bool isExactWidgetTypeContainer(DartType? type) =>
type is InterfaceType &&
isExactly(type.element, _nameContainer, _uriContainer);
isExactly(type.element3, _nameContainer, _uriContainer);

bool isExactWidgetTypeSizedBox(DartType? type) =>
type is InterfaceType &&
isExactly(type.element, _nameSizedBox, _uriBasic);
isExactly(type.element3, _nameSizedBox, _uriBasic);

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

bool isState(InterfaceElement element) =>
bool isState(InterfaceElement2 element) =>
isExactly(element, _nameState, _uriFramework) ||
element.allSupertypes
.any((type) => isExactly(type.element, _nameState, _uriFramework));

bool isState2(InterfaceElement2 element) =>
isExactly2(element, _nameState, _uriFramework) ||
element.allSupertypes
.any((type) => isExactly2(type.element3, _nameState, _uriFramework));
.any((type) => isExactly(type.element3, _nameState, _uriFramework));

bool isStatefulWidget(ClassElement2 element) =>
isExactly2(element, _nameStatefulWidget, _uriFramework) ||
isExactly(element, _nameStatefulWidget, _uriFramework) ||
element.allSupertypes.any((type) =>
isExactly(type.element, _nameStatefulWidget, _uriFramework));
isExactly(type.element3, _nameStatefulWidget, _uriFramework));

bool isWidget(InterfaceElement2 element) {
if (isExactly2(element, _nameWidget, _uriFramework)) {
if (isExactly(element, _nameWidget, _uriFramework)) {
return true;
}
for (var type in element.allSupertypes) {
if (isExactly2(type.element3, _nameWidget, _uriFramework)) {
if (isExactly(type.element3, _nameWidget, _uriFramework)) {
return true;
}
}
Expand Down

0 comments on commit e19bc57

Please sign in to comment.