Skip to content

Commit 9da1eaa

Browse files
pqCommit Queue
authored andcommitted
[element model] migrate exhaustive_cases, no_default_cases
Bug: https://github.com/dart-lang/linter/issues/5099 Change-Id: I5f5440d4aa4f4577136b2f015dc3e92e0dd67b4f Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/390800 Auto-Submit: Phil Quitslund <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent e3730b8 commit 9da1eaa

File tree

3 files changed

+42
-43
lines changed

3 files changed

+42
-43
lines changed

pkg/linter/lib/src/extensions.dart

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ import 'analyzer.dart';
2121
import 'util/dart_type_utilities.dart';
2222

2323
class EnumLikeClassDescription {
24-
final Map<DartObject, Set<FieldElement>> _enumConstants;
24+
final Map<DartObject, Set<FieldElement2>> _enumConstants;
2525
EnumLikeClassDescription(this._enumConstants);
2626

2727
/// Returns a fresh map of the class's enum-like constant values.
28-
Map<DartObject, Set<FieldElement>> get enumConstants => {..._enumConstants};
28+
Map<DartObject, Set<FieldElement2>> get enumConstants => {..._enumConstants};
2929
}
3030

3131
extension AstNodeExtension on AstNode {
@@ -161,9 +161,28 @@ extension ClassElementExtension on ClassElement {
161161
/// Get all mixins, including merged augmentations.
162162
List<InterfaceType> get allMixins => augmented.mixins;
163163

164+
/// Returns whether this class is exactly [otherName] declared in
165+
/// [otherLibrary].
166+
bool isClass(String otherName, String otherLibrary) =>
167+
name == otherName && library.name == otherLibrary;
168+
}
169+
170+
extension ClassElementExtension2 on ClassElement2 {
171+
bool get hasImmutableAnnotation {
172+
var inheritedAndSelfElements = <InterfaceElement2>[
173+
...allSupertypes.map((t) => t.element3),
174+
this,
175+
];
176+
177+
return inheritedAndSelfElements.any((e) => e.metadata2.hasImmutable);
178+
179+
// TODO(pq): update when implemented or replace w/ a better has{*} call
180+
// https://github.com/dart-lang/linter/issues/4939
181+
//return inheritedAndSelfElements.any((e) => e.augmented.metadata.any((e) => e.isImmutable));
182+
}
183+
164184
bool get hasSubclassInDefiningCompilationUnit {
165-
var compilationUnit = library.definingCompilationUnit;
166-
for (var cls in compilationUnit.classes) {
185+
for (var cls in library2.classes) {
167186
InterfaceType? classType = cls.thisType;
168187
do {
169188
classType = classType?.superclass;
@@ -201,7 +220,7 @@ extension ClassElementExtension on ClassElement {
201220
}
202221

203222
// With only private non-factory constructors.
204-
for (var constructor in constructors) {
223+
for (var constructor in constructors2) {
205224
if (!constructor.isPrivate || constructor.isFactory) {
206225
return null;
207226
}
@@ -211,8 +230,8 @@ extension ClassElementExtension on ClassElement {
211230

212231
// And 2 or more static const fields whose type is the enclosing class.
213232
var enumConstantCount = 0;
214-
var enumConstants = <DartObject, Set<FieldElement>>{};
215-
for (var field in fields) {
233+
var enumConstants = <DartObject, Set<FieldElement2>>{};
234+
for (var field in fields2) {
216235
// Ensure static const.
217236
if (field.isSynthetic || !field.isConst || !field.isStatic) {
218237
continue;
@@ -238,29 +257,9 @@ extension ClassElementExtension on ClassElement {
238257
return EnumLikeClassDescription(enumConstants);
239258
}
240259

241-
/// Returns whether this class is exactly [otherName] declared in
242-
/// [otherLibrary].
243-
bool isClass(String otherName, String otherLibrary) =>
244-
name == otherName && library.name == otherLibrary;
245-
246260
bool isEnumLikeClass() => asEnumLikeClass() != null;
247261
}
248262

249-
extension ClassElementExtension2 on ClassElement2 {
250-
bool get hasImmutableAnnotation {
251-
var inheritedAndSelfElements = <InterfaceElement2>[
252-
...allSupertypes.map((t) => t.element3),
253-
this,
254-
];
255-
256-
return inheritedAndSelfElements.any((e) => e.metadata2.hasImmutable);
257-
258-
// TODO(pq): update when implemented or replace w/ a better has{*} call
259-
// https://github.com/dart-lang/linter/issues/4939
260-
//return inheritedAndSelfElements.any((e) => e.augmented.metadata.any((e) => e.isImmutable));
261-
}
262-
}
263-
264263
extension ClassMemberListExtension on List<ClassMember> {
265264
MethodDeclaration? getMethod(String name) => whereType<MethodDeclaration>()
266265
.firstWhereOrNull((node) => node.name.lexeme == name);

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
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
import 'package:analyzer/dart/element/type.dart';
99

1010
import '../analyzer.dart';
@@ -39,9 +39,9 @@ class _Visitor extends SimpleAstVisitor<void> {
3939
void visitSwitchStatement(SwitchStatement statement) {
4040
var expressionType = statement.expression.staticType;
4141
if (expressionType is InterfaceType) {
42-
var interfaceElement = expressionType.element;
42+
var interfaceElement = expressionType.element3;
4343
// Handled in analyzer.
44-
if (interfaceElement is! ClassElement) {
44+
if (interfaceElement is! ClassElement2) {
4545
return;
4646
}
4747
var enumDescription = interfaceElement.asEnumLikeClass();
@@ -61,13 +61,13 @@ class _Visitor extends SimpleAstVisitor<void> {
6161
expression = member.expression.unParenthesized;
6262
}
6363
if (expression is Identifier) {
64-
var variable = expression.staticElement.variableElement;
65-
if (variable is VariableElement) {
64+
var variable = expression.element.variableElement;
65+
if (variable is VariableElement2) {
6666
enumConstants.remove(variable.computeConstantValue());
6767
}
6868
} else if (expression is PropertyAccess) {
69-
var variable = expression.propertyName.staticElement.variableElement;
70-
if (variable is VariableElement) {
69+
var variable = expression.propertyName.element.variableElement;
70+
if (variable is VariableElement2) {
7171
enumConstants.remove(variable.computeConstantValue());
7272
}
7373
}
@@ -82,7 +82,7 @@ class _Visitor extends SimpleAstVisitor<void> {
8282
var end = statement.rightParenthesis.end;
8383
var elements = enumConstants[constant]!;
8484
var preferredElement = elements.firstWhere(
85-
(element) => !element.hasDeprecated,
85+
(element) => !element.metadata2.hasDeprecated,
8686
orElse: () => elements.first);
8787
rule.reportLintForOffset(
8888
offset,
@@ -94,11 +94,11 @@ class _Visitor extends SimpleAstVisitor<void> {
9494
}
9595
}
9696

97-
extension on Element? {
98-
Element? get variableElement {
97+
extension on Element2? {
98+
Element2? get variableElement {
9999
var self = this;
100-
if (self is PropertyAccessorElement) {
101-
var variable = self.variable2;
100+
if (self is GetterElement) {
101+
var variable = self.variable3;
102102
if (variable != null) {
103103
return variable;
104104
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
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
import 'package:analyzer/dart/element/type.dart';
99

1010
import '../analyzer.dart';
@@ -42,9 +42,9 @@ class _Visitor extends SimpleAstVisitor<void> {
4242
if (expressionType is InterfaceType) {
4343
for (var member in statement.members) {
4444
if (member is SwitchDefault) {
45-
var interfaceElement = expressionType.element;
46-
if (interfaceElement is EnumElement ||
47-
interfaceElement is ClassElement &&
45+
var interfaceElement = expressionType.element3;
46+
if (interfaceElement is EnumElement2 ||
47+
interfaceElement is ClassElement2 &&
4848
interfaceElement.isEnumLikeClass()) {
4949
rule.reportLint(member);
5050
}

0 commit comments

Comments
 (0)