Skip to content

Commit

Permalink
[Migrate] required_parameters_verifier.dart
Browse files Browse the repository at this point in the history
Change-Id: I2103c28faff5b062c59c3ba293dcae791a582ffa
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/402203
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
  • Loading branch information
bwilkerson committed Dec 21, 2024
1 parent 7cc1322 commit 5b9e53e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 31 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 @@ -96,7 +96,6 @@ lib/src/error/duplicate_definition_verifier.dart
lib/src/error/imports_verifier.dart
lib/src/error/inheritance_override.dart
lib/src/error/override_verifier.dart
lib/src/error/required_parameters_verifier.dart
lib/src/error/unused_local_elements_verifier.dart
lib/src/generated/element_resolver.dart
lib/src/generated/element_walker.dart
Expand Down
61 changes: 32 additions & 29 deletions pkg/analyzer/lib/src/error/required_parameters_verifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/syntactic_entity.dart';
import 'package:analyzer/dart/ast/visitor.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';
import 'package:analyzer/src/dart/element/element.dart';
Expand All @@ -20,13 +20,13 @@ class RequiredParametersVerifier extends SimpleAstVisitor<void> {

@override
void visitAnnotation(Annotation node) {
var element = node.element;
var element = node.element2;
var argumentList = node.arguments;
if (element is ConstructorElement && argumentList != null) {
if (element is ConstructorElement2 && argumentList != null) {
var errorNode = node.constructorIdentifier ?? node.classIdentifier;
if (errorNode != null) {
_check(
parameters: element.parameters,
parameters: element.formalParameters,
arguments: argumentList.arguments,
errorEntity: errorNode,
);
Expand All @@ -37,7 +37,7 @@ class RequiredParametersVerifier extends SimpleAstVisitor<void> {
@override
void visitEnumConstantDeclaration(EnumConstantDeclaration node) {
_check(
parameters: node.constructorElement?.parameters,
parameters: node.constructorElement2?.formalParameters,
arguments: node.arguments?.argumentList.arguments ?? <Expression>[],
errorEntity: node.name,
);
Expand All @@ -48,7 +48,7 @@ class RequiredParametersVerifier extends SimpleAstVisitor<void> {
var type = node.staticInvokeType;
if (type is FunctionType) {
_check(
parameters: type.parameters,
parameters: type.formalParameters,
arguments: node.argumentList.arguments,
errorEntity: node,
);
Expand All @@ -58,19 +58,19 @@ class RequiredParametersVerifier extends SimpleAstVisitor<void> {
@override
void visitInstanceCreationExpression(InstanceCreationExpression node) {
_check(
parameters: node.constructorName.staticElement?.parameters,
parameters: node.constructorName.element?.formalParameters,
arguments: node.argumentList.arguments,
errorEntity: node.constructorName,
);
}

@override
void visitMethodInvocation(MethodInvocation node) {
if (node.methodName.name == FunctionElement.CALL_METHOD_NAME) {
if (node.methodName.name == MethodElement2.CALL_METHOD_NAME) {
var targetType = node.realTarget?.staticType;
if (targetType is FunctionType) {
_check(
parameters: targetType.parameters,
parameters: targetType.formalParameters,
arguments: node.argumentList.arguments,
errorEntity: node.argumentList,
);
Expand All @@ -79,7 +79,7 @@ class RequiredParametersVerifier extends SimpleAstVisitor<void> {
}

_check(
parameters: _executableElement(node.methodName.staticElement)?.parameters,
parameters: _executableElement(node.methodName.element)?.formalParameters,
arguments: node.argumentList.arguments,
errorEntity: node.methodName,
);
Expand All @@ -89,7 +89,7 @@ class RequiredParametersVerifier extends SimpleAstVisitor<void> {
void visitRedirectingConstructorInvocation(
RedirectingConstructorInvocation node) {
_check(
parameters: _executableElement(node.staticElement)?.parameters,
parameters: _executableElement(node.element)?.formalParameters,
arguments: node.argumentList.arguments,
errorEntity: node,
);
Expand All @@ -98,31 +98,33 @@ class RequiredParametersVerifier extends SimpleAstVisitor<void> {
@override
void visitSuperConstructorInvocation(
SuperConstructorInvocation node, {
ConstructorElement? enclosingConstructor,
ConstructorElement2? enclosingConstructor,
}) {
_check(
parameters: _executableElement(node.staticElement)?.parameters,
parameters: _executableElement(node.element)?.formalParameters,
enclosingConstructor: enclosingConstructor,
arguments: node.argumentList.arguments,
errorEntity: node,
);
}

void _check({
required List<ParameterElement>? parameters,
ConstructorElement? enclosingConstructor,
required List<FormalParameterElement>? parameters,
ConstructorElement2? enclosingConstructor,
required List<Expression> arguments,
required SyntacticEntity errorEntity,
}) {
if (parameters == null) {
return;
}

for (ParameterElement parameter in parameters) {
for (FormalParameterElement parameter in parameters) {
if (parameter.isRequiredNamed) {
String parameterName = parameter.name;
String parameterName = parameter.name3!;
if (!_containsNamedExpression(
enclosingConstructor, arguments, parameterName)) {
_containsNamedExpression(
enclosingConstructor, arguments, parameterName);
_errorReporter.atEntity(
errorEntity,
CompileTimeErrorCode.MISSING_REQUIRED_ARGUMENT,
Expand All @@ -133,7 +135,7 @@ class RequiredParametersVerifier extends SimpleAstVisitor<void> {
if (parameter.isOptionalNamed) {
var annotation = _requiredAnnotation(parameter);
if (annotation != null) {
String parameterName = parameter.name;
String parameterName = parameter.name3!;
if (!_containsNamedExpression(
enclosingConstructor, arguments, parameterName)) {
var reason = annotation.getReason(strictCasts: true);
Expand All @@ -157,7 +159,7 @@ class RequiredParametersVerifier extends SimpleAstVisitor<void> {
}

static bool _containsNamedExpression(
ConstructorElement? enclosingConstructor,
ConstructorElement2? enclosingConstructor,
List<Expression> arguments,
String name,
) {
Expand All @@ -171,29 +173,30 @@ class RequiredParametersVerifier extends SimpleAstVisitor<void> {
}

if (enclosingConstructor != null) {
return enclosingConstructor.parameters.any((e) =>
e is SuperFormalParameterElement && e.isNamed && e.name == name);
return enclosingConstructor.formalParameters.any((e) =>
e is SuperFormalParameterElement2 && e.isNamed && e.name3 == name);
}

return false;
}

static ExecutableElement? _executableElement(Element? element) {
if (element is ExecutableElement) {
static ExecutableElement2? _executableElement(Element2? element) {
if (element is ExecutableElement2) {
return element;
} else {
return null;
}
}

static _RequiredAnnotation? _requiredAnnotation(ParameterElement element) {
var annotation = element.metadata.firstWhereOrNull((e) => e.isRequired)
as ElementAnnotationImpl?;
static _RequiredAnnotation? _requiredAnnotation(
FormalParameterElement element) {
var annotation = element.metadata2.annotations
.firstWhereOrNull((e) => e.isRequired) as ElementAnnotationImpl?;
if (annotation != null) {
return _RequiredAnnotation(annotation);
}

if (element.declaration.isRequiredNamed) {
if (element.baseElement.isRequiredNamed) {
return _RequiredAnnotation(annotation);
}

Expand Down Expand Up @@ -256,10 +259,10 @@ extension _InstantiatedAnnotation on Annotation {
}

static SimpleIdentifier? _ifClassElement(SimpleIdentifier? node) {
return node?.staticElement is InterfaceElement ? node : null;
return node?.element is InterfaceElement2 ? node : null;
}

static SimpleIdentifier? _ifConstructorElement(SimpleIdentifier? node) {
return node?.staticElement is ConstructorElement ? node : null;
return node?.element is ConstructorElement2 ? node : null;
}
}
4 changes: 3 additions & 1 deletion pkg/analyzer/lib/src/generated/error_verifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ class EnclosingExecutableContext {
return element?.displayName;
}

ExecutableElement2? get element2 => element.asElement2;

bool get isClosure {
return element is FunctionElement && element!.displayName.isEmpty;
}
Expand Down Expand Up @@ -1484,7 +1486,7 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
void visitSuperConstructorInvocation(SuperConstructorInvocation node) {
_requiredParametersVerifier.visitSuperConstructorInvocation(
node,
enclosingConstructor: _enclosingExecutable.element.ifTypeOrNull(),
enclosingConstructor: _enclosingExecutable.element2.ifTypeOrNull(),
);
_constArgumentsVerifier.visitSuperConstructorInvocation(node);
_isInConstructorInitializer = true;
Expand Down

0 comments on commit 5b9e53e

Please sign in to comment.