-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: defined_async_value_getter_type rule (#139)
- Loading branch information
Showing
10 changed files
with
233 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
packages/nilts/lib/src/lints/defined_async_value_getter_type.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// ignore_for_file: comment_references | ||
|
||
import 'package:analyzer/dart/element/type.dart'; | ||
import 'package:analyzer/error/error.dart'; | ||
import 'package:analyzer/error/listener.dart'; | ||
import 'package:custom_lint_builder/custom_lint_builder.dart'; | ||
import 'package:nilts/src/change_priority.dart'; | ||
|
||
/// A class for `defined_async_value_getter_type` rule. | ||
/// | ||
/// This rule checks defining `Future<T> Function()` type. | ||
/// | ||
/// - Target SDK : Any versions nilts supports | ||
/// - Rule type : Practice | ||
/// - Maturity level : Experimental | ||
/// - Quick fix : ✅ | ||
/// | ||
/// **Consider** replace `Future<T> Function()` with [AsyncValueGetter] | ||
/// which is defined in Flutter SDK. | ||
/// | ||
/// **BAD:** | ||
/// ```dart | ||
/// final Future<int> Function() callback; | ||
/// ``` | ||
/// | ||
/// **GOOD:** | ||
/// ```dart | ||
/// final AsyncValueGetter<int> callback; | ||
/// ``` | ||
/// | ||
/// See also: | ||
/// | ||
/// - [AsyncValueGetter typedef - foundation library - Dart API](https://api.flutter.dev/flutter/foundation/AsyncValueGetter.html) | ||
class DefinedAsyncValueGetterType extends DartLintRule { | ||
/// Create a new instance of [DefinedAsyncValueGetterType]. | ||
const DefinedAsyncValueGetterType() : super(code: _code); | ||
|
||
static const _code = LintCode( | ||
name: 'defined_async_value_getter_type', | ||
problemMessage: '`AsyncValueGetter<T>` type is defined in Flutter SDK.', | ||
url: 'https://github.com/ronnnnn/nilts#defined_async_value_getter_type', | ||
); | ||
|
||
@override | ||
void run( | ||
CustomLintResolver resolver, | ||
ErrorReporter reporter, | ||
CustomLintContext context, | ||
) { | ||
context.registry.addTypeAnnotation((node) { | ||
final type = node.type; | ||
// Do nothing if the type is not Function. | ||
if (type is! FunctionType) return; | ||
|
||
// Do nothing if Function has parameters. | ||
if (type.parameters.isNotEmpty) return; | ||
|
||
// Do nothing if the return type is not Future<T>. | ||
final returnType = type.returnType; | ||
if (returnType is! InterfaceType) return; | ||
if (!returnType.isDartAsyncFuture) return; | ||
if (returnType.typeArguments.length != 1) return; | ||
final typeArgument = returnType.typeArguments.first; | ||
if (typeArgument is VoidType || | ||
typeArgument is InvalidType || | ||
typeArgument is NeverType) return; | ||
|
||
reporter.reportErrorForNode(_code, node); | ||
}); | ||
} | ||
|
||
@override | ||
List<Fix> getFixes() => [ | ||
_ReplaceWithAsyncValueGetter(), | ||
]; | ||
} | ||
|
||
class _ReplaceWithAsyncValueGetter extends DartFix { | ||
@override | ||
void run( | ||
CustomLintResolver resolver, | ||
ChangeReporter reporter, | ||
CustomLintContext context, | ||
AnalysisError analysisError, | ||
List<AnalysisError> others, | ||
) { | ||
context.registry.addTypeAnnotation((node) { | ||
if (!node.sourceRange.intersects(analysisError.sourceRange)) return; | ||
|
||
reporter | ||
.createChangeBuilder( | ||
message: 'Replace with AsyncValueGetter<T>', | ||
priority: ChangePriority.replaceWithAsyncValueGetter, | ||
) | ||
.addDartFileEdit((builder) { | ||
final returnType = (node.type! as FunctionType).returnType; | ||
final returnTypeArgumentName = | ||
(returnType as InterfaceType).typeArguments.first.element!.name; | ||
|
||
final delta = node.question != null ? -1 : 0; | ||
builder.addSimpleReplacement( | ||
node.sourceRange.getMoveEnd(delta), | ||
'AsyncValueGetter<$returnTypeArgumentName>', | ||
); | ||
}); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
packages/nilts_test/test/lints/defined_async_value_getter_type.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// ignore_for_file: prefer_function_declarations_over_variables | ||
// ignore_for_file: type_init_formals | ||
// ignore_for_file: unused_element | ||
// ignore_for_file: defined_value_getter_type | ||
// ignore_for_file: defined_async_callback_type | ||
// ignore_for_file: defined_async_value_setter_type | ||
|
||
import 'dart:async'; | ||
|
||
import 'package:flutter/material.dart'; | ||
|
||
class MainButton extends StatelessWidget { | ||
const MainButton( | ||
Future<void> Function() this.onPressed, | ||
// expect_lint: defined_async_value_getter_type | ||
Future<int> Function() this.onReturnPressed, | ||
int Function() this.onFutureReturnPressed, { | ||
Future<void> Function()? this.onNullablePressed, | ||
Future<void> Function(int)? this.onParamPressed, | ||
// expect_lint: defined_async_value_getter_type | ||
Future<int> Function()? this.onNullableReturnPressed, | ||
int Function()? this.onNullableFutureReturnPressed, | ||
super.key, | ||
}); | ||
|
||
final Future<void> Function() onPressed; | ||
// expect_lint: defined_async_value_getter_type | ||
final Future<int> Function() onReturnPressed; | ||
final int Function() onFutureReturnPressed; | ||
final Future<void> Function()? onNullablePressed; | ||
final Future<void> Function(int)? onParamPressed; | ||
// expect_lint: defined_async_value_getter_type | ||
final Future<int> Function()? onNullableReturnPressed; | ||
final int Function()? onNullableFutureReturnPressed; | ||
|
||
void _onPressed( | ||
Future<void> Function() onPressed, | ||
// expect_lint: defined_async_value_getter_type | ||
Future<int> Function() onReturnPressed, | ||
int Function() onFutureReturnPressed, { | ||
Future<void> Function()? onNullablePressed, | ||
Future<void> Function(int)? onParamPressed, | ||
// expect_lint: defined_async_value_getter_type | ||
Future<int> Function()? onNullableReturnPressed, | ||
int Function()? onNullableFutureReturnPressed, | ||
}) {} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return FilledButton( | ||
onPressed: () { | ||
_onPressed( | ||
() async {}, | ||
() async => 0, | ||
() => 0, | ||
); | ||
onPressed(); | ||
}, | ||
child: const Text('Hello World!'), | ||
); | ||
} | ||
} | ||
|
||
final Future<void> Function() globalFunction = () async {}; | ||
// expect_lint: defined_async_value_getter_type | ||
final Future<int> Function() globalReturnFunction = () async => 0; | ||
final int Function() globalFutureReturnFunction = () => 0; | ||
const Future<void> Function()? globalNullableFunction = null; | ||
const Future<void> Function(int)? globalParamFunction = null; | ||
// expect_lint: defined_async_value_getter_type | ||
const Future<int> Function()? globalNullableReturnFunction = null; | ||
const int Function()? globalNullableFutureReturnFunction = null; | ||
|
||
void _globalFunction( | ||
Future<void> Function() onPressed, | ||
// expect_lint: defined_async_value_getter_type | ||
Future<int> Function() onReturnPressed, | ||
int Function() onFutureReturnPressed, { | ||
Future<void> Function()? onNullablePressed, | ||
Future<void> Function(int)? onParamPressed, | ||
// expect_lint: defined_async_value_getter_type | ||
Future<int> Function()? onNullableReturnPressed, | ||
int Function()? onNullableFutureReturnPressed, | ||
}) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters