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

feat: remove declaration in prefer-immediate-return #824

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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