File tree Expand file tree Collapse file tree 4 files changed +90
-0
lines changed
lib/src/lints/prefer_early_return/visitors Expand file tree Collapse file tree 4 files changed +90
-0
lines changed Original file line number Diff line number Diff line change 1+ ## 0.2.4
2+
3+ - Fixed an issue with ` prefer_early_retrun ` for throw expression
4+
15## 0.2.3
26
37- Replace deprecated whereNotNull()
Original file line number Diff line number Diff line change 11import 'package:analyzer/dart/ast/ast.dart' ;
22import 'package:analyzer/dart/ast/visitor.dart' ;
33import 'package:solid_lints/src/lints/prefer_early_return/visitors/return_statement_visitor.dart' ;
4+ import 'package:solid_lints/src/lints/prefer_early_return/visitors/throw_expression_visitor.dart' ;
45
56/// The AST visitor that will collect all unnecessary if statements
67class PreferEarlyReturnVisitor extends RecursiveAstVisitor <void > {
@@ -33,6 +34,7 @@ class PreferEarlyReturnVisitor extends RecursiveAstVisitor<void> {
3334 if (_isElseIfStatement (node)) return ;
3435 if (_hasElseStatement (node)) return ;
3536 if (_hasReturnStatement (node)) return ;
37+ if (_hasThrowExpression (node)) return ;
3638
3739 _nodes.add (node);
3840 }
@@ -70,4 +72,10 @@ class PreferEarlyReturnVisitor extends RecursiveAstVisitor<void> {
7072 node.accept (visitor);
7173 return visitor.nodes.isNotEmpty;
7274 }
75+
76+ bool _hasThrowExpression (Statement node) {
77+ final visitor = ThrowExpressionVisitor ();
78+ node.accept (visitor);
79+ return visitor.nodes.isNotEmpty;
80+ }
7381}
Original file line number Diff line number Diff line change 1+ import 'package:analyzer/dart/ast/ast.dart' ;
2+ import 'package:analyzer/dart/ast/visitor.dart' ;
3+
4+ /// The AST visitor that will collect every Return statement
5+ class ThrowExpressionVisitor extends RecursiveAstVisitor <void > {
6+ final _nodes = < ThrowExpression > [];
7+
8+ /// All unnecessary return statements
9+ Iterable <ThrowExpression > get nodes => _nodes;
10+
11+ @override
12+ void visitThrowExpression (ThrowExpression node) {
13+ super .visitThrowExpression (node);
14+ _nodes.add (node);
15+ }
16+ }
Original file line number Diff line number Diff line change @@ -235,3 +235,65 @@ void threeSeqentialIfReturn2() {
235235 _doSomething ();
236236 }
237237}
238+
239+ void oneIfWithThrowWithReturn () {
240+ //no lint
241+ if (true ) {
242+ throw '' ;
243+ }
244+
245+ return ;
246+ }
247+
248+ void oneIfElseWithThrowReturn () {
249+ //no lint
250+ if (true ) {
251+ _doSomething ();
252+ } else {
253+ throw '' ;
254+ }
255+ }
256+
257+ void twoSeqentialIfWithThrow () {
258+ if (false ) throw '' ;
259+ //expect_lint: prefer_early_return
260+ if (true ) {
261+ _doSomething ();
262+ }
263+ }
264+
265+ void twoSeqentialIfWithThrowReturn2 () {
266+ //no lint
267+ if (false ) throw '' ;
268+ //expect_lint: prefer_early_return
269+ if (true ) {
270+ _doSomething ();
271+ }
272+
273+ return ;
274+ }
275+
276+ void threeSeqentialIfWithThrowReturn () {
277+ //no lint
278+ if (false ) throw '' ;
279+ if (true ) throw '' ;
280+ //expect_lint: prefer_early_return
281+ if (true ) {
282+ _doSomething ();
283+ }
284+
285+ return ;
286+ }
287+
288+ void threeSeqentialIfWithThrowReturn2 () {
289+ //no lint
290+ if (false ) throw '' ;
291+ //no lint
292+ if (true ) {
293+ _doSomething ();
294+ }
295+ //expect_lint: prefer_early_return
296+ if (true ) {
297+ _doSomething ();
298+ }
299+ }
You can’t perform that action at this time.
0 commit comments