Skip to content
This repository was archived by the owner on Jul 16, 2023. It is now read-only.

Commit 490e032

Browse files
author
Konoshenko Vlad
authored
fix: prefer conditional expressions rule (#567)
* fix: prefer conditional expressions rule breaks code with increment / decrement operators * Added new operators * formatted code * fix compare by type * fix compare by type * fix compare by type * review changes * review changes
1 parent 70bdf50 commit 490e032

File tree

4 files changed

+121
-5
lines changed

4 files changed

+121
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Unreleased
44

55
* feat: add alphabetical sorting by type for `member-ordering-extended` rule.
6+
* fix: prefer conditional expressions rule breaks code with increment / decrement operators
67

78
## 4.7.0
89

lib/src/analyzers/lint_analyzer/rules/rules_list/prefer_conditional_expressions/prefer_conditional_expressions_rule.dart

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// ignore_for_file: public_member_api_docs
22

33
import 'package:analyzer/dart/ast/ast.dart';
4+
import 'package:analyzer/dart/ast/token.dart';
45
import 'package:analyzer/dart/ast/visitor.dart';
56
import 'package:meta/meta.dart';
67

@@ -72,7 +73,13 @@ class PreferConditionalExpressionsRule extends CommonRule {
7273
final firstExpression = thenStatement.rightHandSide;
7374
final secondExpression = elseStatement.rightHandSide;
7475

75-
return '$target = $condition ? $firstExpression : $secondExpression;';
76+
final thenStatementOperator = thenStatement.operator.type;
77+
final elseStatementOperator = elseStatement.operator.type;
78+
79+
return _isAssignmentOperatorNotEq(thenStatementOperator) &&
80+
_isAssignmentOperatorNotEq(elseStatementOperator)
81+
? '$condition ? ${thenStatement.leftHandSide} ${thenStatementOperator.stringValue} $firstExpression : ${thenStatement.leftHandSide} ${elseStatementOperator.stringValue} $secondExpression;'
82+
: '$target = $condition ? $firstExpression : $secondExpression;';
7683
}
7784

7885
if (thenStatement is ReturnStatement && elseStatement is ReturnStatement) {
@@ -84,4 +91,7 @@ class PreferConditionalExpressionsRule extends CommonRule {
8491

8592
return null;
8693
}
94+
95+
bool _isAssignmentOperatorNotEq(TokenType token) =>
96+
token.isAssignmentOperator && token != TokenType.EQ;
8797
}

test/src/analyzers/lint_analyzer/rules/rules_list/prefer_conditional_expressions/examples/example.dart

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,37 @@ int anotherTestFunction() {
140140
a = 6;
141141
}
142142
}
143+
144+
int newCase() {
145+
final cond = false;
146+
final delta = 1;
147+
final value = 9;
148+
149+
// LINT
150+
if (cond) {
151+
value += delta;
152+
} else {
153+
value -= delta;
154+
}
155+
156+
// LINT
157+
if (cond) {
158+
value -= delta;
159+
} else {
160+
value += delta;
161+
}
162+
163+
// LINT
164+
if (cond) {
165+
value -= 2;
166+
} else {
167+
value += 5;
168+
}
169+
170+
// LINT
171+
if (cond) {
172+
value *= 2;
173+
} else {
174+
value /= 5;
175+
}
176+
}

test/src/analyzers/lint_analyzer/rules/rules_list/prefer_conditional_expressions/prefer_conditional_expressions_rule_test.dart

Lines changed: 75 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,49 @@ void main() {
2626

2727
RuleTestHelper.verifyIssues(
2828
issues: issues,
29-
startOffsets: [111, 289, 426, 490, 557, 616, 889, 1040],
30-
startLines: [11, 30, 45, 51, 58, 64, 93, 109],
31-
startColumns: [3, 3, 3, 3, 3, 3, 3, 3],
32-
endOffsets: [161, 345, 476, 543, 602, 663, 930, 1087],
29+
startOffsets: [
30+
111,
31+
289,
32+
426,
33+
490,
34+
557,
35+
616,
36+
889,
37+
1040,
38+
1497,
39+
1577,
40+
1657,
41+
1729,
42+
],
43+
startLines: [
44+
11,
45+
30,
46+
45,
47+
51,
48+
58,
49+
64,
50+
93,
51+
109,
52+
150,
53+
157,
54+
164,
55+
171,
56+
],
57+
startColumns: [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3],
58+
endOffsets: [
59+
161,
60+
345,
61+
476,
62+
543,
63+
602,
64+
663,
65+
930,
66+
1087,
67+
1563,
68+
1643,
69+
1715,
70+
1787,
71+
],
3372
locationTexts: [
3473
'if (a == 3) {\n'
3574
' a = 2;\n'
@@ -67,6 +106,26 @@ void main() {
67106
' return 2;\n'
68107
' else\n'
69108
' return a;',
109+
'if (cond) {\n'
110+
' value += delta;\n'
111+
' } else {\n'
112+
' value -= delta;\n'
113+
' }',
114+
'if (cond) {\n'
115+
' value -= delta;\n'
116+
' } else {\n'
117+
' value += delta;\n'
118+
' }',
119+
'if (cond) {\n'
120+
' value -= 2;\n'
121+
' } else {\n'
122+
' value += 5;\n'
123+
' }',
124+
'if (cond) {\n'
125+
' value *= 2;\n'
126+
' } else {\n'
127+
' value /= 5;\n'
128+
' }',
70129
],
71130
messages: [
72131
'Prefer conditional expression.',
@@ -77,6 +136,10 @@ void main() {
77136
'Prefer conditional expression.',
78137
'Prefer conditional expression.',
79138
'Prefer conditional expression.',
139+
'Prefer conditional expression.',
140+
'Prefer conditional expression.',
141+
'Prefer conditional expression.',
142+
'Prefer conditional expression.',
80143
],
81144
replacements: [
82145
'a = a == 3 ? 2 : 3;',
@@ -87,6 +150,10 @@ void main() {
87150
'a = a == 12 ? 2 : 3;',
88151
'a = a == 17 ? 2 : 3;',
89152
'return a == 20 ? 2 : a;',
153+
'cond ? value += delta : value -= delta;',
154+
'cond ? value -= delta : value += delta;',
155+
'cond ? value -= 2 : value += 5;',
156+
'cond ? value *= 2 : value /= 5;',
90157
],
91158
replacementComments: [
92159
'Convert to conditional expression.',
@@ -97,6 +164,10 @@ void main() {
97164
'Convert to conditional expression.',
98165
'Convert to conditional expression.',
99166
'Convert to conditional expression.',
167+
'Convert to conditional expression.',
168+
'Convert to conditional expression.',
169+
'Convert to conditional expression.',
170+
'Convert to conditional expression.',
100171
],
101172
);
102173
});

0 commit comments

Comments
 (0)