Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: defined_async_callback_type rule #136

Merged
merged 1 commit into from
Jul 22, 2024
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
20 changes: 10 additions & 10 deletions examples/nilts_example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ packages:
dependency: transitive
description:
name: _fe_analyzer_shared
sha256: eb376e9acf6938204f90eb3b1f00b578640d3188b4c8a8ec054f9f479af8d051
sha256: "0b2f2bd91ba804e53a61d757b986f89f1f9eaed5b11e4b2f5a2468d86d6c9fc7"
url: "https://pub.dev"
source: hosted
version: "64.0.0"
version: "67.0.0"
analyzer:
dependency: transitive
description:
name: analyzer
sha256: "69f54f967773f6c26c7dcb13e93d7ccee8b17a641689da39e878d5cf13b06893"
sha256: "37577842a27e4338429a1cbc32679d508836510b056f1eedf0c8d20e39c1383d"
url: "https://pub.dev"
source: hosted
version: "6.2.0"
version: "6.4.1"
analyzer_plugin:
dependency: transitive
description:
Expand Down Expand Up @@ -117,26 +117,26 @@ packages:
dependency: "direct dev"
description:
name: custom_lint
sha256: f89ff83efdba7c8996e86bb3bad0b759d58f9b19ae4d0e277a386ddd8b481217
sha256: "7c0aec12df22f9082146c354692056677f1e70bc43471644d1fdb36c6fdda799"
url: "https://pub.dev"
source: hosted
version: "0.6.0"
version: "0.6.4"
custom_lint_builder:
dependency: transitive
description:
name: custom_lint_builder
sha256: "9cdd9987feaa6925ec5f98d64de4fbbb5d94248ff77bbf2489366efad6c4baef"
sha256: d7dc41e709dde223806660268678be7993559e523eb3164e2a1425fd6f7615a9
url: "https://pub.dev"
source: hosted
version: "0.6.0"
version: "0.6.4"
custom_lint_core:
dependency: transitive
description:
name: custom_lint_core
sha256: "9003a91409c9f1db6e2e50b4870d1d5e802e5923b25f7261bf3cb3e11ea9d4fb"
sha256: a85e8f78f4c52f6c63cdaf8c872eb573db0231dcdf3c3a5906d493c1f8bc20e6
url: "https://pub.dev"
source: hosted
version: "0.6.0"
version: "0.6.3"
dart_style:
dependency: transitive
description:
Expand Down
31 changes: 31 additions & 0 deletions packages/nilts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ Some of lint rules support quick fixes on IDE.

