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

fix: partially handle normal completion function body for avoid-redundant-async #1148

Merged
merged 4 commits into from
Jan 20, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

* fix: partially handle normal completion function body for [`avoid-redundant-async`](https://dcm.dev/docs/individuals/rules/common/avoid-redundant-async).
* fix: ignore enum constant arguments for [`no-magic-number`](https://dcm.dev/docs/individuals/rules/common/no-magic-number).
* fix: correctly handle prefixed enums and static instance fields for [`prefer-moving-to-variable`](https://dcm.dev/docs/individuals/rules/common/prefer-moving-to-variable).
* feat: add static code diagnostic [`prefer-provide-intl-description`](https://dcm.dev/docs/individuals/rules/intl/prefer-provide-intl-description).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,40 @@ class _Visitor extends RecursiveAstVisitor<void> {
}
}

final asyncVisitor = _AsyncVisitor();
final asyncVisitor = _AsyncVisitor(body);
body.parent?.visitChildren(asyncVisitor);

if (asyncVisitor._allReturns.isNotEmpty) {
return !asyncVisitor.hasValidAsync &&
asyncVisitor._allReturns.length !=
asyncVisitor._returnsInsideIf.length;
}

return !asyncVisitor.hasValidAsync;
}
}

class _AsyncVisitor extends RecursiveAstVisitor<void> {
final FunctionBody body;

bool hasValidAsync = false;

final _allReturns = <ReturnStatement>{};
final _returnsInsideIf = <ReturnStatement>{};

_AsyncVisitor(this.body);

@override
void visitReturnStatement(ReturnStatement node) {
super.visitReturnStatement(node);

final type = node.expression?.staticType;

_allReturns.add(node);
if (_isInsideIfStatement(node, body)) {
_returnsInsideIf.add(node);
}

if (type == null ||
!type.isDartAsyncFuture ||
type.nullabilitySuffix == NullabilitySuffix.question) {
Expand Down Expand Up @@ -80,4 +98,12 @@ class _AsyncVisitor extends RecursiveAstVisitor<void> {

hasValidAsync = true;
}

bool _isInsideIfStatement(ReturnStatement node, FunctionBody body) {
final parent = node.thisOrAncestorMatching(
(parent) => parent == body || parent is IfStatement,
);

return parent is IfStatement;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,12 @@ void main() {
() async => instance.someAsyncMethod(), // LINT
onSelectedNamed: () async => instance.someAsyncMethod(), // LINT
);

WithFunctionField(onSelectedNamed: () async {
if (shouldRefresh) return apiCall();
});
}

Future<void> apiCall() {
return Future.value();
}