Skip to content

Commit e76aafe

Browse files
author
shaark
committed
issue-182. fixed throw expression
1 parent 9dcdb64 commit e76aafe

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

lib/src/lints/prefer_early_return/visitors/prefer_early_return_visitor.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'package:analyzer/dart/ast/ast.dart';
22
import 'package:analyzer/dart/ast/visitor.dart';
33
import '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
67
class 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
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
}

lint_test/prefer_early_return_test.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
}

0 commit comments

Comments
 (0)