Skip to content

Commit

Permalink
[Migration] deprecated_member_use_verifier.dart
Browse files Browse the repository at this point in the history
Change-Id: I2e64e8693c829d130ef36396488a25454bdc4990
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/402201
Reviewed-by: Konstantin Shcheglov <[email protected]>
  • Loading branch information
bwilkerson committed Dec 21, 2024
1 parent 5b9e53e commit 354c64f
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 72 deletions.
1 change: 0 additions & 1 deletion pkg/analyzer/analyzer_use_new_elements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
140 changes: 72 additions & 68 deletions pkg/analyzer/lib/src/error/deprecated_member_use_verifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -24,79 +23,79 @@ 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() {
_inDeprecatedMemberStack.removeLast();
}

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<Annotation> metadata) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand All @@ -171,7 +170,7 @@ abstract class BaseDeprecatedMemberUseVerifier {
return;
}

if (element is ParameterElement && element.isRequired) {
if (element is FormalParameterElement && element.isRequired) {
return;
}

Expand Down Expand Up @@ -207,55 +206,55 @@ 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,
);
}
}

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();
Expand All @@ -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;
}
}
Expand All @@ -310,11 +314,11 @@ abstract class BaseDeprecatedMemberUseVerifier {
}

static void _visitParametersAndArguments(
List<ParameterElement> parameters,
List<FormalParameterElement> parameters,
List<Expression> arguments,
void Function(ParameterElement, Expression) f,
void Function(FormalParameterElement, Expression) f,
) {
Map<String, ParameterElement>? namedParameters;
Map<String, FormalParameterElement>? namedParameters;

var positionalIndex = 0;
for (var argument in arguments) {
Expand All @@ -323,7 +327,7 @@ abstract class BaseDeprecatedMemberUseVerifier {
namedParameters = {};
for (var parameter in parameters) {
if (parameter.isNamed) {
namedParameters[parameter.name] = parameter;
namedParameters[parameter.name3!] = parameter;
}
}
}
Expand Down Expand Up @@ -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 == '.') {
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -887,8 +887,6 @@ void f() {
}
''', [
error(HintCode.DEPRECATED_MEMBER_USE, 43, 1),
// TODO(pq): consider deduplicating.
error(HintCode.DEPRECATED_MEMBER_USE, 43, 1),
]);
}

Expand Down Expand Up @@ -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."),
],
);
}
Expand Down

0 comments on commit 354c64f

Please sign in to comment.