Skip to content

Commit

Permalink
[element model] migrate prefer_asserts_in_initializer_lists
Browse files Browse the repository at this point in the history
Bug: #59548
Change-Id: I8ff2f686171311d22e4e2c1b1203dc492102aaff
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/398502
Commit-Queue: Brian Wilkerson <[email protected]>
Auto-Submit: Phil Quitslund <[email protected]>
Reviewed-by: Brian Wilkerson <[email protected]>
  • Loading branch information
pq authored and Commit Queue committed Dec 2, 2024
1 parent eaf3912 commit c54255f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 26 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 @@ -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
Expand Down
51 changes: 26 additions & 25 deletions pkg/linter/lib/src/rules/prefer_asserts_in_initializer_lists.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -31,7 +31,7 @@ class PreferAssertsInInitializerLists extends LintRule {
}

class _AssertVisitor extends RecursiveAstVisitor<void> {
final ConstructorElement constructorElement;
final ConstructorElement2 constructorElement;
final _ClassAndSuperClasses? classAndSuperClasses;

bool needInstance = false;
Expand All @@ -40,45 +40,45 @@ class _AssertVisitor extends RecursiveAstVisitor<void> {

@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
void visitThisExpression(ThisExpression node) {
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<ParameterElement> parameters) {
bool _paramMatchesField(PropertyAccessorElement2 element,
List<FormalParameterElement> 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;
}
}
Expand All @@ -89,20 +89,20 @@ class _AssertVisitor extends RecursiveAstVisitor<void> {

/// Lazy cache of elements.
class _ClassAndSuperClasses {
final ClassElement? element;
final Set<InterfaceElement> _classes = {};
final ClassElement2? element;
final Set<InterfaceElement2> _classes = {};

_ClassAndSuperClasses(this.element);

/// The [element] and its super classes, including mixins.
Set<InterfaceElement> get classes {
Set<InterfaceElement2> 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);
}
}

Expand All @@ -122,12 +122,13 @@ class _Visitor extends SimpleAstVisitor<void> {

@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;
Expand Down

0 comments on commit c54255f

Please sign in to comment.