Skip to content

Commit f0834db

Browse files
authored
Support --fix and fix-all assist (#223)
1 parent f6f020a commit f0834db

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1512
-308
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ To create a custom lint, you will need two things:
8282
# pubspec.yaml
8383
name: my_custom_lint_package
8484
environment:
85-
sdk: ">=2.16.0 <3.0.0"
85+
sdk: ">=3.0.0 <4.0.0"
8686

8787
dependencies:
8888
# we will use analyzer for inspecting Dart files
@@ -166,7 +166,7 @@ For users to run custom_lint packages, there are a few steps:
166166
# The pubspec.yaml of an application using our lints
167167
name: example_app
168168
environment:
169-
sdk: ">=2.16.0 <3.0.0"
169+
sdk: ">=3.0.0 <4.0.0"
170170
171171
dev_dependencies:
172172
custom_lint:

packages/custom_lint/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## Unreleased patch
2+
3+
- Added support for `--fix`
4+
15
## 0.5.11 - 2024-01-27
26

37
- Added support for `analysis_options.yaml` that are nt at the root of the project (thanks to @mrgnhnt96)

packages/custom_lint/bin/custom_lint.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ Future<void> entrypoint([List<String> args = const []]) async {
3939
help: "Watches plugins' sources and perform a hot-reload on change",
4040
negatable: false,
4141
)
42+
..addFlag(
43+
'fix',
44+
help: 'Apply all possible fixes to the lint issues found.',
45+
negatable: false,
46+
)
4247
..addFlag(
4348
'help',
4449
abbr: 'h',
@@ -55,6 +60,7 @@ Future<void> entrypoint([List<String> args = const []]) async {
5560
}
5661

5762
final watchMode = result['watch'] as bool;
63+
final fix = result['fix'] as bool;
5864
final fatalInfos = result['fatal-infos'] as bool;
5965
final fatalWarnings = result['fatal-warnings'] as bool;
6066
final format = result['format'] as String;
@@ -64,6 +70,7 @@ Future<void> entrypoint([List<String> args = const []]) async {
6470
watchMode: watchMode,
6571
fatalInfos: fatalInfos,
6672
fatalWarnings: fatalWarnings,
73+
fix: fix,
6774
format: OutputFormatEnum.fromName(format),
6875
);
6976
}

packages/custom_lint/example/example_lint/lib/custom_lint_example_lint.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class _MakeProviderFinalFix extends DartFix {
112112
message: 'Make provider final',
113113
// This represents how high-low should this quick-fix show-up in the list
114114
// of quick-fixes.
115-
priority: 1,
115+
priority: 10,
116116
);
117117

118118
// Our edit will consist of editing a Dart file, so we invoke "addDartFileEdit".

packages/custom_lint/lib/custom_lint.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Future<void> customLint({
4040
bool fatalInfos = true,
4141
bool fatalWarnings = true,
4242
OutputFormatEnum format = OutputFormatEnum.plain,
43+
bool fix = false,
4344
}) async {
4445
// Reset the code
4546
exitCode = 0;
@@ -53,6 +54,7 @@ Future<void> customLint({
5354
fatalInfos: fatalInfos,
5455
fatalWarnings: fatalWarnings,
5556
format: format,
57+
fix: fix,
5658
);
5759
} catch (_) {
5860
exitCode = 1;
@@ -68,10 +70,12 @@ Future<void> _runServer(
6870
required bool fatalInfos,
6971
required bool fatalWarnings,
7072
required OutputFormatEnum format,
73+
required bool fix,
7174
}) async {
7275
final customLintServer = await CustomLintServer.start(
7376
sendPort: channel.receivePort.sendPort,
7477
watchMode: watchMode,
78+
fix: fix,
7579
workingDirectory: workingDirectory,
7680
// In the CLI, only show user defined lints. Errors & logs will be
7781
// rendered separately

packages/custom_lint/lib/src/analyzer_plugin_starter.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ void start(Iterable<String> _, SendPort sendPort) {
1313
CustomLintServer.start(
1414
sendPort: sendPort,
1515
includeBuiltInLints: true,
16+
// The IDE client should write to files, as what's visible in the editor
17+
// may not be the same as what's on disk.
18+
fix: false,
1619
// "start" may be run by `dart analyze`, in which case we don't want to
1720
// enable watch mode. There's no way to detect this, but this only matters
18-
// in the CI. So we disble watch mode if we detect that we're in CI.
21+
// in the CI. So we disable watch mode if we detect that we're in CI.
1922
// TODO enable hot-restart only if running plugin from source (excluding pub cache)
2023
watchMode: !isInCI,
2124
delegate: AnalyzerPluginCustomLintDelegate(),

packages/custom_lint/lib/src/v2/custom_lint_analyzer_plugin.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class CustomLintServer {
2828
required this.includeBuiltInLints,
2929
required this.delegate,
3030
required this.workingDirectory,
31+
required this.fix,
3132
});
3233

3334
/// Start the server while also capturing prints and errors.
@@ -38,6 +39,7 @@ class CustomLintServer {
3839
required SendPort sendPort,
3940
required bool watchMode,
4041
required bool includeBuiltInLints,
42+
required bool fix,
4143
required CustomLintDelegate delegate,
4244
required Directory workingDirectory,
4345
}) {
@@ -48,6 +50,7 @@ class CustomLintServer {
4850
() {
4951
server = CustomLintServer._(
5052
watchMode: watchMode,
53+
fix: fix,
5154
includeBuiltInLints: includeBuiltInLints,
5255
delegate: delegate,
5356
workingDirectory: workingDirectory,
@@ -96,6 +99,10 @@ class CustomLintServer {
9699
/// Whether plugins should be started in watch mode
97100
final bool watchMode;
98101

102+
/// If enabled, attempt to fix all issues found before reporting them.
103+
/// Can only be enabled in the CLI.
104+
final bool fix;
105+
99106
/// Whether plugins should include lints used for debugging.
100107
final bool includeBuiltInLints;
101108

packages/custom_lint/lib/src/v2/server_to_client_channel.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ void main(List<String> args) async {
209209
runSocket(
210210
port: port,
211211
host: host,
212+
fix: ${_server.fix},
212213
includeBuiltInLints: ${_server.includeBuiltInLints},
213214
{$plugins},
214215
);

packages/custom_lint/lib/src/workspace.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ publish_to: 'none'
562562
.toList();
563563

564564
final constraintCompatibleWithAllProjects = projectMeta.fold(
565-
VersionConstraint.any,
565+
VersionConstraint.parse('^3.0.0'),
566566
(acc, constraint) => acc.intersect(constraint.constraint),
567567
);
568568

packages/custom_lint/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ dev_dependencies:
3232
build_runner: ^2.3.2
3333
file: ^7.0.0
3434
freezed: ^2.3.2
35+
glob: ^2.1.2
3536
json_serializable: ^6.5.4
3637
test: ^1.20.2
3738
test_process: ^2.1.0

0 commit comments

Comments
 (0)