File tree Expand file tree Collapse file tree 3 files changed +33
-0
lines changed
lib/src/lints/prefer_early_return/visitors Expand file tree Collapse file tree 3 files changed +33
-0
lines changed 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,12 @@ void threeSeqentialIfReturn2() {
235235 _doSomething ();
236236 }
237237}
238+
239+ void throwExpression () {
240+ //no lint
241+ if (true ) {
242+ throw '' ;
243+ }
244+
245+ return ;
246+ }
You can’t perform that action at this time.
0 commit comments