Skip to content

Commit

Permalink
Elements. Migrate AddMissingParameter.
Browse files Browse the repository at this point in the history
Change-Id: If292e7155f7f13e3208759325801197736d84ad9
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/387623
Reviewed-by: Samuel Rawlins <srawlins@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
  • Loading branch information
scheglov authored and Commit Queue committed Sep 30, 2024
1 parent 1f8aea0 commit 52b1b9d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
1 change: 1 addition & 0 deletions pkg/analysis_server/analyzer_use_new_elements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ lib/src/services/correction/dart/add_key_to_constructors.dart
lib/src/services/correction/dart/add_late.dart
lib/src/services/correction/dart/add_leading_newline_to_string.dart
lib/src/services/correction/dart/add_missing_enum_like_case_clauses.dart
lib/src/services/correction/dart/add_missing_parameter.dart
lib/src/services/correction/dart/add_missing_parameter_named.dart
lib/src/services/correction/dart/add_missing_required_argument.dart
lib/src/services/correction/dart/add_ne_null.dart
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class AddMissingParameter extends MultiCorrectionProducer {
return const [];
}

var includeOptional = executableParameters.optionalPositional.isEmpty &&
executableParameters.named.isEmpty;
var includeOptional = executableParameters.optionalPositional2.isEmpty &&
executableParameters.named2.isEmpty;
return <ResolvedCorrectionProducer>[
_AddMissingRequiredPositionalParameter(executableParameters,
context: context),
Expand All @@ -56,10 +56,11 @@ class _AddMissingOptionalPositionalParameter extends _AddMissingParameter {

@override
Future<void> compute(ChangeBuilder builder) async {
var prefix = _executableParameters.required.isNotEmpty ? ', [' : '[';
if (_executableParameters.required.isNotEmpty) {
var prefix = _executableParameters.required2.isNotEmpty ? ', [' : '[';
if (_executableParameters.required2.isNotEmpty) {
var lastElement = _executableParameters.required2.last;
var prevNode = await _executableParameters
.getParameterNode(_executableParameters.required.last);
.getParameterNode2(lastElement.firstFragment!);
await _addParameter(builder, prevNode?.end, prefix, ']');
} else {
var parameterList = await _executableParameters.getParameterList();
Expand Down Expand Up @@ -89,7 +90,7 @@ abstract class _AddMissingParameter extends ResolvedCorrectionProducer {
return;
}
List<Expression> arguments = argumentList.arguments;
var numRequired = _executableParameters.required.length;
var numRequired = _executableParameters.required2.length;
if (numRequired >= arguments.length) {
return;
}
Expand Down Expand Up @@ -118,15 +119,17 @@ class _AddMissingRequiredPositionalParameter extends _AddMissingParameter {

@override
Future<void> compute(ChangeBuilder builder) async {
if (_executableParameters.required.isNotEmpty) {
if (_executableParameters.required2.isNotEmpty) {
var lastElement = _executableParameters.required2.last;
var prevNode = await _executableParameters
.getParameterNode(_executableParameters.required.last);
.getParameterNode2(lastElement.firstFragment!);
await _addParameter(builder, prevNode?.end, ', ', '');
} else {
var parameterList = await _executableParameters.getParameterList();
var offset = parameterList?.leftParenthesis.end;
var suffix =
_executableParameters.executable.parameters.isNotEmpty ? ', ' : '';
var suffix = _executableParameters.executable2.formalParameters.isNotEmpty
? ', '
: '';
await _addParameter(builder, offset, '', suffix);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:analyzer/src/dart/analysis/session_helper.dart';
class ExecutableParameters {
final AnalysisSessionHelper sessionHelper;
final ExecutableElement executable;
final ExecutableElement2 executable2;

final List<ParameterElement> required = [];
final List<ParameterElement> optionalPositional = [];
Expand All @@ -20,7 +21,11 @@ class ExecutableParameters {
final List<FormalParameterElement> optionalPositional2 = [];
final List<FormalParameterElement> named2 = [];

ExecutableParameters._(this.sessionHelper, this.executable) {
ExecutableParameters._(
this.sessionHelper,
this.executable,
this.executable2,
) {
for (var parameter in executable.parameters) {
if (parameter.isRequiredPositional) {
required.add(parameter);
Expand Down Expand Up @@ -89,18 +94,26 @@ class ExecutableParameters {
static ExecutableParameters? forInvocation(
AnalysisSessionHelper sessionHelper, AstNode? invocation) {
Element? element;
Element2? element2;
// This doesn't handle FunctionExpressionInvocation.
if (invocation is Annotation) {
element = invocation.element;
element2 = invocation.element2;
} else if (invocation is InstanceCreationExpression) {
element = invocation.constructorName.staticElement;
element2 = invocation.constructorName.element;
} else if (invocation is MethodInvocation) {
element = invocation.methodName.staticElement;
element2 = invocation.methodName.element;
} else if (invocation is ConstructorReferenceNode) {
element = invocation.staticElement;
element2 = invocation.element;
}
if (element is ExecutableElement && !element.isSynthetic) {
return ExecutableParameters._(sessionHelper, element);
if (element is ExecutableElement &&
!element.isSynthetic &&
element2 is ExecutableElement2 &&
!element2.isSynthetic) {
return ExecutableParameters._(sessionHelper, element, element2);
} else {
return null;
}
Expand Down

0 comments on commit 52b1b9d

Please sign in to comment.