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

fix: correctly handle FunctionExpressions for avoid-redundant-async #1124

Merged
merged 3 commits into from
Dec 28, 2022
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: correctly handle FunctionExpressions for [`avoid-redundant-async`](https://dartcodemetrics.dev/docs/rules/common/avoid-redundant-async).
* feat: support ignoring nesting for [`prefer-conditional-expressions`](https://dartcodemetrics.dev/docs/rules/common/prefer-conditional-expressions).
* fix: ignore Providers for ['avoid-returning-widgets'](https://dartcodemetrics.dev/docs/rules/common/avoid-returning-widgets).
* feat: add [`use-setstate-synchronously`](https://dartcodemetrics.dev/docs/rules/flutter/use-setstate-synchronously).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@ class AvoidRedundantAsyncRule extends CommonRule {

source.unit.visitChildren(visitor);

return visitor.declarations.map((declaration) => createIssue(
rule: this,
location: nodeLocation(
node: declaration,
source: source,
),
message: _warningMessage,
));
return visitor.nodes.map(
(node) => createIssue(
rule: this,
location: nodeLocation(
node: node,
source: source,
),
message: _warningMessage,
),
);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
part of 'avoid_redundant_async_rule.dart';

class _Visitor extends RecursiveAstVisitor<void> {
final _declarations = <Declaration>[];
final _nodes = <AstNode>[];

Iterable<Declaration> get declarations => _declarations;
Iterable<AstNode> get nodes => _nodes;

@override
void visitMethodDeclaration(MethodDeclaration node) {
Expand All @@ -15,16 +15,16 @@ class _Visitor extends RecursiveAstVisitor<void> {
);

if (!isOverride && _hasRedundantAsync(node.body)) {
_declarations.add(node);
_nodes.add(node);
}
}

@override
void visitFunctionDeclaration(FunctionDeclaration node) {
super.visitFunctionDeclaration(node);
void visitFunctionExpression(FunctionExpression node) {
super.visitFunctionExpression(node);

if (_hasRedundantAsync(node.functionExpression.body)) {
_declarations.add(node);
if (_hasRedundantAsync(node.body)) {
_nodes.add(node);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,21 @@ void main() {

RuleTestHelper.verifyIssues(
issues: issues,
startLines: [12, 22],
startColumns: [1, 3],
startLines: [12, 22, 59, 60],
startColumns: [26, 3, 5, 22],
locationTexts: [
'Future<int> fastestBranch(Future<int> left, Future<int> right) async {\n'
'(Future<int> left, Future<int> right) async {\n'
' return Future.any([left, right]);\n'
'}',
"Future<String> anotherAsyncMethod() async => Future.value('value');",
'() async => instance.someAsyncMethod()',
'() async => instance.someAsyncMethod()',
],
messages: [
"'async' keyword is redundant, consider removing it.",
"'async' keyword is redundant, consider removing it.",
"'async' keyword is redundant, consider removing it.",
"'async' keyword is redundant, consider removing it.",
],
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,19 @@ class SomeClass {
}
}
}

class WithFunctionField {
final Function(void) onSelected;
final Function(void) onSelectedNamed;

const WithFunctionField(this.onSelected, {required this.onSelectedNamed});
}

void main() {
final instance = SomeClass();

WithFunctionField(
() async => instance.someAsyncMethod(), // LINT
onSelectedNamed: () async => instance.someAsyncMethod(), // LINT
);
}