diff --git a/CHANGELOG.md b/CHANGELOG.md index 31779dec..1f3d977e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.2.4 + +- Fixed an issue with `prefer_early_retrun` for throw expression + ## 0.2.3 - Replace deprecated whereNotNull() diff --git a/lib/src/lints/prefer_early_return/visitors/prefer_early_return_visitor.dart b/lib/src/lints/prefer_early_return/visitors/prefer_early_return_visitor.dart index cc82a1ec..127de6e8 100644 --- a/lib/src/lints/prefer_early_return/visitors/prefer_early_return_visitor.dart +++ b/lib/src/lints/prefer_early_return/visitors/prefer_early_return_visitor.dart @@ -1,6 +1,7 @@ import 'package:analyzer/dart/ast/ast.dart'; import 'package:analyzer/dart/ast/visitor.dart'; import 'package:solid_lints/src/lints/prefer_early_return/visitors/return_statement_visitor.dart'; +import 'package:solid_lints/src/lints/prefer_early_return/visitors/throw_expression_visitor.dart'; /// The AST visitor that will collect all unnecessary if statements class PreferEarlyReturnVisitor extends RecursiveAstVisitor { @@ -33,6 +34,7 @@ class PreferEarlyReturnVisitor extends RecursiveAstVisitor { if (_isElseIfStatement(node)) return; if (_hasElseStatement(node)) return; if (_hasReturnStatement(node)) return; + if (_hasThrowExpression(node)) return; _nodes.add(node); } @@ -70,4 +72,10 @@ class PreferEarlyReturnVisitor extends RecursiveAstVisitor { node.accept(visitor); return visitor.nodes.isNotEmpty; } + + bool _hasThrowExpression(Statement node) { + final visitor = ThrowExpressionVisitor(); + node.accept(visitor); + return visitor.nodes.isNotEmpty; + } } diff --git a/lib/src/lints/prefer_early_return/visitors/throw_expression_visitor.dart b/lib/src/lints/prefer_early_return/visitors/throw_expression_visitor.dart new file mode 100644 index 00000000..ceff7bd2 --- /dev/null +++ b/lib/src/lints/prefer_early_return/visitors/throw_expression_visitor.dart @@ -0,0 +1,16 @@ +import 'package:analyzer/dart/ast/ast.dart'; +import 'package:analyzer/dart/ast/visitor.dart'; + +/// The AST visitor that will collect every Return statement +class ThrowExpressionVisitor extends RecursiveAstVisitor { + final _nodes = []; + + /// All unnecessary return statements + Iterable get nodes => _nodes; + + @override + void visitThrowExpression(ThrowExpression node) { + super.visitThrowExpression(node); + _nodes.add(node); + } +} diff --git a/lint_test/prefer_early_return_test.dart b/lint_test/prefer_early_return_test.dart index f5a076bf..d9108186 100644 --- a/lint_test/prefer_early_return_test.dart +++ b/lint_test/prefer_early_return_test.dart @@ -235,3 +235,65 @@ void threeSeqentialIfReturn2() { _doSomething(); } } + +void oneIfWithThrowWithReturn() { + //no lint + if (true) { + throw ''; + } + + return; +} + +void oneIfElseWithThrowReturn() { + //no lint + if (true) { + _doSomething(); + } else { + throw ''; + } +} + +void twoSeqentialIfWithThrow() { + if (false) throw ''; + //expect_lint: prefer_early_return + if (true) { + _doSomething(); + } +} + +void twoSeqentialIfWithThrowReturn2() { + //no lint + if (false) throw ''; + //expect_lint: prefer_early_return + if (true) { + _doSomething(); + } + + return; +} + +void threeSeqentialIfWithThrowReturn() { + //no lint + if (false) throw ''; + if (true) throw ''; + //expect_lint: prefer_early_return + if (true) { + _doSomething(); + } + + return; +} + +void threeSeqentialIfWithThrowReturn2() { + //no lint + if (false) throw ''; + //no lint + if (true) { + _doSomething(); + } + //expect_lint: prefer_early_return + if (true) { + _doSomething(); + } +}