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

feat: support boolean literals removal for prefer-conditional-expressions auto-fix #1096

Merged
merged 1 commit into from
Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* feat: add static code diagnostic [`avoid-double-slash-imports`](https://dartcodemetrics.dev/docs/rules/common/avoid-double-slash-imports).
* feat: add static code diagnostic [`prefer-using-list-view`](https://dartcodemetrics.dev/docs/rules/flutter/prefer-using-list-view).
* feat: add static code diagnostic [`avoid-unnecessary-conditionals`](https://dartcodemetrics.dev/docs/rules/common/avoid-unnecessary-conditionals).
* feat: support boolean literals removal for [`prefer-conditional-expressions`](https://dartcodemetrics.dev/docs/rules/common/prefer-conditional-expressions) auto-fix.

## 5.1.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,34 @@ class PreferConditionalExpressionsRule extends CommonRule {
return _isAssignmentOperatorNotEq(thenStatementOperator) &&
_isAssignmentOperatorNotEq(elseStatementOperator)
? '$condition ? ${thenStatement.leftHandSide} ${thenStatementOperator.stringValue} $firstExpression : ${thenStatement.leftHandSide} ${elseStatementOperator.stringValue} $secondExpression;'
: '$target = $condition ? $firstExpression : $secondExpression;';
: '$target = ${_createCorrectionForLiterals(condition, firstExpression, secondExpression)}';
}

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

return 'return $condition ? $firstExpression : $secondExpression;';
return 'return ${_createCorrectionForLiterals(condition, firstExpression, secondExpression)}';
}

return null;
}

String _createCorrectionForLiterals(
Expression condition,
Expression? firstExpression,
Expression? secondExpression,
) {
if (firstExpression is BooleanLiteral &&
secondExpression is BooleanLiteral) {
final isInverted = !firstExpression.value && secondExpression.value;

return '${isInverted ? "!" : ""}$condition;';
}

return '$condition ? $firstExpression : $secondExpression;';
}

bool _isAssignmentOperatorNotEq(TokenType token) =>
token.isAssignmentOperator && token != TokenType.EQ;
}
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,12 @@ int newCase() {
} else {
value /= 5;
}

// LINT
bool val = false;
if (true) {
val = true;
} else {
val = false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,8 @@ void main() {

RuleTestHelper.verifyIssues(
issues: issues,
startLines: [
11,
30,
45,
51,
58,
64,
93,
109,
150,
157,
164,
171,
],
startColumns: [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3],
startLines: [11, 30, 45, 51, 58, 64, 93, 109, 150, 157, 164, 171, 179],
startColumns: [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3],
locationTexts: [
'if (a == 3) {\n'
' a = 2;\n'
Expand Down Expand Up @@ -97,6 +84,11 @@ void main() {
' } else {\n'
' value /= 5;\n'
' }',
'if (true) {\n'
' val = true;\n'
' } else {\n'
' val = false;\n'
' }',
],
messages: [
'Prefer conditional expression.',
Expand All @@ -111,6 +103,7 @@ void main() {
'Prefer conditional expression.',
'Prefer conditional expression.',
'Prefer conditional expression.',
'Prefer conditional expression.',
],
replacements: [
'a = a == 3 ? 2 : 3;',
Expand All @@ -125,6 +118,7 @@ void main() {
'cond ? value -= delta : value += delta;',
'cond ? value -= 2 : value += 5;',
'cond ? value *= 2 : value /= 5;',
'val = true;',
],
replacementComments: [
'Convert to conditional expression.',
Expand All @@ -139,6 +133,7 @@ void main() {
'Convert to conditional expression.',
'Convert to conditional expression.',
'Convert to conditional expression.',
'Convert to conditional expression.',
],
);
});
Expand Down