Skip to content

Commit ab004f3

Browse files
fshcheglovCommit Queue
authored andcommitted
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 <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 062af9f commit ab004f3

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ class RemoveComparison extends ResolvedCorrectionProducer {
8585
await _ifElement(parent, builder);
8686
} else if (parent is IfStatement) {
8787
await _ifStatement(parent, builder);
88+
} else if (parent is ConditionalExpression) {
89+
await _conditionalExpression(parent, builder);
8890
}
8991
}
9092

@@ -111,6 +113,24 @@ class RemoveComparison extends ResolvedCorrectionProducer {
111113
return buffer.toString();
112114
}
113115

116+
Future<void> _conditionalExpression(
117+
ConditionalExpression node,
118+
ChangeBuilder builder,
119+
) async {
120+
Future<void> replaceWithExpression(Expression expression) async {
121+
var text = utils.getNodeText(expression);
122+
await builder.addDartFileEdit(file, (builder) {
123+
builder.addSimpleReplacement(range.node(node), text);
124+
});
125+
}
126+
127+
if (_conditionIsTrue) {
128+
await replaceWithExpression(node.thenExpression);
129+
} else if (_conditionIsFalse) {
130+
await replaceWithExpression(node.elseExpression);
131+
}
132+
}
133+
114134
Future<void> _ifElement(IfElement node, ChangeBuilder builder) async {
115135
Future<void> replaceWithElement(CollectionElement element) async {
116136
var text = _textWithLeadingComments(element);

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,32 @@ void f(String s) {
155155
''');
156156
}
157157

158+
Future<void> test_conditional_expression_alwaysFalse() async {
159+
await resolveTestCode('''
160+
void f(int x) {
161+
print(x == null ? 1 : 0);
162+
}
163+
''');
164+
await assertHasFix('''
165+
void f(int x) {
166+
print(0);
167+
}
168+
''', errorFilter: _ignoreDeadCode);
169+
}
170+
171+
Future<void> test_conditional_expression_alwaysTrue() async {
172+
await resolveTestCode('''
173+
void f(int x) {
174+
print(x != null ? 1 : 0);
175+
}
176+
''');
177+
await assertHasFix('''
178+
void f(int x) {
179+
print(1);
180+
}
181+
''', errorFilter: _ignoreDeadCode);
182+
}
183+
158184
Future<void> test_ifElement_alwaysFalse_hasElse() async {
159185
await resolveTestCode('''
160186
void f(int x) {
@@ -789,6 +815,32 @@ class RemoveTypeCheckTest extends FixProcessorTest {
789815
@override
790816
FixKind get kind => DartFixKind.REMOVE_TYPE_CHECK;
791817

818+
Future<void> test_conditional_expression_alwaysFalse() async {
819+
await resolveTestCode('''
820+
void f(int x) {
821+
print(x is! int ? 1 : 0);
822+
}
823+
''');
824+
await assertHasFix('''
825+
void f(int x) {
826+
print(0);
827+
}
828+
''', errorFilter: _ignoreDeadCode);
829+
}
830+
831+
Future<void> test_conditional_expression_alwaysTrue() async {
832+
await resolveTestCode('''
833+
void f(int x) {
834+
print(x is int ? 1 : 0);
835+
}
836+
''');
837+
await assertHasFix('''
838+
void f(int x) {
839+
print(1);
840+
}
841+
''', errorFilter: _ignoreDeadCode);
842+
}
843+
792844
Future<void> test_unnecessaryTypeCheck_false() async {
793845
await resolveTestCode('''
794846
void f(int a, int b) {

0 commit comments

Comments
 (0)