diff --git a/pkg/linter/lib/src/extensions.dart b/pkg/linter/lib/src/extensions.dart index 4ad8ffb8fd61..8dec722b5948 100644 --- a/pkg/linter/lib/src/extensions.dart +++ b/pkg/linter/lib/src/extensions.dart @@ -54,8 +54,8 @@ extension AstNodeExtension on AstNode { MethodDeclaration() => self.augmentKeyword != null, MixinDeclaration() => self.augmentKeyword != null, TopLevelVariableDeclaration() => self.augmentKeyword != null, - VariableDeclaration(declaredElement: var element) => - element is PropertyInducingElement && element.isAugmentation, + VariableDeclaration(declaredFragment: var fragment?) => + fragment is PropertyInducingFragment && fragment.isAugmentation, _ => false }; } @@ -64,7 +64,7 @@ extension AstNodeExtension on AstNode { var node = this; if (node.isInternal) return true; if (node is ClassDeclaration) { - var classElement = node.declaredElement; + var classElement = node.declaredFragment?.element; if (classElement != null) { if (classElement.isSealed) return true; if (classElement.isAbstract) { @@ -80,27 +80,15 @@ extension AstNodeExtension on AstNode { var parent = thisOrAncestorOfType(); if (parent == null) return false; - var element = parent.declaredElement; - return element != null && element.hasInternal; + return switch (parent.declaredFragment?.element) { + Annotatable(:var metadata2) => metadata2.hasInternal, + _ => false, + }; } } extension AstNodeNullableExtension on AstNode? { - Element? get canonicalElement { - var self = this; - if (self is Expression) { - var node = self.unParenthesized; - if (node is Identifier) { - return node.staticElement?.canonicalElement; - } else if (node is PropertyAccess) { - return node.propertyName.staticElement?.canonicalElement; - } - } - return null; - } - - Element2? get canonicalElement2 { - // TODO(pq): can this be replaced w/ a use of an `ElementLocator2`? + Element2? get canonicalElement { var self = this; if (self is Expression) { var node = self.unParenthesized; @@ -276,7 +264,7 @@ extension DartTypeExtension on DartType? { bool extendsClass(String? className, String library) { var self = this; if (self is InterfaceType) { - return _extendsClass(self, {}, className, library); + return _extendsClass(self, {}, className, library); } return false; } @@ -291,8 +279,8 @@ extension DartTypeExtension on DartType? { } if (typeToCheck is InterfaceType) { return isAnyInterface(typeToCheck) || - !typeToCheck.element.isSynthetic && - typeToCheck.element.allSupertypes.any(isAnyInterface); + !typeToCheck.element3.isSynthetic && + typeToCheck.element3.allSupertypes.any(isAnyInterface); } else { return false; } @@ -304,7 +292,7 @@ extension DartTypeExtension on DartType? { return false; } bool predicate(InterfaceType i) => i.isSameAs(interface, library); - var element = self.element; + var element = self.element3; return predicate(self) || !element.isSynthetic && element.allSupertypes.any(predicate); } @@ -314,17 +302,17 @@ extension DartTypeExtension on DartType? { bool isSameAs(String? interface, String? library) { var self = this; return self is InterfaceType && - self.element.name == interface && - self.element.library.name == library; + self.element3.name3 == interface && + self.element3.library2.name3 == library; } static bool _extendsClass( InterfaceType? type, - Set seenElements, + Set seenElements, String? className, String? library) => type != null && - seenElements.add(type.element) && + seenElements.add(type.element3) && (type.isSameAs(className, library) || _extendsClass(type.superclass, seenElements, className, library)); } @@ -399,15 +387,15 @@ extension ExpressionExtension on Expression? { case ArgumentList(): // Allow `function(LinkedHashSet())` for `function(LinkedHashSet mySet)` // and `function(LinkedHashMap())` for `function(LinkedHashMap myMap)`. - return self.staticParameterElement?.type ?? InvalidTypeImpl.instance; + return self.correspondingParameter?.type ?? InvalidTypeImpl.instance; case AssignmentExpression(): // Allow `x = LinkedHashMap()`. return ancestor.staticType; case ConditionalExpression(): return ancestor.staticType; case ConstructorFieldInitializer(): - var fieldElement = ancestor.fieldName.staticElement; - return (fieldElement is VariableElement) ? fieldElement.type : null; + var fieldElement = ancestor.fieldName.element; + return (fieldElement is VariableElement2) ? fieldElement.type : null; case ExpressionFunctionBody(parent: var function) when function is FunctionExpression: // Allow `{}.putIfAbsent(3, () => LinkedHashSet())` @@ -426,7 +414,7 @@ extension ExpressionExtension on Expression? { return function.returnType?.type; case NamedExpression(): // Allow `void f({required LinkedHashSet s})`. - return ancestor.staticParameterElement?.type ?? + return ancestor.correspondingParameter?.type ?? InvalidTypeImpl.instance; case ReturnStatement(): return ancestor.thisOrAncestorOfType().expectedReturnType; @@ -460,7 +448,7 @@ extension FunctionBodyExtension on FunctionBody? { if (parent is FunctionExpression) { var grandparent = parent.parent; if (grandparent is FunctionDeclaration) { - var returnType = grandparent.declaredElement?.returnType; + var returnType = grandparent.declaredFragment?.element.returnType; return self._expectedReturnableOrYieldableType(returnType); } var functionType = parent.approximateContextType; @@ -469,7 +457,7 @@ extension FunctionBodyExtension on FunctionBody? { return self._expectedReturnableOrYieldableType(returnType); } if (parent is MethodDeclaration) { - var returnType = parent.declaredElement?.returnType; + var returnType = parent.declaredFragment?.element.returnType; return self._expectedReturnableOrYieldableType(returnType); } return null; @@ -546,9 +534,9 @@ extension InterfaceTypeExtension on InterfaceType { Iterable get implementedInterfaces { void searchSupertypes( InterfaceType? type, - Set alreadyVisited, + Set alreadyVisited, List interfaceTypes) { - if (type == null || !alreadyVisited.add(type.element)) { + if (type == null || !alreadyVisited.add(type.element3)) { return; } interfaceTypes.add(type); diff --git a/pkg/linter/lib/src/rules/avoid_null_checks_in_equality_operators.dart b/pkg/linter/lib/src/rules/avoid_null_checks_in_equality_operators.dart index ec58a90a265e..33897c44a706 100644 --- a/pkg/linter/lib/src/rules/avoid_null_checks_in_equality_operators.dart +++ b/pkg/linter/lib/src/rules/avoid_null_checks_in_equality_operators.dart @@ -25,7 +25,7 @@ bool _isComparingParameterWithNull( _isParameter(node.leftOperand, parameter))); bool _isParameter(Expression expression, Element2? parameter) => - expression.canonicalElement2 == parameter; + expression.canonicalElement == parameter; bool _isParameterWithQuestionQuestion( BinaryExpression node, Element2? parameter) => @@ -69,7 +69,7 @@ class _BodyVisitor extends RecursiveAstVisitor { @override visitMethodInvocation(MethodInvocation node) { if (node.operator?.type == TokenType.QUESTION_PERIOD && - node.target.canonicalElement2 == parameter) { + node.target.canonicalElement == parameter) { rule.reportLint(node); } super.visitMethodInvocation(node); @@ -78,7 +78,7 @@ class _BodyVisitor extends RecursiveAstVisitor { @override visitPropertyAccess(PropertyAccess node) { if (node.operator.type == TokenType.QUESTION_PERIOD && - node.target.canonicalElement2 == parameter) { + node.target.canonicalElement == parameter) { rule.reportLint(node); } super.visitPropertyAccess(node); diff --git a/pkg/linter/lib/src/rules/cascade_invocations.dart b/pkg/linter/lib/src/rules/cascade_invocations.dart index ec92bf6b1826..3bba80604da7 100644 --- a/pkg/linter/lib/src/rules/cascade_invocations.dart +++ b/pkg/linter/lib/src/rules/cascade_invocations.dart @@ -33,7 +33,7 @@ Element2? _getElementFromVariableDeclarationStatement( ExecutableElement2? _getExecutableElementFromMethodInvocation( MethodInvocation node) { if (_isInvokedWithoutNullAwareOperator(node.operator)) { - var executableElement = node.methodName.canonicalElement2; + var executableElement = node.methodName.canonicalElement; if (executableElement is ExecutableElement2) { return executableElement; } @@ -44,20 +44,20 @@ ExecutableElement2? _getExecutableElementFromMethodInvocation( Element2? _getPrefixElementFromExpression(Expression rawExpression) { var expression = rawExpression.unParenthesized; if (expression is PrefixedIdentifier) { - return expression.prefix.canonicalElement2; + return expression.prefix.canonicalElement; } else if (expression is PropertyAccess && _isInvokedWithoutNullAwareOperator(expression.operator) && expression.target is SimpleIdentifier) { - return expression.target.canonicalElement2; + return expression.target.canonicalElement; } return null; } Element2? _getTargetElementFromCascadeExpression(CascadeExpression node) => - node.target.canonicalElement2; + node.target.canonicalElement; Element2? _getTargetElementFromMethodInvocation(MethodInvocation node) => - node.target.canonicalElement2; + node.target.canonicalElement; bool _isInvokedWithoutNullAwareOperator(Token? token) => token?.type == TokenType.PERIOD; @@ -201,12 +201,12 @@ class _CascadableExpression { factory _CascadableExpression._fromPrefixedIdentifier( PrefixedIdentifier node) => - _CascadableExpression._internal(node.prefix.canonicalElement2, [], + _CascadableExpression._internal(node.prefix.canonicalElement, [], canJoin: true, canReceive: true, canBeCascaded: true); factory _CascadableExpression._fromPropertyAccess(PropertyAccess node) { var targetIsSimple = node.target is SimpleIdentifier; - return _CascadableExpression._internal(node.target.canonicalElement2, [], + return _CascadableExpression._internal(node.target.canonicalElement, [], canJoin: targetIsSimple, canReceive: targetIsSimple, canBeCascaded: true); @@ -248,7 +248,7 @@ class _NodeVisitor extends UnifyingAstVisitor { _NodeVisitor(this.expressionBox); bool isCriticalNode(AstNode node) => - node.canonicalElement2 == expressionBox.element; + node.canonicalElement == expressionBox.element; bool isOrHasCriticalNode(AstNode node) { node.accept(this); diff --git a/pkg/linter/lib/src/rules/prefer_final_fields.dart b/pkg/linter/lib/src/rules/prefer_final_fields.dart index 97306a2c3b1d..f6a13ef0db05 100644 --- a/pkg/linter/lib/src/rules/prefer_final_fields.dart +++ b/pkg/linter/lib/src/rules/prefer_final_fields.dart @@ -155,7 +155,7 @@ extension on VariableElement2 { /// Whether `this` is initialized in [initializer]. bool isSetInInitializer(ConstructorInitializer initializer) => initializer is ConstructorFieldInitializer && - initializer.fieldName.canonicalElement2 == this; + initializer.fieldName.canonicalElement == this; /// Whether `this` is initialized with [parameter]. bool isSetInParameter(FormalParameter parameter) { diff --git a/pkg/linter/lib/src/rules/prefer_foreach.dart b/pkg/linter/lib/src/rules/prefer_foreach.dart index e02d6690f9df..6b7ace4ab203 100644 --- a/pkg/linter/lib/src/rules/prefer_foreach.dart +++ b/pkg/linter/lib/src/rules/prefer_foreach.dart @@ -64,7 +64,7 @@ class _PreferForEachVisitor extends SimpleAstVisitor { @override void visitFunctionExpressionInvocation(FunctionExpressionInvocation node) { var arguments = node.argumentList.arguments; - if (arguments.length == 1 && arguments.first.canonicalElement2 == element) { + if (arguments.length == 1 && arguments.first.canonicalElement == element) { rule.reportLint(forEachStatement); } } @@ -74,7 +74,7 @@ class _PreferForEachVisitor extends SimpleAstVisitor { var arguments = node.argumentList.arguments; var target = node.target; if (arguments.length == 1 && - arguments.first.canonicalElement2 == element && + arguments.first.canonicalElement == element && (target == null || !_ReferenceFinder(element).references(target))) { rule.reportLint(forEachStatement); } @@ -92,7 +92,7 @@ class _ReferenceFinder extends UnifyingAstVisitor { _ReferenceFinder(this.element); bool references(Expression target) { - if (target.canonicalElement2 == element) return true; + if (target.canonicalElement == element) return true; target.accept(this); return found; @@ -102,7 +102,7 @@ class _ReferenceFinder extends UnifyingAstVisitor { visitNode(AstNode node) { if (found) return; - found = node.canonicalElement2 == element; + found = node.canonicalElement == element; if (!found) { super.visitNode(node); } diff --git a/pkg/linter/lib/src/rules/prefer_initializing_formals.dart b/pkg/linter/lib/src/rules/prefer_initializing_formals.dart index 45fdff34f813..f2702e37c20a 100644 --- a/pkg/linter/lib/src/rules/prefer_initializing_formals.dart +++ b/pkg/linter/lib/src/rules/prefer_initializing_formals.dart @@ -39,7 +39,7 @@ Iterable _getParameters(ConstructorDeclaration node) => node.parameters.parameters.map((e) => e.declaredFragment?.element); Element2? _getRightElement(AssignmentExpression assignment) => - assignment.rightHandSide.canonicalElement2; + assignment.rightHandSide.canonicalElement; class PreferInitializingFormals extends LintRule { PreferInitializingFormals() diff --git a/pkg/linter/lib/src/rules/unnecessary_overrides.dart b/pkg/linter/lib/src/rules/unnecessary_overrides.dart index 2a42be5e1b7c..f9edd3b06fb4 100644 --- a/pkg/linter/lib/src/rules/unnecessary_overrides.dart +++ b/pkg/linter/lib/src/rules/unnecessary_overrides.dart @@ -264,7 +264,7 @@ class _UnnecessaryOperatorOverrideVisitor parameters != null && parameters.length == 1 && parameters.first.declaredFragment?.element == - node.rightOperand.canonicalElement2) { + node.rightOperand.canonicalElement) { var leftPart = node.leftOperand.unParenthesized; if (leftPart is SuperExpression) { visitSuperExpression(leftPart); @@ -310,7 +310,7 @@ class _UnnecessarySetterOverrideVisitor if (parameters != null && parameters.length == 1 && parameters.first.declaredFragment?.element == - node.rightHandSide.canonicalElement2) { + node.rightHandSide.canonicalElement) { var leftPart = node.leftHandSide.unParenthesized; if (leftPart is PropertyAccess) { if (node.writeElement2?.name3 == _inheritedMethod.name3) { diff --git a/pkg/linter/lib/src/rules/use_rethrow_when_possible.dart b/pkg/linter/lib/src/rules/use_rethrow_when_possible.dart index 5d9fb686d4ed..a7010754c86e 100644 --- a/pkg/linter/lib/src/rules/use_rethrow_when_possible.dart +++ b/pkg/linter/lib/src/rules/use_rethrow_when_possible.dart @@ -37,7 +37,7 @@ class _Visitor extends SimpleAstVisitor { void visitThrowExpression(ThrowExpression node) { if (node.parent is! ExpressionStatement) return; - var element = node.expression.canonicalElement2; + var element = node.expression.canonicalElement; if (element != null) { var catchClause = node.thisOrAncestorOfType(); var exceptionParameter = diff --git a/pkg/linter/lib/src/rules/use_setters_to_change_properties.dart b/pkg/linter/lib/src/rules/use_setters_to_change_properties.dart index 678610837f0c..77b456261fdd 100644 --- a/pkg/linter/lib/src/rules/use_setters_to_change_properties.dart +++ b/pkg/linter/lib/src/rules/use_setters_to_change_properties.dart @@ -51,7 +51,7 @@ class _Visitor extends SimpleAstVisitor { if (expression is AssignmentExpression && expression.operator.type == TokenType.EQ) { var leftOperand = expression.writeElement2?.canonicalElement2; - var rightOperand = expression.rightHandSide.canonicalElement2; + var rightOperand = expression.rightHandSide.canonicalElement; var parameterElement = node.declaredFragment?.element.formalParameters.first; if (rightOperand == parameterElement && leftOperand is FieldElement2) { diff --git a/pkg/linter/lib/src/util/dart_type_utilities.dart b/pkg/linter/lib/src/util/dart_type_utilities.dart index d45e7cb36e23..03515823af35 100644 --- a/pkg/linter/lib/src/util/dart_type_utilities.dart +++ b/pkg/linter/lib/src/util/dart_type_utilities.dart @@ -30,13 +30,13 @@ bool argumentsMatchParameters( } for (var argument in arguments) { if (argument is NamedExpression) { - var element = argument.expression.canonicalElement2; + var element = argument.expression.canonicalElement; if (element == null) { return false; } namedArguments[argument.name.label.name] = element; } else { - var element = argument.canonicalElement2; + var element = argument.canonicalElement; if (element == null) { return false; } @@ -69,7 +69,7 @@ bool canonicalElementsAreEqual(Element2? element1, Element2? element2) => /// Returns whether the canonical elements from two nodes are equal. /// -/// As in, [AstNodeNullableExtension.canonicalElement2], the two nodes must be +/// As in, [AstNodeNullableExtension.canonicalElement], the two nodes must be /// [Expression]s in order to be compared (otherwise `false` is returned). /// /// The two nodes must both be a [SimpleIdentifier], [PrefixedIdentifier], or