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

Commit 8b8508c

Browse files
committed
fix: handle await inside conditions
1 parent f3c1414 commit 8b8508c

File tree

4 files changed

+22
-7
lines changed

4 files changed

+22
-7
lines changed

lib/src/analyzers/lint_analyzer/rules/rules_list/use_setstate_synchronously/use_setstate_synchronously_rule.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ import '../../../models/severity.dart';
1111
import '../../models/flutter_rule.dart';
1212
import '../../rule_utils.dart';
1313

14+
part 'config.dart';
1415
part 'fact.dart';
1516
part 'helpers.dart';
16-
part 'config.dart';
1717
part 'visitor.dart';
1818

1919
class UseSetStateSynchronouslyRule extends FlutterRule {
2020
static const ruleId = 'use-setstate-synchronously';
2121

22-
Set<String> methods;
22+
final Set<String> methods;
2323

2424
UseSetStateSynchronouslyRule([Map<String, Object> options = const {}])
2525
: methods = readMethods(options),

lib/src/analyzers/lint_analyzer/rules/rules_list/use_setstate_synchronously/visitor.dart

+6-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ part of 'use_setstate_synchronously_rule.dart';
22

33
class _Visitor extends RecursiveAstVisitor<void> {
44
final Set<String> methods;
5+
56
_Visitor({required this.methods});
67

78
final nodes = <SimpleIdentifier>[];
@@ -72,6 +73,8 @@ class _AsyncSetStateVisitor extends RecursiveAstVisitor<void> {
7273
return node.visitChildren(this);
7374
}
7475

76+
node.condition.accept(this);
77+
7578
final newMounted = _extractMountedCheck(node.condition);
7679
mounted = newMounted.or(mounted);
7780

@@ -105,7 +108,7 @@ class _AsyncSetStateVisitor extends RecursiveAstVisitor<void> {
105108
return node.visitChildren(this);
106109
}
107110

108-
node.condition.visitChildren(this);
111+
node.condition.accept(this);
109112

110113
final oldMounted = mounted;
111114
final newMounted = _extractMountedCheck(node.condition);
@@ -127,7 +130,7 @@ class _AsyncSetStateVisitor extends RecursiveAstVisitor<void> {
127130
return node.visitChildren(this);
128131
}
129132

130-
node.forLoopParts.visitChildren(this);
133+
node.forLoopParts.accept(this);
131134

132135
final oldInControlFlow = inControlFlow;
133136
inControlFlow = true;
@@ -181,7 +184,7 @@ class _AsyncSetStateVisitor extends RecursiveAstVisitor<void> {
181184
return node.visitChildren(this);
182185
}
183186

184-
node.expression.visitChildren(this);
187+
node.expression.accept(this);
185188

186189
final oldInControlFlow = inControlFlow;
187190
inControlFlow = true;

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

+10
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,17 @@ class _FooState extends State<StatefulWidget> {
9595
await fetch();
9696
}
9797
setState(() {}); // LINT
98+
99+
if (!mounted) return;
100+
101+
if (await condition()) {
102+
setState(() {}); // LINT
103+
}
98104
}
99105
}
100106

101107
class State {}
108+
109+
Future<void> fetch() {}
110+
111+
Future<bool> condition() {}

test/src/analyzers/lint_analyzer/rules/rules_list/use_setstate_synchronously/use_setstate_synchronously_rule_test.dart

+4-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ void main() {
3232

3333
RuleTestHelper.verifyIssues(
3434
issues: issues,
35-
startLines: [7, 24, 29, 36, 51, 66, 70, 76, 82, 92, 97],
36-
startColumns: [9, 10, 7, 7, 5, 7, 5, 5, 5, 5, 5],
35+
startLines: [7, 24, 29, 36, 51, 66, 70, 76, 82, 92, 97, 102],
36+
startColumns: [9, 10, 7, 7, 5, 7, 5, 5, 5, 5, 5, 7],
3737
locationTexts: [
3838
'setState',
3939
'setState',
@@ -46,6 +46,7 @@ void main() {
4646
'setState',
4747
'setState',
4848
'setState',
49+
'setState',
4950
],
5051
messages: [
5152
"Avoid calling 'setState' past an await point without checking if the widget is mounted.",
@@ -59,6 +60,7 @@ void main() {
5960
"Avoid calling 'setState' past an await point without checking if the widget is mounted.",
6061
"Avoid calling 'setState' past an await point without checking if the widget is mounted.",
6162
"Avoid calling 'setState' past an await point without checking if the widget is mounted.",
63+
"Avoid calling 'setState' past an await point without checking if the widget is mounted.",
6264
],
6365
);
6466
});

0 commit comments

Comments
 (0)