Skip to content
This repository has been archived by the owner on Jul 16, 2023. It is now read-only.

Commit

Permalink
feat: remove declaration in prefer-immediate-return
Browse files Browse the repository at this point in the history
  • Loading branch information
ookami-kb committed Apr 30, 2022
1 parent c451262 commit 78f688e
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 30 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -3,6 +3,7 @@
## Unreleased

* fix: dart-code-metrics crash saying `Bad state: No element` when running command.
* feat: remove declaration in prefer-immediate-return

## 4.14.0

Expand Down
Expand Up @@ -32,18 +32,29 @@ class PreferImmediateReturnRule extends CommonRule {
final visitor = _Visitor();
source.unit.visitChildren(visitor);

return visitor.issues
.map(
(issue) => createIssue(
rule: this,
location: nodeLocation(node: issue.returnStatement, source: source),
message: _warningMessage,
replacement: Replacement(
comment: _replaceComment,
replacement: 'return ${issue.variableDeclarationInitializer};',
),
return visitor.issues.map(
(issue) {
AstNode? getSingleDeclarationNode() {
final declarationList =
issue.variableDeclaration.parent as VariableDeclarationList;

return declarationList.variables.length == 1 ? declarationList : null;
}

return createIssue(
rule: this,
location: nodeLocation(
node: getSingleDeclarationNode() ?? issue.returnStatement,
endNode: issue.returnStatement,
source: source,
),
)
.toList(growable: false);
message: _warningMessage,
replacement: Replacement(
comment: _replaceComment,
replacement: 'return ${issue.variableDeclaration.initializer};',
),
);
},
).toList(growable: false);
}
}
Expand Up @@ -33,18 +33,18 @@ class _Visitor extends RecursiveAstVisitor<void> {
}

_issues.add(_IssueDetails(
lastDeclaredVariable.initializer,
lastDeclaredVariable,
returnStatement,
));
}
}

class _IssueDetails {
const _IssueDetails(
this.variableDeclarationInitializer,
this.variableDeclaration,
this.returnStatement,
);

final Expression? variableDeclarationInitializer;
final VariableDeclaration variableDeclaration;
final ReturnStatement returnStatement;
}
3 changes: 2 additions & 1 deletion lib/src/utils/node_utils.dart
Expand Up @@ -8,12 +8,13 @@ import '../analyzers/lint_analyzer/models/internal_resolved_unit_result.dart';
SourceSpan nodeLocation({
required SyntacticEntity node,
required InternalResolvedUnitResult source,
SyntacticEntity? endNode,
bool withCommentOrMetadata = false,
}) {
final offset = !withCommentOrMetadata && node is AnnotatedNode
? node.firstTokenAfterCommentAndMetadata.offset
: node.offset;
final end = node.end;
final end = endNode?.end ?? node.end;
final sourceUrl = Uri.file(source.path);

final offsetLocation = source.lineInfo.getLocation(offset);
Expand Down
Expand Up @@ -28,15 +28,15 @@ void main() {
RuleTestHelper.verifyIssues(
issues: issues,
startLines: [
4,
2,
10,
16,
23,
30,
49,
53,
63,
68,
21,
28,
47,
51,
61,
66,
],
startColumns: [
3,
Expand Down Expand Up @@ -83,15 +83,29 @@ void main() {
'return a + b;',
],
locationTexts: [
'final sum = a + b;\n'
'\n'
' return sum;',
'return sum;',
'return sum;',
'return sum;',
'return result;',
'return x;',
'return sum;',
'return sum;',
'return result;',
'return result;',
'final result = width * height;\n'
'\n'
' return result;',
'final String? x;\n'
'\n'
' return x;',
'final sum = a + b;\n'
'\n'
' return sum;',
'final sum = 0;\n'
'\n'
' return sum;',
'final result = a * b;\n'
'\n'
' return result;',
'final result = a + b;\n'
'\n'
' return result;',
],
);
});
Expand Down

0 comments on commit 78f688e

Please sign in to comment.