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

Commit 9b1efee

Browse files
authored
feat: support boolean literals removal for prefer-conditional-expressions auto-fix (#1096)
1 parent e77069d commit 9b1efee

File tree

4 files changed

+36
-17
lines changed

4 files changed

+36
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* feat: add static code diagnostic [`avoid-double-slash-imports`](https://dartcodemetrics.dev/docs/rules/common/avoid-double-slash-imports).
77
* feat: add static code diagnostic [`prefer-using-list-view`](https://dartcodemetrics.dev/docs/rules/flutter/prefer-using-list-view).
88
* feat: add static code diagnostic [`avoid-unnecessary-conditionals`](https://dartcodemetrics.dev/docs/rules/common/avoid-unnecessary-conditionals).
9+
* feat: support boolean literals removal for [`prefer-conditional-expressions`](https://dartcodemetrics.dev/docs/rules/common/prefer-conditional-expressions) auto-fix.
910

1011
## 5.1.0
1112

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,19 +75,34 @@ class PreferConditionalExpressionsRule extends CommonRule {
7575
return _isAssignmentOperatorNotEq(thenStatementOperator) &&
7676
_isAssignmentOperatorNotEq(elseStatementOperator)
7777
? '$condition ? ${thenStatement.leftHandSide} ${thenStatementOperator.stringValue} $firstExpression : ${thenStatement.leftHandSide} ${elseStatementOperator.stringValue} $secondExpression;'
78-
: '$target = $condition ? $firstExpression : $secondExpression;';
78+
: '$target = ${_createCorrectionForLiterals(condition, firstExpression, secondExpression)}';
7979
}
8080

8181
if (thenStatement is ReturnStatement && elseStatement is ReturnStatement) {
8282
final firstExpression = thenStatement.expression;
8383
final secondExpression = elseStatement.expression;
8484

85-
return 'return $condition ? $firstExpression : $secondExpression;';
85+
return 'return ${_createCorrectionForLiterals(condition, firstExpression, secondExpression)}';
8686
}
8787

8888
return null;
8989
}
9090

91+
String _createCorrectionForLiterals(
92+
Expression condition,
93+
Expression? firstExpression,
94+
Expression? secondExpression,
95+
) {
96+
if (firstExpression is BooleanLiteral &&
97+
secondExpression is BooleanLiteral) {
98+
final isInverted = !firstExpression.value && secondExpression.value;
99+
100+
return '${isInverted ? "!" : ""}$condition;';
101+
}
102+
103+
return '$condition ? $firstExpression : $secondExpression;';
104+
}
105+
91106
bool _isAssignmentOperatorNotEq(TokenType token) =>
92107
token.isAssignmentOperator && token != TokenType.EQ;
93108
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,4 +173,12 @@ int newCase() {
173173
} else {
174174
value /= 5;
175175
}
176+
177+
// LINT
178+
bool val = false;
179+
if (true) {
180+
val = true;
181+
} else {
182+
val = false;
183+
}
176184
}

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

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,8 @@ void main() {
2525

2626
RuleTestHelper.verifyIssues(
2727
issues: issues,
28-
startLines: [
29-
11,
30-
30,
31-
45,
32-
51,
33-
58,
34-
64,
35-
93,
36-
109,
37-
150,
38-
157,
39-
164,
40-
171,
41-
],
42-
startColumns: [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3],
28+
startLines: [11, 30, 45, 51, 58, 64, 93, 109, 150, 157, 164, 171, 179],
29+
startColumns: [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3],
4330
locationTexts: [
4431
'if (a == 3) {\n'
4532
' a = 2;\n'
@@ -97,6 +84,11 @@ void main() {
9784
' } else {\n'
9885
' value /= 5;\n'
9986
' }',
87+
'if (true) {\n'
88+
' val = true;\n'
89+
' } else {\n'
90+
' val = false;\n'
91+
' }',
10092
],
10193
messages: [
10294
'Prefer conditional expression.',
@@ -111,6 +103,7 @@ void main() {
111103
'Prefer conditional expression.',
112104
'Prefer conditional expression.',
113105
'Prefer conditional expression.',
106+
'Prefer conditional expression.',
114107
],
115108
replacements: [
116109
'a = a == 3 ? 2 : 3;',
@@ -125,6 +118,7 @@ void main() {
125118
'cond ? value -= delta : value += delta;',
126119
'cond ? value -= 2 : value += 5;',
127120
'cond ? value *= 2 : value /= 5;',
121+
'val = true;',
128122
],
129123
replacementComments: [
130124
'Convert to conditional expression.',
@@ -139,6 +133,7 @@ void main() {
139133
'Convert to conditional expression.',
140134
'Convert to conditional expression.',
141135
'Convert to conditional expression.',
136+
'Convert to conditional expression.',
142137
],
143138
);
144139
});

0 commit comments

Comments
 (0)