Allow hooking into DeprecatedMemberUseVerifier for other use

Split out a base class with an abstract `reportError` method that can be overriden to possibly filter errors or report them in a different way.

For the main class there is no change in behavior at all, it just implements `reportError` with the prior behavior.

Bug: b/247790768
Change-Id: I56e9afcd40b35db0063a25bfa4f6afb763267103
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/260200
Auto-Submit: Oleh Prypin <oprypin@google.com>
Commit-Queue: Oleh Prypin <oprypin@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
Oleh Prypin 2022-09-21 08:00:05 +00:00 committed by Commit Bot
parent 1aa398d4be
commit fba97eb77f

View file

@ -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<bool> _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);
}
}