Support for always-false IfStatement(s) in RemoveComparison.

Change-Id: I14a8dedd6770081fc7fe9ee5c4e53c28e029ac3e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/313740
Reviewed-by: Samuel Rawlins <srawlins@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
This commit is contained in:
Konstantin Shcheglov 2023-07-14 18:39:33 +00:00 committed by Commit Queue
parent d1a75901c4
commit d4a70c8423
2 changed files with 170 additions and 23 deletions

View file

@ -82,27 +82,25 @@ class RemoveComparison extends ResolvedCorrectionProducer {
} else if (parent is IfElement) {
await _ifElement(parent, builder);
} else if (parent is IfStatement) {
if (parent.elseStatement == null && _conditionIsTrue) {
await _ifStatement(parent, builder);
}
await _ifStatement(parent, builder);
}
}
Future<void> _ifElement(IfElement node, ChangeBuilder builder) async {
if (_conditionIsTrue) {
Future<void> replaceWithElement(CollectionElement element) async {
final text = _textWithLeadingComments(element);
final unIndented = utils.indentLeft(text);
await builder.addDartFileEdit(file, (builder) {
final text = _textWithLeadingComments(node.thenElement);
final unIndented = utils.indentLeft(text);
builder.addSimpleReplacement(range.node(node), unIndented);
});
}
if (_conditionIsTrue) {
await replaceWithElement(node.thenElement);
} else if (_conditionIsFalse) {
final elseElement = node.elseElement;
if (elseElement != null) {
await builder.addDartFileEdit(file, (builder) {
final text = _textWithLeadingComments(elseElement);
final unIndented = utils.indentLeft(text);
builder.addSimpleReplacement(range.node(node), unIndented);
});
await replaceWithElement(elseElement);
} else {
final elements = node.parent.containerElements;
if (elements != null) {
@ -116,25 +114,57 @@ class RemoveComparison extends ResolvedCorrectionProducer {
}
Future<void> _ifStatement(IfStatement node, ChangeBuilder builder) async {
await builder.addDartFileEdit(file, (builder) {
final body = node.thenStatement;
if (body is Block) {
final text = utils.getRangeText(
utils.getLinesRange(
range.endStart(body.leftBracket, body.rightBracket),
Future<void> replaceWithBlock(Block replacement) async {
final text = utils.getRangeText(
utils.getLinesRange(
range.endStart(
replacement.leftBracket,
replacement.rightBracket,
),
);
final unIndented = utils.indentLeft(text);
),
);
final unIndented = utils.indentLeft(text);
await builder.addDartFileEdit(file, (builder) {
builder.addSimpleReplacement(
utils.getLinesRangeStatements([node]),
unIndented,
);
} else {
final text = _textWithLeadingComments(body);
final unIndented = utils.indentLeft(text);
});
}
Future<void> replaceWithStatement(Statement replacement) async {
final text = _textWithLeadingComments(replacement);
final unIndented = utils.indentLeft(text);
await builder.addDartFileEdit(file, (builder) {
builder.addSimpleReplacement(range.node(node), unIndented);
});
}
final thenStatement = node.thenStatement;
final elseStatement = node.elseStatement;
if (_conditionIsTrue) {
if (thenStatement case Block thenBlock) {
await replaceWithBlock(thenBlock);
} else {
await replaceWithStatement(thenStatement);
}
});
} else if (_conditionIsFalse) {
if (elseStatement != null) {
if (elseStatement case Block elseBlock) {
await replaceWithBlock(elseBlock);
} else {
await replaceWithStatement(elseStatement);
}
} else {
if (node.parent case final Block block) {
final statement = block.statements;
final nodeRange = range.nodeInList(statement, node);
await builder.addDartFileEdit(file, (builder) {
builder.addDeletion(nodeRange);
});
}
}
}
}
/// Use the [builder] to add an edit to delete the operator and given

View file

@ -333,6 +333,123 @@ void f(int x) {
''');
}
Future<void> test_ifStatement_alwaysFalse_hasElse_block() async {
await resolveTestCode('''
void f(int x) {
0;
if (x == null) {
1;
} else {
2;
}
3;
}
''');
await assertHasFix('''
void f(int x) {
0;
2;
3;
}
''');
}
Future<void> test_ifStatement_alwaysFalse_hasElse_block_empty() async {
await resolveTestCode('''
void f(int x) {
0;
if (x == null) {
1;
} else {}
2;
}
''');
await assertHasFix('''
void f(int x) {
0;
2;
}
''');
}
Future<void> test_ifStatement_alwaysFalse_hasElse_statement() async {
await resolveTestCode('''
void f(int x) {
0;
if (x == null) {
1;
} else
2;
3;
}
''');
await assertHasFix('''
void f(int x) {
0;
2;
3;
}
''');
}
Future<void> test_ifStatement_alwaysFalse_noElse() async {
await resolveTestCode('''
void f(int x) {
0;
if (x == null) {
1;
}
2;
}
''');
await assertHasFix('''
void f(int x) {
0;
2;
}
''');
}
Future<void> test_ifStatement_alwaysTrue_hasElse_block() async {
await resolveTestCode('''
void f(int x) {
0;
if (x != null) {
1;
} else {
2;
}
3;
}
''');
await assertHasFix('''
void f(int x) {
0;
1;
3;
}
''');
}
Future<void> test_ifStatement_alwaysTrue_noElse() async {
await resolveTestCode('''
void f(int x) {
0;
if (x != null) {
1;
}
2;
}
''');
await assertHasFix('''
void f(int x) {
0;
1;
2;
}
''');
}
Future<void> test_ifStatement_thenBlock() async {
await resolveTestCode('''
void f(String s) {