Write tests and implementation for conditional expression remove_comparison quickfix.
Change-Id: Ifc3373895b8c96e610e9515e0cae92be85e8d0b5 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/436161 Reviewed-by: Konstantin Shcheglov <scheglov@google.com> Commit-Queue: Konstantin Shcheglov <scheglov@google.com> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
committed by
Commit Queue
parent
062af9fd1e
commit
ab004f3bcc
@@ -85,6 +85,8 @@ class RemoveComparison extends ResolvedCorrectionProducer {
|
||||
await _ifElement(parent, builder);
|
||||
} else if (parent is IfStatement) {
|
||||
await _ifStatement(parent, builder);
|
||||
} else if (parent is ConditionalExpression) {
|
||||
await _conditionalExpression(parent, builder);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -111,6 +113,24 @@ class RemoveComparison extends ResolvedCorrectionProducer {
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
Future<void> _conditionalExpression(
|
||||
ConditionalExpression node,
|
||||
ChangeBuilder builder,
|
||||
) async {
|
||||
Future<void> replaceWithExpression(Expression expression) async {
|
||||
var text = utils.getNodeText(expression);
|
||||
await builder.addDartFileEdit(file, (builder) {
|
||||
builder.addSimpleReplacement(range.node(node), text);
|
||||
});
|
||||
}
|
||||
|
||||
if (_conditionIsTrue) {
|
||||
await replaceWithExpression(node.thenExpression);
|
||||
} else if (_conditionIsFalse) {
|
||||
await replaceWithExpression(node.elseExpression);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _ifElement(IfElement node, ChangeBuilder builder) async {
|
||||
Future<void> replaceWithElement(CollectionElement element) async {
|
||||
var text = _textWithLeadingComments(element);
|
||||
|
||||
@@ -155,6 +155,32 @@ void f(String s) {
|
||||
''');
|
||||
}
|
||||
|
||||
Future<void> test_conditional_expression_alwaysFalse() async {
|
||||
await resolveTestCode('''
|
||||
void f(int x) {
|
||||
print(x == null ? 1 : 0);
|
||||
}
|
||||
''');
|
||||
await assertHasFix('''
|
||||
void f(int x) {
|
||||
print(0);
|
||||
}
|
||||
''', errorFilter: _ignoreDeadCode);
|
||||
}
|
||||
|
||||
Future<void> test_conditional_expression_alwaysTrue() async {
|
||||
await resolveTestCode('''
|
||||
void f(int x) {
|
||||
print(x != null ? 1 : 0);
|
||||
}
|
||||
''');
|
||||
await assertHasFix('''
|
||||
void f(int x) {
|
||||
print(1);
|
||||
}
|
||||
''', errorFilter: _ignoreDeadCode);
|
||||
}
|
||||
|
||||
Future<void> test_ifElement_alwaysFalse_hasElse() async {
|
||||
await resolveTestCode('''
|
||||
void f(int x) {
|
||||
@@ -789,6 +815,32 @@ class RemoveTypeCheckTest extends FixProcessorTest {
|
||||
@override
|
||||
FixKind get kind => DartFixKind.REMOVE_TYPE_CHECK;
|
||||
|
||||
Future<void> test_conditional_expression_alwaysFalse() async {
|
||||
await resolveTestCode('''
|
||||
void f(int x) {
|
||||
print(x is! int ? 1 : 0);
|
||||
}
|
||||
''');
|
||||
await assertHasFix('''
|
||||
void f(int x) {
|
||||
print(0);
|
||||
}
|
||||
''', errorFilter: _ignoreDeadCode);
|
||||
}
|
||||
|
||||
Future<void> test_conditional_expression_alwaysTrue() async {
|
||||
await resolveTestCode('''
|
||||
void f(int x) {
|
||||
print(x is int ? 1 : 0);
|
||||
}
|
||||
''');
|
||||
await assertHasFix('''
|
||||
void f(int x) {
|
||||
print(1);
|
||||
}
|
||||
''', errorFilter: _ignoreDeadCode);
|
||||
}
|
||||
|
||||
Future<void> test_unnecessaryTypeCheck_false() async {
|
||||
await resolveTestCode('''
|
||||
void f(int a, int b) {
|
||||
|
||||
Reference in New Issue
Block a user