Skip to content

Commit 5e79752

Browse files
pqCommit Queue
authored andcommitted
[element model] migrate specify_nonobvious_property_types
Bug: https://github.com/dart-lang/linter/issues/5099 Change-Id: I5bb2753f96527f80a000c08f923ac78c1c03e89d Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/391680 Commit-Queue: Konstantin Shcheglov <[email protected]> Commit-Queue: Phil Quitslund <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]> Auto-Submit: Phil Quitslund <[email protected]>
1 parent cb3de27 commit 5e79752

File tree

4 files changed

+44
-10
lines changed

4 files changed

+44
-10
lines changed

pkg/analyzer/lib/src/dart/element/type.dart

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,13 @@ class InterfaceTypeImpl extends TypeImpl implements InterfaceType {
569569
@override
570570
List<GetterElement> get getters => accessors
571571
.where((accessor) => accessor.isGetter)
572-
.map((fragment) => (fragment as GetterFragment).element as GetterElement)
572+
.map((fragment) => switch (fragment) {
573+
GetterFragment(:var element) => element as GetterElement,
574+
GetterMember() => fragment,
575+
_ => throw StateError(
576+
'unexpected fragment type: ${fragment.runtimeType}',
577+
)
578+
})
573579
.toList();
574580

575581
@override
@@ -718,7 +724,13 @@ class InterfaceTypeImpl extends TypeImpl implements InterfaceType {
718724
@override
719725
List<SetterElement> get setters => accessors
720726
.where((accessor) => accessor.isSetter)
721-
.map((fragment) => (fragment as SetterFragment).element as SetterElement)
727+
.map((fragment) => switch (fragment) {
728+
SetterFragment(:var element) => element as SetterElement,
729+
SetterMember() => fragment,
730+
_ => throw StateError(
731+
'unexpected fragment type: ${fragment.runtimeType}',
732+
)
733+
})
722734
.toList();
723735

724736
@override

pkg/linter/analyzer_use_new_elements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ lib/src/rules/sort_child_properties_last.dart
158158
lib/src/rules/sort_constructors_first.dart
159159
lib/src/rules/sort_unnamed_constructors_first.dart
160160
lib/src/rules/specify_nonobvious_local_variable_types.dart
161+
lib/src/rules/specify_nonobvious_property_types.dart
161162
lib/src/rules/super_goes_last.dart
162163
lib/src/rules/test_types_in_equals.dart
163164
lib/src/rules/throw_in_finally.dart
@@ -196,6 +197,7 @@ lib/src/rules/use_if_null_to_convert_nulls_to_bools.dart
196197
lib/src/rules/use_is_even_rather_than_modulo.dart
197198
lib/src/rules/use_named_constants.dart
198199
lib/src/rules/use_raw_strings.dart
200+
lib/src/rules/use_rethrow_when_possible.dart
199201
lib/src/rules/use_string_in_part_of_directives.dart
200202
lib/src/rules/use_test_throws_matchers.dart
201203
lib/src/rules/use_to_and_as_if_applicable.dart

pkg/linter/lib/src/extensions.dart

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

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

@@ -609,6 +619,12 @@ extension InterfaceTypeExtension on InterfaceType {
609619
searchSupertypes(this, {}, interfaceTypes);
610620
return interfaceTypes;
611621
}
622+
623+
GetterElement? getGetter2(String name) =>
624+
getters.firstWhereOrNull((s) => s.name3 == name);
625+
626+
SetterElement? getSetter2(String name) =>
627+
setters.firstWhereOrNull((s) => s.canonicalName == name);
612628
}
613629

614630
extension LibraryElementExtension2 on LibraryElement2? {

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
import 'package:analyzer/dart/ast/ast.dart';
66
import 'package:analyzer/dart/ast/visitor.dart';
7-
import 'package:analyzer/dart/element/element.dart';
7+
import 'package:analyzer/dart/element/element2.dart';
88

99
import '../analyzer.dart';
10+
import '../extensions.dart';
1011
import '../util/obvious_types.dart';
1112

1213
const _desc = r'Specify non-obvious type annotations for local variables.';
@@ -63,20 +64,23 @@ class _Visitor extends SimpleAstVisitor<void> {
6364
bool ignoreThisVariable = false;
6465
AstNode? owningDeclaration = node;
6566
while (owningDeclaration != null) {
66-
InterfaceElement? owningElement = switch (owningDeclaration) {
67-
ClassDeclaration(:var declaredElement) => declaredElement,
68-
MixinDeclaration(:var declaredElement) => declaredElement,
69-
EnumDeclaration(:var declaredElement) => declaredElement,
70-
ExtensionTypeDeclaration(:var declaredElement) => declaredElement,
67+
InterfaceElement2? owningElement = switch (owningDeclaration) {
68+
ClassDeclaration(:var declaredFragment?) =>
69+
declaredFragment.element,
70+
MixinDeclaration(:var declaredFragment?) =>
71+
declaredFragment.element,
72+
EnumDeclaration(:var declaredFragment?) => declaredFragment.element,
73+
ExtensionTypeDeclaration(:var declaredFragment?) =>
74+
declaredFragment.element,
7175
_ => null,
7276
};
7377
if (owningElement != null) {
7478
var variableName = child.name.lexeme;
7579
for (var superInterface in owningElement.allSupertypes) {
76-
if (superInterface.getGetter(variableName) != null) {
80+
if (superInterface.getGetter2(variableName) != null) {
7781
ignoreThisVariable = true;
7882
}
79-
if (superInterface.getSetter(variableName) != null) {
83+
if (superInterface.getSetter2(variableName) != null) {
8084
ignoreThisVariable = true;
8185
}
8286
}

0 commit comments

Comments
 (0)