From c54255fe9dbdd6546008d81643e0ff203a4e39ca Mon Sep 17 00:00:00 2001 From: pq Date: Mon, 2 Dec 2024 19:51:58 +0000 Subject: [PATCH] [element model] migrate `prefer_asserts_in_initializer_lists` Bug: https://github.com/dart-lang/sdk/issues/59548 Change-Id: I8ff2f686171311d22e4e2c1b1203dc492102aaff Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/398502 Commit-Queue: Brian Wilkerson Auto-Submit: Phil Quitslund Reviewed-by: Brian Wilkerson --- pkg/linter/analyzer_use_new_elements.txt | 1 - .../prefer_asserts_in_initializer_lists.dart | 51 ++++++++++--------- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/pkg/linter/analyzer_use_new_elements.txt b/pkg/linter/analyzer_use_new_elements.txt index a8a5d83e82d1..b7b375004a3e 100644 --- a/pkg/linter/analyzer_use_new_elements.txt +++ b/pkg/linter/analyzer_use_new_elements.txt @@ -3,7 +3,6 @@ lib/src/extensions.dart lib/src/rules/avoid_setters_without_getters.dart lib/src/rules/deprecated_member_use_from_same_package.dart lib/src/rules/invalid_runtime_check_with_js_interop_types.dart -lib/src/rules/prefer_asserts_in_initializer_lists.dart lib/src/rules/prefer_initializing_formals.dart lib/src/rules/unnecessary_overrides.dart lib/src/rules/use_build_context_synchronously.dart diff --git a/pkg/linter/lib/src/rules/prefer_asserts_in_initializer_lists.dart b/pkg/linter/lib/src/rules/prefer_asserts_in_initializer_lists.dart index 04ade10e6200..a00edf19d73d 100644 --- a/pkg/linter/lib/src/rules/prefer_asserts_in_initializer_lists.dart +++ b/pkg/linter/lib/src/rules/prefer_asserts_in_initializer_lists.dart @@ -4,7 +4,7 @@ import 'package:analyzer/dart/ast/ast.dart'; import 'package:analyzer/dart/ast/visitor.dart'; -import 'package:analyzer/dart/element/element.dart'; +import 'package:analyzer/dart/element/element2.dart'; import '../analyzer.dart'; import '../ast.dart'; @@ -31,7 +31,7 @@ class PreferAssertsInInitializerLists extends LintRule { } class _AssertVisitor extends RecursiveAstVisitor { - final ConstructorElement constructorElement; + final ConstructorElement2 constructorElement; final _ClassAndSuperClasses? classAndSuperClasses; bool needInstance = false; @@ -40,18 +40,18 @@ class _AssertVisitor extends RecursiveAstVisitor { @override void visitSimpleIdentifier(SimpleIdentifier node) { - var element = getWriteOrReadElement(node); + var element = getWriteOrReadElement2(node); // use method needInstance = needInstance || - element is MethodElement && !element.isStatic && _hasMethod(element); + element is MethodElement2 && !element.isStatic && _hasMethod(element); // use property accessor not used as field formal parameter needInstance = needInstance || - element is PropertyAccessorElement && + (element is PropertyAccessorElement2) && !element.isStatic && _hasAccessor(element) && - !_paramMatchesField(element, constructorElement.parameters); + !_paramMatchesField(element, constructorElement.formalParameters); } @override @@ -59,26 +59,26 @@ class _AssertVisitor extends RecursiveAstVisitor { needInstance = true; } - bool _hasAccessor(PropertyAccessorElement element) { + bool _hasAccessor(PropertyAccessorElement2 element) { var classes = classAndSuperClasses?.classes; - return classes != null && classes.contains(element.enclosingElement3); + return classes != null && classes.contains(element.enclosingElement2); } - bool _hasMethod(MethodElement element) { + bool _hasMethod(MethodElement2 element) { var classes = classAndSuperClasses?.classes; - return classes != null && classes.contains(element.enclosingElement3); + return classes != null && classes.contains(element.enclosingElement2); } - bool _paramMatchesField( - PropertyAccessorElement element, List parameters) { + bool _paramMatchesField(PropertyAccessorElement2 element, + List parameters) { for (var p in parameters) { - ParameterElement? parameterElement = p; - if (parameterElement is SuperFormalParameterElement) { - parameterElement = parameterElement.superConstructorParameter; + FormalParameterElement? parameterElement = p; + if (parameterElement is SuperFormalParameterElement2) { + parameterElement = parameterElement.superConstructorParameter2; } - if (parameterElement is FieldFormalParameterElement) { - if (parameterElement.field?.getter == element) { + if (parameterElement is FieldFormalParameterElement2) { + if (parameterElement.field2?.getter2 == element) { return true; } } @@ -89,20 +89,20 @@ class _AssertVisitor extends RecursiveAstVisitor { /// Lazy cache of elements. class _ClassAndSuperClasses { - final ClassElement? element; - final Set _classes = {}; + final ClassElement2? element; + final Set _classes = {}; _ClassAndSuperClasses(this.element); /// The [element] and its super classes, including mixins. - Set get classes { + Set get classes { if (_classes.isEmpty) { - void addRecursively(InterfaceElement? element) { + void addRecursively(InterfaceElement2? element) { if (element != null && _classes.add(element)) { for (var t in element.mixins) { - addRecursively(t.element); + addRecursively(t.element3); } - addRecursively(element.supertype?.element); + addRecursively(element.supertype?.element3); } } @@ -122,12 +122,13 @@ class _Visitor extends SimpleAstVisitor { @override void visitClassDeclaration(ClassDeclaration node) { - _classAndSuperClasses = _ClassAndSuperClasses(node.declaredElement); + _classAndSuperClasses = + _ClassAndSuperClasses(node.declaredFragment?.element); } @override void visitConstructorDeclaration(ConstructorDeclaration node) { - var declaredElement = node.declaredElement; + var declaredElement = node.declaredFragment?.element; if (declaredElement == null || declaredElement.isFactory) return; var body = node.body;