Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 23 additions & 29 deletions lib/src/lints/prefer_early_return/prefer_early_return_rule.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'package:analyzer/error/listener.dart';
import 'package:custom_lint_builder/custom_lint_builder.dart';
import 'package:analyzer/analysis_rule/analysis_rule.dart';
import 'package:analyzer/analysis_rule/rule_context.dart';
import 'package:analyzer/analysis_rule/rule_visitor_registry.dart';
import 'package:analyzer/error/error.dart';
import 'package:solid_lints/src/lints/prefer_early_return/visitors/prefer_early_return_visitor.dart';
import 'package:solid_lints/src/models/rule_config.dart';
import 'package:solid_lints/src/models/solid_lint_rule.dart';

/// A rule which highlights `if` statements that span the entire body,
/// and suggests replacing them with a reversed boolean check
Expand Down Expand Up @@ -31,38 +31,32 @@ import 'package:solid_lints/src/models/solid_lint_rule.dart';
/// c;
/// }
/// ```
class PreferEarlyReturnRule extends SolidLintRule {
/// This lint rule represents the error if
/// 'if' statements should be reversed
class PreferEarlyReturnRule extends AnalysisRule {
/// The name of the lint
static const String lintName = 'prefer_early_return';

PreferEarlyReturnRule._(super.config);
/// Lint code
static const LintCode _code = LintCode(
lintName,
"Use reverse if reduce nesting",
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The lint message is missing the word 'to', which makes it grammatically incorrect and inconsistent with the rule's description on line 48. It should be updated to 'Use reverse if to reduce nesting'.

Suggested change
"Use reverse if reduce nesting",
"Use reverse if to reduce nesting",

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Dariaa14, please take a look. Also, I believe we can extract it into a constant to reuse.

);

/// Creates a new instance of [PreferEarlyReturnRule]
/// based on the lint configuration.
factory PreferEarlyReturnRule.createRule(CustomLintConfigs configs) {
final rule = RuleConfig(
configs: configs,
name: lintName,
problemMessage: (_) => "Use reverse if to reduce nesting",
);
PreferEarlyReturnRule()
: super(
name: lintName,
description: 'Use reverse if to reduce nesting',
);

return PreferEarlyReturnRule._(rule);
}
@override
LintCode get diagnosticCode => _code;

@override
void run(
CustomLintResolver resolver,
DiagnosticReporter reporter,
CustomLintContext context,
void registerNodeProcessors(
RuleVisitorRegistry registry,
RuleContext context,
) {
context.registry.addBlockFunctionBody((node) {
final visitor = PreferEarlyReturnVisitor();
node.accept(visitor);

for (final element in visitor.nodes) {
reporter.atNode(element, code);
}
});
final visitor = PreferEarlyReturnVisitor(this);
registry.addBlockFunctionBody(this, visitor);
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:solid_lints/src/lints/prefer_early_return/prefer_early_return_rule.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
/// Visitor for [PreferEarlyReturnRule].
class PreferEarlyReturnVisitor extends RecursiveAstVisitor<void> {
final _nodes = <AstNode>[];
/// The rule associated with this visitor.
final PreferEarlyReturnRule rule;

/// All unnecessary if statements and conditional expressions.
Iterable<AstNode> get nodes => _nodes;
/// Creates an instance of [PreferEarlyReturnVisitor].
PreferEarlyReturnVisitor(this.rule);

@override
void visitBlockFunctionBody(BlockFunctionBody node) {
Expand Down Expand Up @@ -36,7 +38,7 @@ class PreferEarlyReturnVisitor extends RecursiveAstVisitor<void> {
if (_hasReturnStatement(node)) return;
if (_hasThrowExpression(node)) return;

_nodes.add(node);
rule.reportAtNode(node);
}

// returns a list of if statements at the start of the function
Expand Down
Loading
Loading