Description
How to observe the problem:
$ cat analysis_options.yaml
linter:
rules:
- always_declare_return_types
- annotate_overrides
$ cat lib/example_lib.dart
class A {
foo() {
return 5;
}
}
class B extends A {
foo() {
return 6;
}
}
void main() {
}
$ dart fix --apply lib/example_lib.dart
example_lib.dart
always_declare_return_types • 3 fixes
annotate_overrides • 1 fix
4 fixes made in 1 file.
$ cat lib/example_lib.dart
class A {
int foo() {
return 5;
}
}
class B extends A {
int @override
int foo() {
return 6;
}
}
void main() {
}
The problem is that foo() {
- got turned into
int @override foo() {
- but should've been
@override int foo() {
And then due to the repeated re-run behavior of dart fix
, yet another int
got inserted because the previous one wasn't acknowledged.
I showed how this can be tried out with dart fix
, but the problem is not specific to it, the bug already applies to the edit.bulkFixes
handler itself.
The way this manifests is that the handler returns both of these fixes at the same offset, in this order:
- offset 123, length 0, replacement
'@override\n '
- offset 123, length 0, replacement
'int '
And they get applied as is in this order.
Dart SDK version: 2.19.0-361.0.dev and also latest main
Here are some pieces of code that I suspect. I tried to fix this by changing these, but eventually gave up.
- Maybe changing the priority would be part of the fix?
sdk/pkg/analysis_server/lib/src/services/correction/fix.dart
Lines 231 to 235 in ea9b19c
- Maybe switching to stable sort would be part of the fix?
- Maybe it's something about how 0-length edits are special-cased?
sdk/pkg/analyzer_plugin/lib/src/protocol/protocol_internal.dart
Lines 48 to 54 in ea9b19c