Skip to content

Commit 5390736

Browse files
committed
Issue #92: fixed formatting with 'dart format'
1 parent 477ae52 commit 5390736

File tree

3 files changed

+54
-63
lines changed

3 files changed

+54
-63
lines changed

lib/src/lints/dont_create_a_return_var/dont_create_a_return_var_rule.dart

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ import 'package:solid_lints/src/models/solid_lint_rule.dart';
1414
/// ### Example
1515
///
1616
/// #### BAD:
17-
///
17+
///
1818
/// ```dart
1919
/// void x() {
2020
/// final y = 1;
21-
///
21+
///
2222
/// return y;
2323
/// }
2424
/// ```
25-
///
25+
///
2626
/// #### GOOD:
27-
///
27+
///
2828
/// ```dart
2929
/// void x() {
3030
/// return 1;
@@ -35,7 +35,7 @@ class DontCreateAReturnVarRule extends SolidLintRule {
3535
/// This lint rule represents the error
3636
/// when unnecessary return variable statement is found
3737
static const lintName = 'dont_create_a_return_var';
38-
38+
3939
DontCreateAReturnVarRule._(super.config);
4040

4141
/// Creates a new instance of [DontCreateAReturnVarRule]
@@ -52,7 +52,6 @@ Rewrite the variable evaluation into return statement instead.""",
5252
return DontCreateAReturnVarRule._(rule);
5353
}
5454

55-
5655
@override
5756
void run(
5857
CustomLintResolver resolver,
@@ -79,16 +78,18 @@ Rewrite the variable evaluation into return statement instead.""",
7978
if (expr is! SimpleIdentifier) return;
8079

8180
final element = expr.element;
82-
if(element is! LocalVariableElement2) return;
81+
if (element is! LocalVariableElement2) return;
8382
final returnVariableElement = element;
8483

85-
if(!returnVariableElement.isFinal && !returnVariableElement.isConst) return;
86-
84+
if (!returnVariableElement.isFinal && !returnVariableElement.isConst) {
85+
return;
86+
}
87+
8788
final blockBody = statement.parent;
8889
if (blockBody == null) return;
8990

9091
final visitor = DontCreateAReturnVarVisitor(
91-
returnVariableElement,
92+
returnVariableElement,
9293
statement,
9394
);
9495
blockBody.visitChildren(visitor);
@@ -102,7 +103,7 @@ Rewrite the variable evaluation into return statement instead.""",
102103
}
103104

104105
final declaration = visitor.variableDeclaration;
105-
106+
106107
//check if immutable
107108
final initializer = declaration?.initializer;
108109
if (initializer == null) return;
@@ -111,20 +112,18 @@ Rewrite the variable evaluation into return statement instead.""",
111112

112113
reporter.atNode(statement, code);
113114
}
114-
115+
115116
bool _isExpressionImmutable(Expression expr) {
116117
if (expr is Literal) return true;
117118

118119
if (expr case final PrefixedIdentifier prefixed) {
119-
return
120-
_isExpressionImmutable(prefixed.prefix)
121-
&& _isExpressionImmutable(prefixed.identifier);
120+
return _isExpressionImmutable(prefixed.prefix) &&
121+
_isExpressionImmutable(prefixed.identifier);
122122
}
123123

124124
if (expr case final BinaryExpression binExpr) {
125-
return
126-
_isExpressionImmutable(binExpr.leftOperand)
127-
&& _isExpressionImmutable(binExpr.rightOperand);
125+
return _isExpressionImmutable(binExpr.leftOperand) &&
126+
_isExpressionImmutable(binExpr.rightOperand);
128127
}
129128

130129
if (expr case final SimpleIdentifier identifier) {
@@ -135,20 +134,16 @@ Rewrite the variable evaluation into return statement instead.""",
135134
}
136135

137136
bool _isSimpleIdentifierImmutable(SimpleIdentifier identifier) {
138-
if (identifier.element
139-
case final VariableElement2 variable
140-
when variable.isFinal || variable.isConst
141-
) {
142-
return true;
137+
if (identifier.element case final VariableElement2 variable
138+
when variable.isFinal || variable.isConst) {
139+
return true;
143140
}
144141

145142
if (identifier.element is ClassElement2) return true;
146-
143+
147144
if (identifier.element case final GetterElement getter) {
148-
if (getter.variable3
149-
case final PropertyInducingElement2 property
150-
when property.isFinal || property.isConst
151-
) {
145+
if (getter.variable3 case final PropertyInducingElement2 property
146+
when property.isFinal || property.isConst) {
152147
return true;
153148
}
154149
}

lib/src/lints/dont_create_a_return_var/visitors/dont_create_a_return_var_visitor.dart

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,26 @@ class DontCreateAReturnVarVisitor extends RecursiveAstVisitor<void> {
1717
bool _foundTokensBetweenDeclarationAndReturn = false;
1818
VariableDeclaration? _variableDeclaration;
1919
int _variableStatementCounter = 0;
20-
20+
2121
final ReturnStatement _returnStatement;
2222

2323
/// After visiting holds info about whether there are any tokens
2424
/// between variable declaration and return statement
2525
bool get foundTokensBetweenDeclarationAndReturn =>
26-
_foundTokensBetweenDeclarationAndReturn;
26+
_foundTokensBetweenDeclarationAndReturn;
27+
2728
/// Returns statement of local variable declaration
28-
VariableDeclaration? get variableDeclaration
29-
=> _variableDeclaration;
29+
VariableDeclaration? get variableDeclaration => _variableDeclaration;
3030

3131
/// Creates a new instance of [DontCreateAReturnVarVisitor].
3232
DontCreateAReturnVarVisitor(
33-
this._returnVariableElement,
33+
this._returnVariableElement,
3434
this._returnStatement,
3535
);
3636

3737
/// Defines whether the variables is used in return statement only.
3838
bool hasBadStatementCount() =>
39-
_variableStatementCounter == _badStatementCount;
39+
_variableStatementCounter == _badStatementCount;
4040

4141
@override
4242
void visitVariableDeclarationStatement(VariableDeclarationStatement node) {
@@ -48,34 +48,31 @@ class DontCreateAReturnVarVisitor extends RecursiveAstVisitor<void> {
4848

4949
@override
5050
void visitSimpleIdentifier(SimpleIdentifier node) {
51-
if(node.element?.id == _returnVariableElement.id) {
51+
if (node.element?.id == _returnVariableElement.id) {
5252
_variableStatementCounter++;
5353
}
5454

5555
super.visitSimpleIdentifier(node);
5656
}
57-
57+
5858
bool _collectVariableDeclaration(VariableDeclarationStatement node) {
59-
final targetVariable =
60-
node.variables.variables
61-
.firstWhereOrNull(
62-
(v)
63-
=> v.declaredElement2?.id == _returnVariableElement.id,
64-
);
59+
final targetVariable = node.variables.variables.firstWhereOrNull(
60+
(v) => v.declaredElement2?.id == _returnVariableElement.id,
61+
);
6562
if (targetVariable == null) return false;
6663

6764
_variableDeclaration = targetVariable;
6865
return true;
6966
}
70-
67+
7168
void _checkTokensInBetween(
72-
VariableDeclarationStatement variableDeclaration,
69+
VariableDeclarationStatement variableDeclaration,
7370
ReturnStatement returnStatement,
7471
) {
75-
final tokenBeforeReturn =
76-
_returnStatement.findPrevious(_returnStatement.beginToken);
77-
78-
if(tokenBeforeReturn != variableDeclaration.endToken) {
72+
final tokenBeforeReturn =
73+
_returnStatement.findPrevious(_returnStatement.beginToken);
74+
75+
if (tokenBeforeReturn != variableDeclaration.endToken) {
7976
_foundTokensBetweenDeclarationAndReturn = true;
8077
}
8178
}

lint_test/dont_create_a_return_var_test.dart

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ int returnVarTestCachedMutable() {
2727
var a = 1;
2828
final result = a;
2929
_doNothing();
30-
30+
3131
return result;
3232
}
3333

@@ -39,7 +39,7 @@ int returnVarTestReturnFollowsDeclaration() {
3939
final result = a;
4040

4141
//Some comment here
42-
42+
4343
//expect_lint: dont_create_a_return_var
4444
return result;
4545
}
@@ -52,7 +52,7 @@ int returnVarTestCachedAnotherMethodResult() {
5252
var a = 1;
5353
final result = _testValueEval();
5454
_doNothing();
55-
55+
5656
return result;
5757
}
5858

@@ -64,7 +64,7 @@ int returnVarTestCachedObjectField() {
6464
final obj = _TestClass();
6565
final result = obj.varField;
6666
_doNothing();
67-
67+
6868
return result;
6969
}
7070

@@ -75,15 +75,15 @@ int returnVarTestUsedVariable() {
7575
var a = 1;
7676
final result = 2;
7777
a += result;
78-
78+
7979
return result;
8080
}
8181

8282
/// Test the dont_create_a_return_var.
8383
/// Bad code, trivial example.
8484
int returnVarTestBadTrivial() {
8585
final result = 1;
86-
86+
8787
//expect_lint: dont_create_a_return_var
8888
return result;
8989
}
@@ -95,13 +95,12 @@ int returnVarTestBadImmutableExpression() {
9595
const constLocal = 1;
9696
final finalLocal = 1;
9797
final testObj = _TestClass();
98-
final result =
99-
constLocal
100-
+ finalLocal
101-
+ 1 //const literal
102-
+ _TestClass.constValue
103-
+ _TestClass.finalValue
104-
+ testObj.finalField;
98+
final result = constLocal +
99+
finalLocal +
100+
1 + //const literal
101+
_TestClass.constValue +
102+
_TestClass.finalValue +
103+
testObj.finalField;
105104
_doNothing();
106105

107106
//expect_lint: dont_create_a_return_var
@@ -114,7 +113,7 @@ int _testValueEval() {
114113

115114
/// This method is a placeholder for unpredictable behaviour
116115
/// which can potentially change any mutable variables
117-
void _doNothing() { }
116+
void _doNothing() {}
118117

119118
//ignore: prefer_match_file_name
120119
class _TestClass {
@@ -123,4 +122,4 @@ class _TestClass {
123122
//ignore: member_ordering
124123
final finalField = 1;
125124
var varField = 1;
126-
}
125+
}

0 commit comments

Comments
 (0)