Skip to content

Commit e526fd7

Browse files
pqCommit Queue
authored andcommitted
[element model] migrate use_enums
Bug: https://github.com/dart-lang/linter/issues/5099 Change-Id: Icb6408171e72cf6065e491b9418eaafe4da11cb6 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/390626 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Phil Quitslund <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]> Auto-Submit: Phil Quitslund <[email protected]>
1 parent 087d2fe commit e526fd7

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

pkg/linter/analyzer_use_new_elements.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,10 @@ lib/src/rules/empty_statements.dart
6565
lib/src/rules/enable_null_safety.dart
6666
lib/src/rules/eol_at_end_of_file.dart
6767
lib/src/rules/erase_dart_type_extension_types.dart
68+
lib/src/rules/exhaustive_cases.dart
6869
lib/src/rules/file_names.dart
6970
lib/src/rules/flutter_style_todos.dart
71+
lib/src/rules/hash_and_equals.dart
7072
lib/src/rules/implicit_call_tearoffs.dart
7173
lib/src/rules/implicit_reopen.dart
7274
lib/src/rules/invalid_case_patterns.dart
@@ -84,6 +86,7 @@ lib/src/rules/literal_only_boolean_expressions.dart
8486
lib/src/rules/missing_code_block_language_in_doc_comment.dart
8587
lib/src/rules/missing_whitespace_between_adjacent_strings.dart
8688
lib/src/rules/no_adjacent_strings_in_list.dart
89+
lib/src/rules/no_default_cases.dart
8790
lib/src/rules/no_duplicate_case_values.dart
8891
lib/src/rules/no_leading_underscores_for_library_prefixes.dart
8992
lib/src/rules/no_leading_underscores_for_local_identifiers.dart
@@ -96,9 +99,11 @@ lib/src/rules/non_constant_identifier_names.dart
9699
lib/src/rules/noop_primitive_operations.dart
97100
lib/src/rules/null_check_on_nullable_type_parameter.dart
98101
lib/src/rules/null_closures.dart
102+
lib/src/rules/omit_local_variable_types.dart
99103
lib/src/rules/omit_obvious_local_variable_types.dart
100104
lib/src/rules/one_member_abstracts.dart
101105
lib/src/rules/only_throw_errors.dart
106+
lib/src/rules/package_api_docs.dart
102107
lib/src/rules/package_prefixed_library_names.dart
103108
lib/src/rules/prefer_adjacent_string_concatenation.dart
104109
lib/src/rules/prefer_asserts_with_message.dart
@@ -174,6 +179,7 @@ lib/src/rules/unnecessary_to_list_in_spreads.dart
174179
lib/src/rules/unrelated_type_equality_checks.dart
175180
lib/src/rules/use_colored_box.dart
176181
lib/src/rules/use_decorated_box.dart
182+
lib/src/rules/use_enums.dart
177183
lib/src/rules/use_full_hex_values_for_flutter_colors.dart
178184
lib/src/rules/use_function_type_syntax_for_parameters.dart
179185
lib/src/rules/use_if_null_to_convert_nulls_to_bools.dart
@@ -196,7 +202,6 @@ lib/src/utils.dart
196202
test/all.dart
197203
test/annotation_test.dart
198204
test/ascii_utils_test.dart
199-
test/canonicalization_test.dart
200205
test/directives_ordering_test.dart
201206
test/doc_test.dart
202207
test/engine_test.dart

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import 'package:analyzer/dart/analysis/features.dart';
66
import 'package:analyzer/dart/ast/ast.dart';
77
import 'package:analyzer/dart/ast/visitor.dart';
8-
import 'package:analyzer/dart/element/element.dart';
8+
import 'package:analyzer/dart/element/element2.dart';
99

1010
import '../analyzer.dart';
1111
import '../ast.dart';
@@ -36,17 +36,17 @@ class UseEnums extends LintRule {
3636
/// A superclass for the [_EnumVisitor] and [_NonEnumVisitor].
3737
class _BaseVisitor extends RecursiveAstVisitor<void> {
3838
/// The element representing the enum declaration that's being visited.
39-
final ClassElement classElement;
39+
final ClassElement2 classElement;
4040

4141
_BaseVisitor(this.classElement);
4242

4343
/// Return `true` if the given [node] is an invocation of a generative
4444
/// constructor from the class being converted.
4545
bool invokesGenerativeConstructor(InstanceCreationExpression node) {
46-
var constructorElement = node.constructorName.staticElement;
46+
var constructorElement = node.constructorName.element;
4747
return constructorElement != null &&
4848
!constructorElement.isFactory &&
49-
constructorElement.enclosingElement3 == classElement;
49+
constructorElement.enclosingElement2 == classElement;
5050
}
5151
}
5252

@@ -101,18 +101,18 @@ class _NonEnumVisitor extends _BaseVisitor {
101101

102102
@override
103103
void visitClassDeclaration(ClassDeclaration node) {
104-
var element = node.declaredElement;
104+
var element = node.declaredFragment?.element;
105105
if (element == null) {
106106
throw _InvalidEnumException();
107107
}
108108
if (element != classElement) {
109-
if (element.supertype?.element == classElement) {
109+
if (element.supertype?.element3 == classElement) {
110110
throw _InvalidEnumException();
111111
} else if (element.interfaces
112-
.map((e) => e.element)
112+
.map((e) => e.element3)
113113
.contains(classElement)) {
114114
throw _InvalidEnumException();
115-
} else if (element.mixins.map((e) => e.element).contains(classElement)) {
115+
} else if (element.mixins.map((e) => e.element3).contains(classElement)) {
116116
// This case won't occur unless there's an error in the source code, but
117117
// it's easier to check for the condition than it is to check for the
118118
// diagnostic.
@@ -143,7 +143,7 @@ class _Visitor extends SimpleAstVisitor<void> {
143143
if (node.isAugmentation) return;
144144

145145
if (node.abstractKeyword != null) return;
146-
var classElement = node.declaredElement;
146+
var classElement = node.declaredFragment?.element;
147147
if (classElement == null) return;
148148

149149
// Enums can only extend Object.
@@ -162,23 +162,23 @@ class _Visitor extends SimpleAstVisitor<void> {
162162
if (member is FieldDeclaration) {
163163
if (!member.isStatic) continue;
164164
for (var field in member.fields.variables) {
165-
var fieldElement = field.declaredElement;
166-
if (fieldElement is! FieldElement) continue;
165+
var fieldElement = field.declaredFragment?.element;
166+
if (fieldElement is! FieldElement2) continue;
167167
if (field.isSynthetic || !field.isConst) continue;
168168
var initializer = field.initializer;
169169
if (initializer is! InstanceCreationExpression) continue;
170170

171-
var constructorElement = initializer.constructorName.staticElement;
171+
var constructorElement = initializer.constructorName.element;
172172
if (constructorElement == null) continue;
173173
if (constructorElement.isFactory) continue;
174-
if (constructorElement.enclosingElement3 != classElement) continue;
174+
if (constructorElement.enclosingElement2 != classElement) continue;
175175
if (fieldElement.computeConstantValue() == null) continue;
176176

177177
candidateConstants.add(field);
178178
}
179179
}
180180
if (member is ConstructorDeclaration) {
181-
var constructor = member.declaredElement;
181+
var constructor = member.declaredFragment?.element;
182182
if (constructor == null) return;
183183
if (!constructor.isFactory && !constructor.isConst) return;
184184
var name = member.name?.lexeme;

0 commit comments

Comments
 (0)