Skip to content

Commit

Permalink
[analysis_server] AddAwait to handle NON_BOOL_CONDITION
Browse files Browse the repository at this point in the history
Fixes flutter#49114

Change-Id: Id6b345a946ec124e4518c7bb526c510ae0cb92c6
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/246100
Reviewed-by: Brian Wilkerson <[email protected]>
Commit-Queue: Brian Wilkerson <[email protected]>
  • Loading branch information
asashour authored and Commit Bot committed Jun 12, 2022
1 parent 0204922 commit 4ecc7ff
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,31 @@

import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
import 'package:collection/collection.dart';

class AddAwait extends CorrectionProducer {
/// The kind of correction to be made.
final _CorrectionKind _correctionKind;

@override
bool get canBeAppliedInBulk => true;
bool canBeAppliedInBulk;

@override
bool get canBeAppliedToFile => true;
bool canBeAppliedToFile;

AddAwait.nonBool()
: _correctionKind = _CorrectionKind.nonBool,
canBeAppliedInBulk = false,
canBeAppliedToFile = false;

AddAwait.unawaited()
: _correctionKind = _CorrectionKind.unawaited,
canBeAppliedInBulk = true,
canBeAppliedToFile = true;

@override
FixKind get fixKind => DartFixKind.ADD_AWAIT;
Expand All @@ -22,8 +38,34 @@ class AddAwait extends CorrectionProducer {

@override
Future<void> compute(ChangeBuilder builder) async {
if (_correctionKind == _CorrectionKind.unawaited) {
await _addAwait(builder);
} else if (_correctionKind == _CorrectionKind.nonBool) {
await _computeNonBool(builder);
}
}

Future<void> _addAwait(ChangeBuilder builder) async {
await builder.addDartFileEdit(file, (builder) {
builder.addSimpleInsertion(node.offset, 'await ');
});
}

Future<void> _computeNonBool(ChangeBuilder builder) async {
var expr = node;
if (expr is! Expression) return;
var staticType = expr.staticType;
if (staticType is! ParameterizedType) return;

if (staticType.isDartAsyncFuture &&
staticType.typeArguments.firstOrNull?.isDartCoreBool == true) {
await _addAwait(builder);
}
}
}

/// The kinds of corrections supported by [AddAwait].
enum _CorrectionKind {
unawaited,
nonBool,
}
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ class FixProcessor extends BaseProcessor {
RemoveTypeAnnotation.new,
],
LintNames.unawaited_futures: [
AddAwait.new,
AddAwait.unawaited,
],
LintNames.unnecessary_brace_in_string_interps: [
RemoveInterpolationBraces.new,
Expand Down Expand Up @@ -1010,6 +1010,7 @@ class FixProcessor extends BaseProcessor {
],
CompileTimeErrorCode.NON_BOOL_CONDITION: [
AddNeNull.new,
AddAwait.nonBool,
],
CompileTimeErrorCode.NON_CONST_GENERATIVE_ENUM_CONSTRUCTOR: [
AddConst.new,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,35 @@ void f() async {
}
''');
}

Future<void> test_nonBoolCondition_futureBool() async {
await resolveTestCode('''
Future<bool> doSomething() async => true;
Future<void> f() async {
if (doSomething()) {
}
}
''');
await assertHasFix('''
Future<bool> doSomething() async => true;
Future<void> f() async {
if (await doSomething()) {
}
}
''');
}

Future<void> test_nonBoolCondition_futureInt() async {
await resolveTestCode('''
Future<int> doSomething() async => 0;
Future<void> f() async {
if (doSomething()) {
}
}
''');
await assertNoFix();
}
}

0 comments on commit 4ecc7ff

Please sign in to comment.