| Rule name | Overview | Target SDK | Rule type | Maturity level | Quick fix |
|:------------------------------------------------------------------------------------|:-------------------------------------------------------------------------------|:------------------------------:| :-------: |:--------------:|:---------:|
| [defined\_async\_callback\_type](#defined_async_callback_type) | Checks `Future<void> Function()` definitions. | Any versions nilts supports | Practice | Experimental | ✅️ |
| [defined\_value\_changed\_type](#defined_value_changed_type) | Checks `void Function(T value)` definitions. | Any versions nilts supports | Practice | Experimental | ✅️ |
| [defined\_value\_getter\_type](#defined_value_getter_type) | Checks `T Function()` definitions. | Any versions nilts supports | Practice | Experimental | ✅️ |
| [defined\_value\_setter\_type](#defined_value_setter_type) | Checks `void Function(T value)` definitions. | Any versions nilts supports | Practice | Experimental | ✅️ |
Expand All @@ -105,6 +106,36 @@ Some of lint rules support quick fixes on IDE.

### Details

#### defined_async_callback_type

<details>

- Target SDK : Any versions nilts supports
- Rule type : Practice
- Maturity level : Experimental
- Quick fix : ✅

**Consider** replace `Future<void> Function()` with `AsyncCallback` which is defined in Flutter SDK.

**BAD:**

```dart
final Future<void> Function() callback;
```


**GOOD:**

```dart
final AsyncCallback callback;
```

See also:

- [AsyncCallback typedef - foundation library - Dart API](https://api.flutter.dev/flutter/foundation/AsyncCallback.html)

</details>

#### defined_value_changed_type

<details>
Expand Down
2 changes: 2 additions & 0 deletions packages/nilts/lib/nilts.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:custom_lint_builder/custom_lint_builder.dart';
import 'package:nilts/src/dart_version.dart';
import 'package:nilts/src/lints/defined_async_callback_type.dart';
import 'package:nilts/src/lints/defined_value_callback_type.dart';
import 'package:nilts/src/lints/defined_value_getter_type.dart';
import 'package:nilts/src/lints/defined_void_callback_type.dart';
Expand All @@ -20,6 +21,7 @@ class _NiltsLint extends PluginBase {

@override
List<LintRule> getLintRules(CustomLintConfigs configs) => [
const DefinedAsyncCallbackType(),
const DefinedValueChangedType(),
const DefinedValueGetterType(),
const DefinedValueSetterType(),
Expand Down
3 changes: 3 additions & 0 deletions packages/nilts/lib/src/change_priority.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class ChangePriority {
/// The priority for [_ReplaceWithAlignmentDirectional].
static const int replaceWithAlignmentDirectional = 100;

/// The priority for [_ReplaceWithAsyncCallback].
static const int replaceWithAsyncCallback = 100;

/// The priority for [_ReplaceWithDefaultTargetPlatform].
static const int replaceWithDefaultTargetPlatform = 100;

Expand Down
102 changes: 102 additions & 0 deletions packages/nilts/lib/src/lints/defined_async_callback_type.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// 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_callback_type` rule.
///
/// This rule checks defining `Future<void> Function()` type.
///
/// - Target SDK : Any versions nilts supports
/// - Rule type : Practice
/// - Maturity level : Experimental
/// - Quick fix : ✅
///
/// **Consider** replace `Future<void> Function()` with [AsyncCallback] which is
/// defined in Flutter SDK.
///
/// **BAD:**
/// ```dart
/// final Future<void> Function() callback;
/// ```
///
/// **GOOD:**
/// ```dart
/// final AsyncCallback callback;
/// ```
///
/// See also:
///
/// - [AsyncCallback typedef - foundation library - Dart API](https://api.flutter.dev/flutter/foundation/AsyncCallback.html)
class DefinedAsyncCallbackType extends DartLintRule {
/// Create a new instance of [DefinedAsyncCallbackType].
const DefinedAsyncCallbackType() : super(code: _code);

static const _code = LintCode(
name: 'defined_async_callback_type',
problemMessage: '`AsyncCallback` type is defined in Flutter SDK.',
url: 'https://github.com/ronnnnn/nilts#defined_async_callback_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<void>.
final returnType = type.returnType;
if (returnType is! InterfaceType) return;
if (!returnType.element.library.isDartAsync) return;
if (returnType.element.name != 'Future') return;
if (returnType.typeArguments.length != 1) return;
if (returnType.typeArguments.first is! VoidType) return;

reporter.reportErrorForNode(_code, node);
});
}

@override
List<Fix> getFixes() => [
_ReplaceWithAsyncCallbackType(),
];
}

class _ReplaceWithAsyncCallbackType 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 AsyncCallback',
priority: ChangePriority.replaceWithAsyncCallback,
)
.addDartFileEdit((builder) {
final delta = node.question != null ? -1 : 0;
builder.addSimpleReplacement(
node.sourceRange.getMoveEnd(delta),
'VoidCallback',
);
});
});
}
}
24 changes: 12 additions & 12 deletions packages/nilts/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ packages:
dependency: transitive
description:
name: _fe_analyzer_shared
sha256: eb376e9acf6938204f90eb3b1f00b578640d3188b4c8a8ec054f9f479af8d051
sha256: "0b2f2bd91ba804e53a61d757b986f89f1f9eaed5b11e4b2f5a2468d86d6c9fc7"
url: "https://pub.dev"
source: hosted
version: "64.0.0"
version: "67.0.0"
analyzer:
dependency: "direct main"
description:
name: analyzer
sha256: "69f54f967773f6c26c7dcb13e93d7ccee8b17a641689da39e878d5cf13b06893"
sha256: "37577842a27e4338429a1cbc32679d508836510b056f1eedf0c8d20e39c1383d"
url: "https://pub.dev"
source: hosted
version: "6.2.0"
version: "6.4.1"
analyzer_plugin:
dependency: "direct main"
description:
Expand Down Expand Up @@ -101,26 +101,26 @@ packages:
dependency: transitive
description:
name: custom_lint
sha256: f89ff83efdba7c8996e86bb3bad0b759d58f9b19ae4d0e277a386ddd8b481217
sha256: "7c0aec12df22f9082146c354692056677f1e70bc43471644d1fdb36c6fdda799"
url: "https://pub.dev"
source: hosted
version: "0.6.0"
version: "0.6.4"
custom_lint_builder:
dependency: "direct main"
description:
name: custom_lint_builder
sha256: "9cdd9987feaa6925ec5f98d64de4fbbb5d94248ff77bbf2489366efad6c4baef"
sha256: d7dc41e709dde223806660268678be7993559e523eb3164e2a1425fd6f7615a9
url: "https://pub.dev"
source: hosted
version: "0.6.0"
version: "0.6.4"
custom_lint_core:
dependency: transitive
description:
name: custom_lint_core
sha256: "9003a91409c9f1db6e2e50b4870d1d5e802e5923b25f7261bf3cb3e11ea9d4fb"
sha256: a85e8f78f4c52f6c63cdaf8c872eb573db0231dcdf3c3a5906d493c1f8bc20e6
url: "https://pub.dev"
source: hosted
version: "0.6.0"
version: "0.6.3"
dart_style:
dependency: transitive
description:
Expand Down Expand Up @@ -189,10 +189,10 @@ packages:
dependency: "direct main"
description:
name: meta
sha256: "3c74dbf8763d36539f114c799d8a2d87343b5067e9d796ca22b5eb8437090ee3"
sha256: bdb68674043280c3428e9ec998512fb681678676b3c54e773629ffe74419f8c7
url: "https://pub.dev"
source: hosted
version: "1.9.1"
version: "1.15.0"
package_config:
dependency: transitive
description:
Expand Down
4 changes: 2 additions & 2 deletions packages/nilts/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ environment:

dependencies:
analyzer: ">=5.12.0 <7.0.0"
analyzer_plugin: ^0.11.2
custom_lint_builder: ^0.6.0
analyzer_plugin: ^0.11.3
custom_lint_builder: ^0.6.4
meta: ^1.9.1
20 changes: 10 additions & 10 deletions packages/nilts_test/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ packages:
dependency: transitive
description:
name: _fe_analyzer_shared
sha256: eb376e9acf6938204f90eb3b1f00b578640d3188b4c8a8ec054f9f479af8d051
sha256: "0b2f2bd91ba804e53a61d757b986f89f1f9eaed5b11e4b2f5a2468d86d6c9fc7"
url: "https://pub.dev"
source: hosted
version: "64.0.0"
version: "67.0.0"
analyzer:
dependency: transitive
description:
name: analyzer
sha256: "69f54f967773f6c26c7dcb13e93d7ccee8b17a641689da39e878d5cf13b06893"
sha256: "37577842a27e4338429a1cbc32679d508836510b056f1eedf0c8d20e39c1383d"
url: "https://pub.dev"
source: hosted
version: "6.2.0"
version: "6.4.1"
analyzer_plugin:
dependency: transitive
description:
Expand Down Expand Up @@ -117,26 +117,26 @@ packages:
dependency: "direct dev"
description:
name: custom_lint
sha256: f89ff83efdba7c8996e86bb3bad0b759d58f9b19ae4d0e277a386ddd8b481217
sha256: "7c0aec12df22f9082146c354692056677f1e70bc43471644d1fdb36c6fdda799"
url: "https://pub.dev"
source: hosted
version: "0.6.0"
version: "0.6.4"
custom_lint_builder:
dependency: transitive
description:
name: custom_lint_builder
sha256: "9cdd9987feaa6925ec5f98d64de4fbbb5d94248ff77bbf2489366efad6c4baef"
sha256: d7dc41e709dde223806660268678be7993559e523eb3164e2a1425fd6f7615a9
url: "https://pub.dev"
source: hosted
version: "0.6.0"
version: "0.6.4"
custom_lint_core:
dependency: transitive
description:
name: custom_lint_core
sha256: "9003a91409c9f1db6e2e50b4870d1d5e802e5923b25f7261bf3cb3e11ea9d4fb"
sha256: a85e8f78f4c52f6c63cdaf8c872eb573db0231dcdf3c3a5906d493c1f8bc20e6
url: "https://pub.dev"
source: hosted
version: "0.6.0"
version: "0.6.3"
dart_style:
dependency: transitive
description:
Expand Down
2 changes: 1 addition & 1 deletion packages/nilts_test/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ dev_dependencies:
flutter_test:
sdk: flutter

custom_lint: ^0.6.0
custom_lint: ^0.6.4

nilts:
path: ../nilts
Expand Down
Loading
Loading