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

fix: prefer conditional expressions rule #567

Merged
merged 10 commits into from
Nov 25, 2021
Merged
Show file tree
Hide file tree
Changes from 7 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased

* fix: prefer conditional expressions rule breaks code with increment / decrement operators

## 4.7.0

* feat: add static code diagnostics `avoid-throw-in-catch-block`, `avoid-unnecessary-type-assertions`, `avoid-unnecessary-type-casts`, `avoid-missing-enum-constant-in-map`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// ignore_for_file: public_member_api_docs

import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/token.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:meta/meta.dart';

Expand Down Expand Up @@ -72,7 +73,13 @@ class PreferConditionalExpressionsRule extends CommonRule {
final firstExpression = thenStatement.rightHandSide;
final secondExpression = elseStatement.rightHandSide;

return '$target = $condition ? $firstExpression : $secondExpression;';
final thenStatementOperator = thenStatement.operator.type;
final elseStatementOperator = elseStatement.operator.type;

return _isAssignmentOperatorNotEq(thenStatementOperator) &&
_isAssignmentOperatorNotEq(elseStatementOperator)
? '$condition ? ${thenStatement.leftHandSide} ${thenStatementOperator.stringValue} $firstExpression : ${thenStatement.leftHandSide} ${elseStatementOperator.stringValue} $secondExpression;'
: '$target = $condition ? $firstExpression : $secondExpression;';
}

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

return null;
}

bool _isAssignmentOperatorNotEq(TokenType token) =>
token.isAssignmentOperator && token != TokenType.EQ;
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,33 @@ int anotherTestFunction() {
a = 6;
}
}

int newCase() {
final cond = false;
final delta = 1;
final value = 9;
// LINT
if (cond) {
value += delta;
} else {
value -= delta;
}
// LINT
if (cond) {
value -= delta;
} else {
value += delta;
}
//LINT
vlkonoshenko marked this conversation as resolved.
Show resolved Hide resolved
if (cond) {
value -= 2;
} else {
value += 5;
}
//LINT
vlkonoshenko marked this conversation as resolved.
Show resolved Hide resolved
if (cond) {
value *= 2;
} else {
value /= 5;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,49 @@ void main() {

RuleTestHelper.verifyIssues(
issues: issues,
startOffsets: [111, 289, 426, 490, 557, 616, 889, 1040],
startLines: [11, 30, 45, 51, 58, 64, 93, 109],
startColumns: [3, 3, 3, 3, 3, 3, 3, 3],
endOffsets: [161, 345, 476, 543, 602, 663, 930, 1087],
startOffsets: [
111,
289,
426,
490,
557,
616,
889,
1040,
1496,
1575,
1653,
1723,
],
startLines: [
11,
30,
45,
51,
58,
64,
93,
109,
149,
155,
161,
167,
],
startColumns: [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3],
endOffsets: [
161,
345,
476,
543,
602,
663,
930,
1087,
1562,
1641,
1711,
1781,
],
locationTexts: [
'if (a == 3) {\n'
' a = 2;\n'
Expand Down Expand Up @@ -67,6 +106,26 @@ void main() {
' return 2;\n'
' else\n'
' return a;',
'if (cond) {\n'
' value += delta;\n'
' } else {\n'
' value -= delta;\n'
' }',
'if (cond) {\n'
' value -= delta;\n'
' } else {\n'
' value += delta;\n'
' }',
'if (cond) {\n'
' value -= 2;\n'
' } else {\n'
' value += 5;\n'
' }',
'if (cond) {\n'
' value *= 2;\n'
' } else {\n'
' value /= 5;\n'
' }',
],
messages: [
'Prefer conditional expression.',
Expand All @@ -77,6 +136,10 @@ void main() {
'Prefer conditional expression.',
'Prefer conditional expression.',
'Prefer conditional expression.',
'Prefer conditional expression.',
'Prefer conditional expression.',
'Prefer conditional expression.',
'Prefer conditional expression.',
],
replacements: [
'a = a == 3 ? 2 : 3;',
Expand All @@ -87,6 +150,10 @@ void main() {
'a = a == 12 ? 2 : 3;',
'a = a == 17 ? 2 : 3;',
'return a == 20 ? 2 : a;',
'cond ? value += delta : value -= delta;',
'cond ? value -= delta : value += delta;',
'cond ? value -= 2 : value += 5;',
'cond ? value *= 2 : value /= 5;',
],
replacementComments: [
'Convert to conditional expression.',
Expand All @@ -97,6 +164,10 @@ void main() {
'Convert to conditional expression.',
'Convert to conditional expression.',
'Convert to conditional expression.',
'Convert to conditional expression.',
'Convert to conditional expression.',
'Convert to conditional expression.',
'Convert to conditional expression.',
],
);
});
Expand Down