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

Commit 2111ddd

Browse files
committed
test: known issues, config
1 parent 75d8762 commit 2111ddd

File tree

5 files changed

+65
-6
lines changed

5 files changed

+65
-6
lines changed
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
part of 'use_setstate_synchronously_rule.dart';
22

33
Set<String> readMethods(Map<String, Object> options) {
4-
final fromConfig = options['methods'];
4+
final methods = options['methods'];
55

6-
return fromConfig is List
7-
? fromConfig.whereType<String>().toSet()
6+
return methods != null && methods is Iterable
7+
? methods.whereType<String>().toSet()
88
: {'setState'};
99
}

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

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class _Visitor extends RecursiveAstVisitor<void> {
1616
@override
1717
void visitBlockFunctionBody(BlockFunctionBody node) {
1818
if (!node.isAsynchronous) {
19-
return super.visitBlockFunctionBody(node);
19+
return node.visitChildren(this);
2020
}
2121
final visitor = _AsyncSetStateVisitor(validateMethod: methods.contains);
2222
node.visitChildren(visitor);
@@ -31,6 +31,7 @@ class _AsyncSetStateVisitor extends RecursiveAstVisitor<void> {
3131
_AsyncSetStateVisitor({this.validateMethod = _noop});
3232

3333
MountedFact mounted = true.asFact();
34+
bool inAsync = true;
3435
final nodes = <SimpleIdentifier>[];
3536

3637
@override
@@ -41,11 +42,15 @@ class _AsyncSetStateVisitor extends RecursiveAstVisitor<void> {
4142

4243
@override
4344
void visitMethodInvocation(MethodInvocation node) {
45+
if (!inAsync) {
46+
return node.visitChildren(this);
47+
}
48+
4449
// [this.]setState()
4550
final mounted_ = mounted.value ?? false;
4651
if (!mounted_ &&
47-
(node.target is ThisExpression?) &&
48-
validateMethod(node.methodName.name)) {
52+
validateMethod(node.methodName.name) &&
53+
node.target is ThisExpression?) {
4954
nodes.add(node.methodName);
5055
}
5156

@@ -54,6 +59,10 @@ class _AsyncSetStateVisitor extends RecursiveAstVisitor<void> {
5459

5560
@override
5661
void visitIfStatement(IfStatement node) {
62+
if (!inAsync) {
63+
return node.visitChildren(this);
64+
}
65+
5766
node.condition.visitChildren(this);
5867
final oldMounted = mounted;
5968
final newMounted = _extractMountedCheck(node.condition);
@@ -84,6 +93,10 @@ class _AsyncSetStateVisitor extends RecursiveAstVisitor<void> {
8493

8594
@override
8695
void visitWhileStatement(WhileStatement node) {
96+
if (!inAsync) {
97+
return node.visitChildren(this);
98+
}
99+
87100
node.condition.visitChildren(this);
88101
final oldMounted = mounted;
89102
final newMounted = _extractMountedCheck(node.condition);
@@ -92,4 +105,17 @@ class _AsyncSetStateVisitor extends RecursiveAstVisitor<void> {
92105

93106
mounted = _blockDiverges(node.body) ? _tryInvert(newMounted) : oldMounted;
94107
}
108+
109+
@override
110+
void visitBlockFunctionBody(BlockFunctionBody node) {
111+
final oldMounted = mounted;
112+
final oldInAsync = inAsync;
113+
mounted = true.asFact();
114+
inAsync = node.isAsynchronous;
115+
116+
node.visitChildren(this);
117+
118+
mounted = oldMounted;
119+
inAsync = oldInAsync;
120+
}
95121
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ class _FooState extends State<StatefulWidget> {
7575
},
7676
);
7777
}
78+
79+
void customConfig() async {
80+
await fetch();
81+
foobar(); // LINT
82+
this.foobar(); // LINT
83+
}
7884
}
7985

8086
class State {}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ class _MyState extends State {
33
await Future<void>.value();
44
setState(() {}); // LINT
55
}
6+
7+
void syncRegression() {
8+
doStuff();
9+
setState(() {});
10+
}
611
}
712

813
class State {}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,27 @@ void main() {
6767
],
6868
);
6969
});
70+
71+
test('reports issues with custom config', () async {
72+
final unit = await RuleTestHelper.resolveFromFile(_examplePath);
73+
final config = {
74+
'methods': ['foobar'],
75+
};
76+
final issues = UseSetStateSynchronouslyRule(config).check(unit);
77+
78+
RuleTestHelper.verifyIssues(
79+
issues: issues,
80+
startLines: [81, 82],
81+
startColumns: [5, 10],
82+
locationTexts: [
83+
'foobar',
84+
'foobar',
85+
],
86+
messages: [
87+
"Avoid calling 'foobar' past an await point without checking if the widget is mounted.",
88+
"Avoid calling 'foobar' past an await point without checking if the widget is mounted.",
89+
],
90+
);
91+
});
7092
});
7193
}

0 commit comments

Comments
 (0)