Skip to content

Commit

Permalink
issue-182. fixed throw expression (#183)
Browse files Browse the repository at this point in the history
* issue-182. fixed throw expression

* issue-182. added new test case

* issue-182. added changelog

---------

Co-authored-by: shaark <[email protected]>
  • Loading branch information
StarovNikita and shaark authored Dec 4, 2024
1 parent 9dcdb64 commit 956d433
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.2.4

- Fixed an issue with `prefer_early_retrun` for throw expression

## 0.2.3

- Replace deprecated whereNotNull()
Expand Down
Original file line number Diff line number Diff line change
@@ -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<void> {
Expand Down Expand Up @@ -33,6 +34,7 @@ class PreferEarlyReturnVisitor extends RecursiveAstVisitor<void> {
if (_isElseIfStatement(node)) return;
if (_hasElseStatement(node)) return;
if (_hasReturnStatement(node)) return;
if (_hasThrowExpression(node)) return;

_nodes.add(node);
}
Expand Down Expand Up @@ -70,4 +72,10 @@ class PreferEarlyReturnVisitor extends RecursiveAstVisitor<void> {
node.accept(visitor);
return visitor.nodes.isNotEmpty;
}

bool _hasThrowExpression(Statement node) {
final visitor = ThrowExpressionVisitor();
node.accept(visitor);
return visitor.nodes.isNotEmpty;
}
}
Original file line number Diff line number Diff line change
@@ -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<void> {
final _nodes = <ThrowExpression>[];

/// All unnecessary return statements
Iterable<ThrowExpression> get nodes => _nodes;

@override
void visitThrowExpression(ThrowExpression node) {
super.visitThrowExpression(node);
_nodes.add(node);
}
}
62 changes: 62 additions & 0 deletions lint_test/prefer_early_return_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

0 comments on commit 956d433

Please sign in to comment.