Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit cd908e1

Browse files
author
Dart CI
committed
Version 2.19.0-31.0.dev
Merge commit '56dc3e360ac07e9a7a1d952f2e576c73e0f4d92e' into 'dev'
2 parents 619f19e + 56dc3e3 commit cd908e1

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

pkg/analysis_server/lib/src/services/correction/dart/convert_to_contains.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'package:analysis_server/src/services/correction/dart/abstract_producer.d
66
import 'package:analysis_server/src/services/correction/fix.dart';
77
import 'package:analyzer/dart/ast/ast.dart';
88
import 'package:analyzer/dart/ast/token.dart';
9+
import 'package:analyzer/source/source_range.dart';
910
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
1011
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
1112
import 'package:analyzer_plugin/utilities/range_factory.dart';
@@ -37,6 +38,7 @@ class ConvertToContains extends CorrectionProducer {
3738
return;
3839
}
3940
var methodName = leftOperand.methodName;
41+
var startArgumentRange = _startArgumentRange(leftOperand);
4042
var deletionRange = range.endEnd(leftOperand, rightOperand);
4143
var notOffset = -1;
4244
var style = _negationStyle(comparison.operator.type, value);
@@ -51,6 +53,9 @@ class ConvertToContains extends CorrectionProducer {
5153
builder.addSimpleInsertion(notOffset, '!');
5254
}
5355
builder.addSimpleReplacement(range.node(methodName), 'contains');
56+
if (startArgumentRange != null) {
57+
builder.addDeletion(startArgumentRange);
58+
}
5459
builder.addDeletion(deletionRange);
5560
});
5661
} else if (_isInteger(leftOperand) && rightOperand is MethodInvocation) {
@@ -59,6 +64,7 @@ class ConvertToContains extends CorrectionProducer {
5964
return;
6065
}
6166
var methodName = rightOperand.methodName;
67+
var startArgumentRange = _startArgumentRange(rightOperand);
6268
var deletionRange = range.startStart(leftOperand, rightOperand);
6369
var notOffset = -1;
6470
var style =
@@ -75,6 +81,9 @@ class ConvertToContains extends CorrectionProducer {
7581
builder.addSimpleInsertion(notOffset, '!');
7682
}
7783
builder.addSimpleReplacement(range.node(methodName), 'contains');
84+
if (startArgumentRange != null) {
85+
builder.addDeletion(startArgumentRange);
86+
}
7887
});
7988
}
8089
}
@@ -156,6 +165,16 @@ class ConvertToContains extends CorrectionProducer {
156165
// so we should never reach this point.
157166
return NegationStyle.none;
158167
}
168+
169+
SourceRange? _startArgumentRange(MethodInvocation invocation) {
170+
var arguments = invocation.argumentList.arguments;
171+
if (arguments.length == 2) {
172+
var firstArgument = arguments[0];
173+
var secondArgument = arguments[1];
174+
return range.endEnd(firstArgument, secondArgument);
175+
}
176+
return null;
177+
}
159178
}
160179

161180
/// An indication of whether the `contains` test should be negated, not negated,

pkg/analysis_server/test/src/services/correction/fix/convert_to_contains_test.dart

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,32 @@ bool f(List<int> list, int value) {
240240
bool f(List<int> list, int value) {
241241
return !list.contains(value);
242242
}
243+
''');
244+
}
245+
246+
Future<void> test_twoArguments_left() async {
247+
await resolveTestCode('''
248+
bool f(List<int> list, int value) {
249+
return list.indexOf(value, 0) >= 0;
250+
}
251+
''');
252+
await assertHasFix('''
253+
bool f(List<int> list, int value) {
254+
return list.contains(value);
255+
}
256+
''');
257+
}
258+
259+
Future<void> test_twoArguments_right() async {
260+
await resolveTestCode('''
261+
bool f(List<int> list, int value) {
262+
return 0 <= list.indexOf(value, 0);
263+
}
264+
''');
265+
await assertHasFix('''
266+
bool f(List<int> list, int value) {
267+
return list.contains(value);
268+
}
243269
''');
244270
}
245271
}

pkg/analyzer/lib/src/test_utilities/mock_sdk.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ class List<E> implements Iterable<E> {
467467
void addAll(Iterable<E> iterable) {}
468468
Map<int, E> asMap() => throw 0;
469469
void clear() {}
470-
int indexOf(Object element);
470+
int indexOf(E element, [int start = 0]);
471471
bool remove(Object? value);
472472
E removeLast() => throw 0;
473473

tools/VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ CHANNEL dev
2727
MAJOR 2
2828
MINOR 19
2929
PATCH 0
30-
PRERELEASE 30
30+
PRERELEASE 31
3131
PRERELEASE_PATCH 0

0 commit comments

Comments
 (0)