DAS import_library: lower the priority of importing a deprecated name or library Fixes https://github.com/dart-lang/sdk/issues/42878 The naming is weird but the other kinds are also named '...1', ..., '...3'. If we want to rename them I think that would be a good idea for a subsequent CL. Change-Id: I05609c1c682d05e663c486aaf82e6131c5f3157f Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/541061 Commit-Queue: Samuel Rawlins <srawlins@google.com> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/import_library.dart b/pkg/analysis_server/lib/src/services/correction/dart/import_library.dart index 3a63ee87..fdd20d8 100644 --- a/pkg/analysis_server/lib/src/services/correction/dart/import_library.dart +++ b/pkg/analysis_server/lib/src/services/correction/dart/import_library.dart
@@ -6,6 +6,7 @@ import 'package:analysis_server/src/services/correction/fix.dart'; import 'package:analysis_server/src/services/correction/namespace.dart'; +import 'package:analysis_server/src/utilities/extensions/element.dart'; import 'package:analysis_server/src/utilities/extensions/iterable.dart'; import 'package:analysis_server_plugin/edit/dart/correction_producer.dart'; import 'package:analysis_server_plugin/edit/fix/dart_fix_context.dart'; @@ -296,7 +297,15 @@ // Compute the fix kind. FixKind fixKind; FixKind fixKindShow; - if (libraryElement.isInSdk) { + if (declaration.hasOrInheritsDeprecated || + libraryElement.hasOrInheritsDeprecated) { + fixKind = prefix.isEmptyOrNull + ? DartFixKind.importLibraryProject4 + : DartFixKind.importLibraryProject4Prefixed; + fixKindShow = prefix.isEmptyOrNull + ? DartFixKind.importLibraryProject4Show + : DartFixKind.importLibraryProject4PrefixedShow; + } else if (libraryElement.isInSdk) { fixKind = prefix.isEmptyOrNull ? DartFixKind.importLibrarySdk : DartFixKind.importLibrarySdkPrefixed;
diff --git a/pkg/analysis_server/lib/src/services/correction/fix.dart b/pkg/analysis_server/lib/src/services/correction/fix.dart index b090f31..f8b6195 100644 --- a/pkg/analysis_server/lib/src/services/correction/fix.dart +++ b/pkg/analysis_server/lib/src/services/correction/fix.dart
@@ -1055,6 +1055,36 @@ DartFixKindPriority.standard + 3, "Import library '{0}' with 'show'", ); + + /// {@template dart.fix.import.libraryProject4} + /// Import a deprecated library or declaration. + /// {@endtemplate} + static const importLibraryProject4 = FixKind( + 'dart.fix.import.libraryProject4', + DartFixKindPriority.standard + 2, + "Import deprecated library '{0}'", + ); + + /// {@macro dart.fix.import.libraryProject4} + static const importLibraryProject4Prefixed = FixKind( + 'dart.fix.import.libraryProject4Prefixed', + DartFixKindPriority.standard + 2, + "Import deprecated library '{0}' with prefix '{1}'", + ); + + /// {@macro dart.fix.import.libraryProject4} + static const importLibraryProject4PrefixedShow = FixKind( + 'dart.fix.import.libraryProject4PrefixedShow', + DartFixKindPriority.standard + 2, + "Import deprecated library '{0}' with prefix '{1}' and 'show'", + ); + + /// {@macro dart.fix.import.libraryProject4} + static const importLibraryProject4Show = FixKind( + 'dart.fix.import.libraryProject4Show', + DartFixKindPriority.standard + 2, + "Import deprecated library '{0}' with 'show'", + ); static const importLibraryRemoveShow = FixKind( 'dart.fix.import.libraryRemoveShow', DartFixKindPriority.standard - 1,
diff --git a/pkg/analysis_server/test/src/services/correction/fix/import_library_project_test.dart b/pkg/analysis_server/test/src/services/correction/fix/import_library_project_test.dart index e1c7810..4006fb4 100644 --- a/pkg/analysis_server/test/src/services/correction/fix/import_library_project_test.dart +++ b/pkg/analysis_server/test/src/services/correction/fix/import_library_project_test.dart
@@ -24,6 +24,7 @@ defineReflectiveTests(ImportLibraryProject3PrefixedWithShowTest); defineReflectiveTests(ImportLibraryProject3Test); defineReflectiveTests(ImportLibraryProject3WithShowTest); + defineReflectiveTests(ImportLibraryProject4Test); }); } @@ -2872,3 +2873,45 @@ '''); } } + +@reflectiveTest +class ImportLibraryProject4Test extends FixProcessorTest { + @override + FixKind get kind => DartFixKind.importLibraryProject4; + + Future<void> test_deprecatedClass() async { + newFile('$testPackageLibPath/a.dart', ''' +@deprecated +class Test {} +'''); + + await resolveTestCode(''' +void f(Test t) {} +'''); + + await assertHasFix(''' +import 'package:test/a.dart'; + +void f(Test t) {} +'''); + } + + Future<void> test_deprecatedLibrary() async { + newFile('$testPackageLibPath/a.dart', ''' +@deprecated +library a; + +class Test {} +'''); + + await resolveTestCode(''' +void f(Test t) {} +'''); + + await assertHasFix(''' +import 'package:test/a.dart'; + +void f(Test t) {} +'''); + } +}