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 719164613c6..a69a06e9818 100644 --- a/pkg/analyzer/lib/src/error/deprecated_member_use_verifier.dart +++ b/pkg/analyzer/lib/src/error/deprecated_member_use_verifier.dart @@ -10,16 +10,11 @@ import 'package:analyzer/src/dart/error/hint_codes.dart'; import 'package:analyzer/src/workspace/workspace.dart'; import 'package:collection/collection.dart'; -class DeprecatedMemberUseVerifier { - final WorkspacePackage? _workspacePackage; - final ErrorReporter _errorReporter; - +abstract class BaseDeprecatedMemberUseVerifier { /// We push a new value every time when we enter into a scope which /// can be marked as deprecated - a class, a method, fields (multiple). final List _inDeprecatedMemberStack = [false]; - DeprecatedMemberUseVerifier(this._workspacePackage, this._errorReporter); - void assignmentExpression(AssignmentExpression node) { _checkForDeprecated(node.readElement, node.leftHandSide); _checkForDeprecated(node.writeElement, node.leftHandSide); @@ -99,6 +94,9 @@ class DeprecatedMemberUseVerifier { _invocationArguments(node.staticElement, node.argumentList); } + void reportError( + AstNode errorNode, Element element, String displayName, String? message); + void simpleIdentifier(SimpleIdentifier node) { // Don't report declared identifiers. if (node.inDeclarationContext()) { @@ -181,25 +179,8 @@ class DeprecatedMemberUseVerifier { var invokeClass = invokeType.element2; displayName = "${invokeClass.name}.${element.displayName}"; } - var library = element is LibraryElement ? element : element.library; var message = _deprecatedMessage(element); - if (message == null || message.isEmpty) { - _errorReporter.reportErrorForNode( - _isLibraryInWorkspacePackage(library) - ? HintCode.DEPRECATED_MEMBER_USE_FROM_SAME_PACKAGE - : HintCode.DEPRECATED_MEMBER_USE, - errorNode, - [displayName], - ); - } else { - _errorReporter.reportErrorForNode( - _isLibraryInWorkspacePackage(library) - ? HintCode.DEPRECATED_MEMBER_USE_FROM_SAME_PACKAGE_WITH_MESSAGE - : HintCode.DEPRECATED_MEMBER_USE_WITH_MESSAGE, - errorNode, - [displayName, message], - ); - } + reportError(errorNode, element, displayName, message); } void _invocationArguments(Element? element, ArgumentList arguments) { @@ -213,15 +194,6 @@ class DeprecatedMemberUseVerifier { } } - bool _isLibraryInWorkspacePackage(LibraryElement? 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); - } - void _simpleIdentifier(SimpleIdentifier identifier) { _checkForDeprecated(identifier.staticElement, identifier); } @@ -323,3 +295,43 @@ class DeprecatedMemberUseVerifier { } } } + +class DeprecatedMemberUseVerifier extends BaseDeprecatedMemberUseVerifier { + final WorkspacePackage? _workspacePackage; + final ErrorReporter _errorReporter; + + DeprecatedMemberUseVerifier(this._workspacePackage, this._errorReporter); + + @override + void reportError( + AstNode errorNode, Element element, String displayName, String? message) { + var library = element is LibraryElement ? element : element.library; + + if (message == null || message.isEmpty) { + _errorReporter.reportErrorForNode( + _isLibraryInWorkspacePackage(library) + ? HintCode.DEPRECATED_MEMBER_USE_FROM_SAME_PACKAGE + : HintCode.DEPRECATED_MEMBER_USE, + errorNode, + [displayName], + ); + } else { + _errorReporter.reportErrorForNode( + _isLibraryInWorkspacePackage(library) + ? HintCode.DEPRECATED_MEMBER_USE_FROM_SAME_PACKAGE_WITH_MESSAGE + : HintCode.DEPRECATED_MEMBER_USE_WITH_MESSAGE, + errorNode, + [displayName, message], + ); + } + } + + bool _isLibraryInWorkspacePackage(LibraryElement? 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); + } +}