[dart fix] Support for fixes when only part files have diagnostics.
Fixes https://github.com/dart-lang/sdk/issues/55520
Change-Id: Ibebe155e00758a6bf36e0327c130be70f2910238
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/374100
Commit-Queue: Keerti Parthasarathy <keertip@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
diff --git a/pkg/analysis_server/lib/src/services/correction/bulk_fix_processor.dart b/pkg/analysis_server/lib/src/services/correction/bulk_fix_processor.dart
index 58e72ce..2ad2101 100644
--- a/pkg/analysis_server/lib/src/services/correction/bulk_fix_processor.dart
+++ b/pkg/analysis_server/lib/src/services/correction/bulk_fix_processor.dart
@@ -485,9 +485,17 @@
}
var library = await context.currentSession.getResolvedLibrary(path);
+
if (isCancelled) {
break;
}
+ if (library is NotLibraryButPartResult) {
+ var unit = await context.currentSession.getResolvedUnit(path);
+ if (unit is ResolvedUnitResult) {
+ library = await context.currentSession
+ .getResolvedLibraryByElement(unit.libraryElement);
+ }
+ }
if (library is ResolvedLibraryResult) {
await _fixErrorsInLibrary(library, stopAfterFirst: stopAfterFirst);
if (isCancelled || (stopAfterFirst && changeMap.hasFixes)) {
diff --git a/pkg/analysis_server/test/src/services/correction/fix/bulk_fix_processor_test.dart b/pkg/analysis_server/test/src/services/correction/fix/bulk_fix_processor_test.dart
index 0aafb4a..53a4070 100644
--- a/pkg/analysis_server/test/src/services/correction/fix/bulk_fix_processor_test.dart
+++ b/pkg/analysis_server/test/src/services/correction/fix/bulk_fix_processor_test.dart
@@ -88,6 +88,59 @@
expect(await computeHasFixes(), isTrue);
}
+ Future<void> test_hasFixes_in_part() async {
+ createAnalysisOptionsFile(experiments: experiments, lints: [
+ LintNames.unnecessary_new,
+ ]);
+
+ newFile('$testPackageLibPath/a.dart', '''
+part of 'test.dart';
+
+class A { }
+
+var a = new A();
+''');
+
+ await resolveTestCode('''
+part 'a.dart';
+''');
+
+ expect(await computeHasFixes(), isTrue);
+ }
+
+ Future<void> test_hasFixes_in_part_and_library() async {
+ createAnalysisOptionsFile(experiments: experiments, lints: [
+ LintNames.unnecessary_new,
+ ]);
+
+ newFile('$testPackageLibPath/a.dart', '''
+part of 'test.dart';
+
+class A { }
+
+var a = new A();
+''');
+
+ newFile('$testPackageLibPath/b.dart', '''
+part of 'test.dart';
+
+class B { }
+
+var b = new B();
+''');
+
+ await resolveTestCode('''
+part 'a.dart';
+part 'b.dart';
+
+class C{}
+var c = new C();
+''');
+
+ expect(await computeHasFixes(), isTrue);
+ expect(processor.changeMap.libraryMap.length, 3);
+ }
+
Future<void> test_hasFixes_stoppedAfterFirst() async {
createAnalysisOptionsFile(experiments: experiments, lints: [
LintNames.annotate_overrides,