From 354c64f52f3813874f04cae26919cae8d7651bb8 Mon Sep 17 00:00:00 2001 From: Brian Wilkerson Date: Sat, 21 Dec 2024 07:25:01 -0800 Subject: [PATCH] [Migration] deprecated_member_use_verifier.dart Change-Id: I2e64e8693c829d130ef36396488a25454bdc4990 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/402201 Reviewed-by: Konstantin Shcheglov --- pkg/analyzer/analyzer_use_new_elements.txt | 1 - .../error/deprecated_member_use_verifier.dart | 140 +++++++++--------- .../deprecated_member_use_test.dart | 4 +- 3 files changed, 73 insertions(+), 72 deletions(-) diff --git a/pkg/analyzer/analyzer_use_new_elements.txt b/pkg/analyzer/analyzer_use_new_elements.txt index b5f4ae7fa5d1..cd8ab8bc19e8 100644 --- a/pkg/analyzer/analyzer_use_new_elements.txt +++ b/pkg/analyzer/analyzer_use_new_elements.txt @@ -91,7 +91,6 @@ lib/src/dart/resolver/yield_statement_resolver.dart lib/src/diagnostic/diagnostic_factory.dart lib/src/error/best_practices_verifier.dart lib/src/error/correct_override.dart -lib/src/error/deprecated_member_use_verifier.dart lib/src/error/duplicate_definition_verifier.dart lib/src/error/imports_verifier.dart lib/src/error/inheritance_override.dart diff --git a/pkg/analyzer/lib/src/error/deprecated_member_use_verifier.dart b/pkg/analyzer/lib/src/error/deprecated_member_use_verifier.dart index fdbe847805dc..d0a2e430f3c3 100644 --- a/pkg/analyzer/lib/src/error/deprecated_member_use_verifier.dart +++ b/pkg/analyzer/lib/src/error/deprecated_member_use_verifier.dart @@ -3,7 +3,6 @@ // BSD-style license that can be found in the LICENSE file. import 'package:analyzer/dart/ast/syntactic_entity.dart'; -import 'package:analyzer/dart/element/element.dart'; import 'package:analyzer/dart/element/element2.dart'; import 'package:analyzer/dart/element/type.dart'; import 'package:analyzer/error/listener.dart'; @@ -24,63 +23,63 @@ abstract class BaseDeprecatedMemberUseVerifier { : _strictCasts = strictCasts; void assignmentExpression(AssignmentExpression node) { - _checkForDeprecated(node.readElement, node.leftHandSide); - _checkForDeprecated(node.writeElement, node.leftHandSide); - _checkForDeprecated(node.staticElement, node); + _checkForDeprecated(node.readElement2, node.leftHandSide); + _checkForDeprecated(node.writeElement2, node.leftHandSide); + _checkForDeprecated(node.element, node); } void binaryExpression(BinaryExpression node) { - _checkForDeprecated(node.staticElement, node); + _checkForDeprecated(node.element, node); } void constructorName(ConstructorName node) { - _checkForDeprecated(node.staticElement, node); + _checkForDeprecated(node.element, node); } void exportDirective(ExportDirective node) { - _checkForDeprecated(node.element?.exportedLibrary, node); + _checkForDeprecated(node.libraryExport?.exportedLibrary2, node); } void extensionOverride(ExtensionOverride node) { - _checkForDeprecated(node.element, node); + _checkForDeprecated(node.element2, node); } void functionExpressionInvocation(FunctionExpressionInvocation node) { - var callElement = node.staticElement; - if (callElement is MethodElement && - callElement.name == FunctionElement.CALL_METHOD_NAME) { + var callElement = node.element; + if (callElement is MethodElement2 && + callElement.name3 == MethodElement2.CALL_METHOD_NAME) { _checkForDeprecated(callElement, node); } } void importDirective(ImportDirective node) { - _checkForDeprecated(node.element?.importedLibrary, node); + _checkForDeprecated(node.libraryImport?.importedLibrary2, node); } void indexExpression(IndexExpression node) { - _checkForDeprecated(node.staticElement, node); + _checkForDeprecated(node.element, node); } void instanceCreationExpression(InstanceCreationExpression node) { _invocationArguments( - node.constructorName.staticElement, + node.constructorName.element, node.argumentList, ); } void methodInvocation(MethodInvocation node) { _invocationArguments( - node.methodName.staticElement, + node.methodName.element, node.argumentList, ); } void namedType(NamedType node) { - _checkForDeprecated(node.element, node); + _checkForDeprecated(node.element2, node); } void patternField(PatternField node) { - _checkForDeprecated(node.element, node); + _checkForDeprecated(node.element2, node); } void popInDeprecated() { @@ -88,15 +87,15 @@ abstract class BaseDeprecatedMemberUseVerifier { } void postfixExpression(PostfixExpression node) { - _checkForDeprecated(node.readElement, node.operand); - _checkForDeprecated(node.writeElement, node.operand); - _checkForDeprecated(node.staticElement, node); + _checkForDeprecated(node.readElement2, node.operand); + _checkForDeprecated(node.writeElement2, node.operand); + _checkForDeprecated(node.element, node); } void prefixExpression(PrefixExpression node) { - _checkForDeprecated(node.readElement, node.operand); - _checkForDeprecated(node.writeElement, node.operand); - _checkForDeprecated(node.staticElement, node); + _checkForDeprecated(node.readElement2, node.operand); + _checkForDeprecated(node.writeElement2, node.operand); + _checkForDeprecated(node.element, node); } void pushInDeprecatedMetadata(List metadata) { @@ -110,18 +109,18 @@ abstract class BaseDeprecatedMemberUseVerifier { } void redirectingConstructorInvocation(RedirectingConstructorInvocation node) { - _checkForDeprecated(node.staticElement, node); - _invocationArguments(node.staticElement, node.argumentList); + _checkForDeprecated(node.element, node); + _invocationArguments(node.element, node.argumentList); } - void reportError(SyntacticEntity errorEntity, Element element, + void reportError(SyntacticEntity errorEntity, Element2 element, String displayName, String? message) { - reportError2(errorEntity, element.asElement2!, displayName, message); + reportError2(errorEntity, element, displayName, message); } void reportError2(SyntacticEntity errorEntity, Element2 element, String displayName, String? message) { - reportError(errorEntity, element.asElement!, displayName, message); + reportError(errorEntity, element, displayName, message); } void simpleIdentifier(SimpleIdentifier node) { @@ -151,14 +150,14 @@ abstract class BaseDeprecatedMemberUseVerifier { } void superConstructorInvocation(SuperConstructorInvocation node) { - _checkForDeprecated(node.staticElement, node); - _invocationArguments(node.staticElement, node.argumentList); + _checkForDeprecated(node.element, node); + _invocationArguments(node.element, node.argumentList); } /// Given some [element], look at the associated metadata and report the use /// of the member if it is declared as deprecated. If a diagnostic is reported /// it should be reported at the given [node]. - void _checkForDeprecated(Element? element, AstNode node) { + void _checkForDeprecated(Element2? element, AstNode node) { if (!_isDeprecated(element)) { return; } @@ -171,7 +170,7 @@ abstract class BaseDeprecatedMemberUseVerifier { return; } - if (element is ParameterElement && element.isRequired) { + if (element is FormalParameterElement && element.isRequired) { return; } @@ -207,30 +206,30 @@ abstract class BaseDeprecatedMemberUseVerifier { } String displayName = element!.displayName; - if (element is ConstructorElement) { + if (element is ConstructorElement2) { // TODO(jwren): We should modify ConstructorElement.displayName, // or have the logic centralized elsewhere, instead of doing this logic // here. - displayName = element.name == '' + displayName = element.name3 == null ? '${element.displayName}.new' : element.displayName; - } else if (element is LibraryElement) { - displayName = element.definingCompilationUnit.source.uri.toString(); + } else if (element is LibraryElement2) { + displayName = element.firstFragment.source.uri.toString(); } else if (node is MethodInvocation && - displayName == FunctionElement.CALL_METHOD_NAME) { + displayName == MethodElement2.CALL_METHOD_NAME) { var invokeType = node.staticInvokeType as InterfaceType; - var invokeClass = invokeType.element; - displayName = "${invokeClass.name}.${element.displayName}"; + var invokeClass = invokeType.element3; + displayName = "${invokeClass.name3}.${element.displayName}"; } var message = _deprecatedMessage(element, strictCasts: _strictCasts); reportError(errorEntity, element, displayName, message); } - void _invocationArguments(Element? element, ArgumentList arguments) { - element = element?.declaration; - if (element is ExecutableElement) { + void _invocationArguments(Element2? element, ArgumentList arguments) { + element = element?.baseElement; + if (element is ExecutableElement2) { _visitParametersAndArguments( - element.parameters, + element.formalParameters, arguments.arguments, _checkForDeprecated, ); @@ -238,24 +237,24 @@ abstract class BaseDeprecatedMemberUseVerifier { } void _simpleIdentifier(SimpleIdentifier identifier) { - _checkForDeprecated(identifier.staticElement, identifier); + _checkForDeprecated(identifier.element, identifier); } /// Return the message in the deprecated annotation on the given [element], or /// `null` if the element doesn't have a deprecated annotation or if the /// annotation does not have a message. - static String? _deprecatedMessage(Element element, + static String? _deprecatedMessage(Element2 element, {required bool strictCasts}) { // Implicit getters/setters. - if (element.isSynthetic && element is PropertyAccessorElement) { - var variable = element.variable2; + if (element.isSynthetic && element is PropertyAccessorElement2) { + var variable = element.variable3; if (variable == null) { return null; } element = variable; } var annotation = element.metadata.firstWhereOrNull((e) => e.isDeprecated); - if (annotation == null || annotation.element is PropertyAccessorElement) { + if (annotation == null || annotation.element2 is PropertyAccessorElement2) { return null; } var constantValue = annotation.computeConstantValue(); @@ -272,35 +271,40 @@ abstract class BaseDeprecatedMemberUseVerifier { return false; } - static bool _isDeprecated(Element? element) { + static bool _isDeprecated(Element2? element) { if (element == null) { return false; } - if (element is PropertyAccessorElement && element.isSynthetic) { + if (element is PropertyAccessorElement2 && element.isSynthetic) { // TODO(brianwilkerson): Why isn't this the implementation for PropertyAccessorElement? - var variable = element.variable2; - return variable != null && variable.hasDeprecated; + var variable = element.variable3; + return variable != null && variable.metadata2.hasDeprecated; + } + if (element is Annotatable) { + return (element as Annotatable).metadata2.hasDeprecated; } - return element.hasDeprecated; + return false; } - /// Return `true` if [element] is a [ParameterElement] declared in [node]. - static bool _isLocalParameter(Element? element, AstNode? node) { - if (element is ParameterElement) { - var definingFunction = element.enclosingElement3 as ExecutableElement; + /// Returns whether [element] is a [FormalParameterElement] declared in + /// [node]. + static bool _isLocalParameter(Element2? element, AstNode? node) { + if (element is FormalParameterElement) { + var definingFunction = element.firstFragment.enclosingFragment?.element + as ExecutableElement2; for (; node != null; node = node.parent) { if (node is ConstructorDeclaration) { - if (node.declaredElement == definingFunction) { + if (node.declaredFragment?.element == definingFunction) { return true; } } else if (node is FunctionExpression) { - if (node.declaredElement == definingFunction) { + if (node.declaredFragment?.element == definingFunction) { return true; } } else if (node is MethodDeclaration) { - if (node.declaredElement == definingFunction) { + if (node.declaredFragment?.element == definingFunction) { return true; } } @@ -310,11 +314,11 @@ abstract class BaseDeprecatedMemberUseVerifier { } static void _visitParametersAndArguments( - List parameters, + List parameters, List arguments, - void Function(ParameterElement, Expression) f, + void Function(FormalParameterElement, Expression) f, ) { - Map? namedParameters; + Map? namedParameters; var positionalIndex = 0; for (var argument in arguments) { @@ -323,7 +327,7 @@ abstract class BaseDeprecatedMemberUseVerifier { namedParameters = {}; for (var parameter in parameters) { if (parameter.isNamed) { - namedParameters[parameter.name] = parameter; + namedParameters[parameter.name3!] = parameter; } } } @@ -352,9 +356,9 @@ class DeprecatedMemberUseVerifier extends BaseDeprecatedMemberUseVerifier { {required super.strictCasts}); @override - void reportError(SyntacticEntity errorEntity, Element element, + void reportError(SyntacticEntity errorEntity, Element2 element, String displayName, String? message) { - var library = element is LibraryElement ? element : element.library; + var library = element is LibraryElement2 ? element : element.library2; message = message?.trim(); if (message == null || message.isEmpty || message == '.') { @@ -381,12 +385,12 @@ class DeprecatedMemberUseVerifier extends BaseDeprecatedMemberUseVerifier { } } - bool _isLibraryInWorkspacePackage(LibraryElement? library) { + bool _isLibraryInWorkspacePackage(LibraryElement2? library) { // Better to not make a big claim that they _are_ in the same package, // if we were unable to determine what package [_currentLibrary] is in. if (_workspacePackage == null || library == null) { return false; } - return _workspacePackage.contains(library.source); + return _workspacePackage.contains(library.firstFragment.source); } } diff --git a/pkg/analyzer/test/src/diagnostics/deprecated_member_use_test.dart b/pkg/analyzer/test/src/diagnostics/deprecated_member_use_test.dart index 6fe5b7fb52c3..73eeb0da2819 100644 --- a/pkg/analyzer/test/src/diagnostics/deprecated_member_use_test.dart +++ b/pkg/analyzer/test/src/diagnostics/deprecated_member_use_test.dart @@ -887,8 +887,6 @@ void f() { } ''', [ error(HintCode.DEPRECATED_MEMBER_USE, 43, 1), - // TODO(pq): consider deduplicating. - error(HintCode.DEPRECATED_MEMBER_USE, 43, 1), ]); } @@ -1623,7 +1621,7 @@ class B extends A { ''', [ error(HintCode.DEPRECATED_MEMBER_USE, 57, 7, - text: "'A.new' is deprecated and shouldn't be used."), + text: "'A' is deprecated and shouldn't be used."), ], ); }