Repository navigation
Add a rule prefer_guard_clause for reversing nested if statements
#154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
illia-romanenko
merged 12 commits into
solid-software:master
from
velvitoff:use_reverse_if_to_reduce_nesting
Apr 22, 2024
Merged
Changes from 5 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
7706d89
reverse_if_to_avoid_nesting wip
velvitoff 2de0d3f
change name to prefer_guard_close and improve tests
velvitoff 44dbe63
prefer_guard_clause wip
velvitoff af5a75b
fix test name for prefer_guard_clause
velvitoff c1e4ef8
changed to detecting nested if statements
velvitoff 96008f3
fix pr comments
velvitoff c8fec9f
adapt to two sequential if statements
velvitoff 77579a8
remove (.length <= 2) limit
velvitoff d5acf40
fix alphabetic ordering of imports
velvitoff ebb354c
refactor and fix prefer_early_return_visitor.dart to handle nested if…
4akloon 17ebe00
refactor prefer_early_return
4akloon b09bd81
Merge branch 'master' into use_reverse_if_to_reduce_nesting
4akloon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
lib/src/lints/reverse_if_to_reduce_nesting/reverse_if_to_reduce_nesting_rule.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import 'package:analyzer/error/listener.dart'; | ||
| import 'package:custom_lint_builder/custom_lint_builder.dart'; | ||
| import 'package:solid_lints/src/lints/reverse_if_to_reduce_nesting/visitors/reverse_if_to_reduce_nesting_visitor.dart'; | ||
| import 'package:solid_lints/src/models/rule_config.dart'; | ||
| import 'package:solid_lints/src/models/solid_lint_rule.dart'; | ||
|
|
||
| /// A `reverse_if_to_reduce_nesting` rule which promotes using guard clauses | ||
| /// over nesting `if` statements | ||
| /// | ||
| /// ### Example | ||
| /// | ||
| /// #### BAD: | ||
| /// | ||
| /// ```dart | ||
| /// void func() { | ||
| /// if(a){ //LINT | ||
| /// if(b){ //LINT | ||
| /// c; | ||
| /// } | ||
| /// } | ||
| /// } | ||
|
velvitoff marked this conversation as resolved.
Outdated
|
||
| /// ``` | ||
| /// | ||
| /// #### GOOD: | ||
| /// | ||
| /// ```dart | ||
| /// void func() { | ||
| /// if(!a) return; | ||
| /// if(!b) return; | ||
| /// c; | ||
| /// } | ||
| /// ``` | ||
| class ReverseIfToReduceNestingRule extends SolidLintRule { | ||
| /// The [LintCode] of this lint rule that represents the error if | ||
| /// 'if' statements should be reversed | ||
| static const String lintName = 'reverse_if_to_reduce_nesting'; | ||
|
velvitoff marked this conversation as resolved.
Outdated
|
||
|
|
||
| ReverseIfToReduceNestingRule._(super.config); | ||
|
|
||
| /// Creates a new instance of [ReverseIfToReduceNestingRule] | ||
| /// based on the lint configuration. | ||
| factory ReverseIfToReduceNestingRule.createRule(CustomLintConfigs configs) { | ||
| final rule = RuleConfig( | ||
| configs: configs, | ||
| name: lintName, | ||
| problemMessage: (_) => "Use reverse if to reduce nesting", | ||
| ); | ||
|
|
||
| return ReverseIfToReduceNestingRule._(rule); | ||
| } | ||
|
|
||
| @override | ||
| void run( | ||
| CustomLintResolver resolver, | ||
| ErrorReporter reporter, | ||
| CustomLintContext context, | ||
| ) { | ||
| context.registry.addBlockFunctionBody((node) { | ||
| final visitor = ReverseIfToReduceNestingVisitor(); | ||
| node.accept(visitor); | ||
|
|
||
| for (final element in visitor.nodes) { | ||
| reporter.reportErrorForNode(code, element); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
16 changes: 16 additions & 0 deletions
16
lib/src/lints/reverse_if_to_reduce_nesting/visitors/if_statement_visitor.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 If statement | ||
| class IfStatementVisitor extends RecursiveAstVisitor<void> { | ||
| final _nodes = <IfStatement>[]; | ||
|
|
||
| /// All unnecessary if statements and conditional expressions. | ||
| Iterable<IfStatement> get nodes => _nodes; | ||
|
|
||
| @override | ||
| void visitIfStatement(IfStatement node) { | ||
| super.visitIfStatement(node); | ||
| _nodes.add(node); | ||
| } | ||
| } | ||
|
velvitoff marked this conversation as resolved.
Outdated
|
||
54 changes: 54 additions & 0 deletions
54
...src/lints/reverse_if_to_reduce_nesting/visitors/reverse_if_to_reduce_nesting_visitor.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import 'package:analyzer/dart/ast/ast.dart'; | ||
| import 'package:analyzer/dart/ast/visitor.dart'; | ||
| import 'package:solid_lints/src/lints/reverse_if_to_reduce_nesting/visitors/if_statement_visitor.dart'; | ||
|
|
||
| /// The AST visitor that will collect all unnecessary if statements | ||
| class ReverseIfToReduceNestingVisitor extends RecursiveAstVisitor<void> { | ||
| final _nodes = <AstNode>[]; | ||
|
|
||
| /// All unnecessary if statements and conditional expressions. | ||
| Iterable<AstNode> get nodes => _nodes; | ||
|
|
||
| @override | ||
| void visitBlockFunctionBody(BlockFunctionBody node) { | ||
| super.visitBlockFunctionBody(node); | ||
| if (node.block.statements.isEmpty) return; | ||
|
|
||
| final first = node.block.statements.first; | ||
|
|
||
| switch (node.block.statements.length) { | ||
| case 1: | ||
| if (first is IfStatement) _handleIfStatement(first); | ||
| return; | ||
| case 2: | ||
| final last = node.block.statements.last; | ||
| if (last is ReturnStatement && first is IfStatement) { | ||
| _handleIfStatement(first); | ||
| } | ||
| case _: | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| void _handleIfStatement(IfStatement node) { | ||
| if (_isElseIfStatement(node)) return; | ||
| if (_hasElseStatement(node)) return; | ||
| if (!_containsIfStatement(node.thenStatement)) return; | ||
|
yurii-prykhodko-solid marked this conversation as resolved.
Outdated
|
||
|
|
||
| _nodes.add(node); | ||
| } | ||
| } | ||
|
|
||
| bool _hasElseStatement(IfStatement node) { | ||
| return node.elseStatement != null; | ||
| } | ||
|
|
||
| bool _isElseIfStatement(IfStatement node) { | ||
| return node.elseStatement != null && node.elseStatement is IfStatement; | ||
| } | ||
|
|
||
| bool _containsIfStatement(Statement node) { | ||
| final visitor = IfStatementVisitor(); | ||
| node.accept(visitor); | ||
| return visitor.nodes.isNotEmpty; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| // ignore_for_file: dead_code, cyclomatic_complexity, no_equal_then_else, prefer_match_file_name | ||
|
|
||
| // ignore: no_empty_block | ||
| void _doSomething() {} | ||
|
|
||
| void oneIf() { | ||
| //no lint | ||
| if (true) { | ||
| _doSomething(); | ||
| } | ||
| } | ||
|
|
||
| void oneIfWithReturn() { | ||
| //no lint | ||
| if (true) { | ||
| _doSomething(); | ||
| } | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| void nestedIf2() { | ||
| //expect_lint: reverse_if_to_reduce_nesting | ||
| if (true) { | ||
| if (true) { | ||
| _doSomething(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void nestedIf3() { | ||
| //expect_lint: reverse_if_to_reduce_nesting | ||
| if (true) { | ||
| if (true) { | ||
| if (true) { | ||
| _doSomething(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void oneNestedIf2WithReturn() { | ||
| //expect_lint: reverse_if_to_reduce_nesting | ||
| if (true) { | ||
| if (true) { | ||
| if (true) { | ||
| _doSomething(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| void oneIfElse() { | ||
| //no lint | ||
| if (true) { | ||
| _doSomething(); | ||
| } else { | ||
| _doSomething(); | ||
| } | ||
| } | ||
|
|
||
| void oneIfElseReturn() { | ||
| //no lint | ||
| if (true) { | ||
| _doSomething(); | ||
| } else { | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| void twoIfElse() { | ||
| //no lint | ||
| if (true) { | ||
| if (true) { | ||
| _doSomething(); | ||
| } | ||
| } else { | ||
| _doSomething(); | ||
| } | ||
| } | ||
|
|
||
| void twoIfElseInner() { | ||
| //expect_lint: reverse_if_to_reduce_nesting | ||
| if (true) { | ||
| if (true) { | ||
| _doSomething(); | ||
| } else { | ||
| _doSomething(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void threeIf() { | ||
| //expect_lint: reverse_if_to_reduce_nesting | ||
| if (true) { | ||
| if (true) { | ||
| if (true) { | ||
| _doSomething(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void threeIfElse1() { | ||
| //no lint | ||
| if (true) { | ||
| if (true) { | ||
| if (true) { | ||
| _doSomething(); | ||
| } | ||
| } | ||
| } else { | ||
| _doSomething(); | ||
| } | ||
| } | ||
|
|
||
| void threeIfElse2() { | ||
| //expect_lint: reverse_if_to_reduce_nesting | ||
| if (true) { | ||
| if (true) { | ||
| if (true) { | ||
| _doSomething(); | ||
| } | ||
| } else { | ||
| _doSomething(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void threeIfElse3() { | ||
| //expect_lint: reverse_if_to_reduce_nesting | ||
| if (true) { | ||
| if (true) { | ||
| if (true) { | ||
| _doSomething(); | ||
| } else { | ||
| _doSomething(); | ||
| } | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.