Skip to content

Commit

Permalink
[analysis_server] fix ConvertForEachToForLoop
Browse files Browse the repository at this point in the history
Fixes #51204
Fixes #51205

Change-Id: I807329d5f1e355767a5ffe26b14e07fc2b4c33fd
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/280176
Reviewed-by: Brian Wilkerson <[email protected]>
Commit-Queue: Brian Wilkerson <[email protected]>
  • Loading branch information
asashour authored and Commit Queue committed Feb 2, 2023
1 parent a9d9b73 commit 7927c5b
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,12 @@ class ConvertForEachToForLoop extends CorrectionProducer {
body.visitChildren(_ReturnVisitor(builder));
});
} else if (body is ExpressionFunctionBody) {
var expression = body.expression;
if (expression is SetOrMapLiteral && expression.typeArguments == null) {
return;
}
var prefix = utils.getPrefix(statement.offset);
await builder.addDartFileEdit(file, (builder) {
var expression = body.expression;
var prefix = utils.getPrefix(statement.offset);
builder.addReplacement(range.startStart(invocation, expression),
(builder) {
builder.write('for (var ');
Expand All @@ -97,6 +100,11 @@ class _ReturnVisitor extends RecursiveAstVisitor<void> {

_ReturnVisitor(this.builder);

@override
void visitFunctionExpression(FunctionExpression node) {
// Don't visit children.
}

@override
void visitReturnStatement(ReturnStatement node) {
builder.addSimpleReplacement(range.node(node), 'continue;');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,53 @@ void f(List<String> list) {
LintNames.avoid_function_literals_in_foreach_calls);
}

Future<void> test_functionExpression() async {
await resolveTestCode('''
void f(List<List<int?>> lists) {
lists.forEach((list) {
list.map((x) {
if (x == null) return 0;
return x.abs();
});
});
}
''');
await assertHasFix('''
void f(List<List<int?>> lists) {
for (var list in lists) {
list.map((x) {
if (x == null) return 0;
return x.abs();
});
}
}
''');
}

Future<void> test_mapLiteral() async {
await resolveTestCode('''
void f(List<int> list) {
list.forEach((x) => {1: 2});
}
''');
await assertNoFix();
}

Future<void> test_mapLiteral_typeArguments() async {
await resolveTestCode('''
void f(List<int> list) {
list.forEach((x) => <int, int>{x: 2});
}
''');
await assertHasFix('''
void f(List<int> list) {
for (var x in list) {
<int, int>{x: 2};
}
}
''');
}

Future<void> test_return() async {
await resolveTestCode('''
void f(List<String> list) {
Expand All @@ -228,6 +275,48 @@ void f(List<String> list) {
}
}
}
''');
}

Future<void> test_setLiteral() async {
await resolveTestCode('''
void f(List<int> list) {
list.forEach((x) => {print('')});
}
''');
await assertNoFix();
}

Future<void> test_setLiteral_multiple() async {
await resolveTestCode('''
void f(List<int> list) {
list.forEach((x) => {print(''), print('')});
}
''');
await assertNoFix();
}

Future<void> test_setLiteral_statement() async {
await resolveTestCode('''
void f(List<int> list, bool b) {
list.forEach((x) => {if (b) print('')});
}
''');
await assertNoFix();
}

Future<void> test_setLiteral_typeArguments() async {
await resolveTestCode('''
void f(List<int> list) {
list.forEach((x) => <int>{x});
}
''');
await assertHasFix('''
void f(List<int> list) {
for (var x in list) {
<int>{x};
}
}
''');
}
}

0 comments on commit 7927c5b

Please sign in to comment.