quick fix for unnecessary_library_name

Change-Id: I22f7734266050c7d06c8ee5809af6912ea39c4e0
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/358681
Commit-Queue: Phil Quitslund <pquitslund@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
pq 2024-03-21 01:35:09 +00:00 committed by Commit Queue
parent 6e32311766
commit 070eac1826
7 changed files with 93 additions and 1 deletions

View file

@ -0,0 +1,33 @@
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
import 'package:analyzer_plugin/utilities/range_factory.dart';
class RemoveLibraryName extends ResolvedCorrectionProducer {
@override
bool get canBeAppliedInBulk => true;
@override
// There can only be one library declaration per file.
bool get canBeAppliedToFile => false;
@override
FixKind get fixKind => DartFixKind.REMOVE_LIBRARY_NAME;
@override
Future<void> compute(ChangeBuilder builder) async {
var libraryName = node;
if (libraryName is SimpleIdentifier || libraryName is LibraryIdentifier) {
await builder.addDartFileEdit(file, (builder) {
builder.addDeletion(range.endStart(
libraryName.beginToken.previous!, libraryName.endToken.next!));
});
}
}
}

View file

@ -2384,7 +2384,7 @@ LintCode.unnecessary_late:
LintCode.unnecessary_library_directive:
status: hasFix
LintCode.unnecessary_library_name:
status: needsFix
status: hasFix
LintCode.unnecessary_new:
status: hasFix
LintCode.no_wildcard_variable_uses:

View file

@ -1135,6 +1135,11 @@ class DartFixKind {
DartFixKindPriority.IN_FILE,
'Remove leading underscores in file',
);
static const REMOVE_LIBRARY_NAME = FixKind(
'dart.fix.remove.library.name',
DartFixKindPriority.DEFAULT,
'Remove the library name',
);
static const REMOVE_METHOD_DECLARATION = FixKind(
'dart.fix.remove.methodDeclaration',
DartFixKindPriority.DEFAULT,

View file

@ -141,6 +141,7 @@ import 'package:analysis_server/src/services/correction/dart/remove_interpolatio
import 'package:analysis_server/src/services/correction/dart/remove_invocation.dart';
import 'package:analysis_server/src/services/correction/dart/remove_late.dart';
import 'package:analysis_server/src/services/correction/dart/remove_leading_underscore.dart';
import 'package:analysis_server/src/services/correction/dart/remove_library_name.dart';
import 'package:analysis_server/src/services/correction/dart/remove_method_declaration.dart';
import 'package:analysis_server/src/services/correction/dart/remove_name_from_combinator.dart';
import 'package:analysis_server/src/services/correction/dart/remove_name_from_declaration_clause.dart';
@ -593,6 +594,9 @@ final _builtInLintProducers = <String, List<ProducerGenerator>>{
LintNames.unnecessary_library_directive: [
RemoveUnnecessaryLibraryDirective.new,
],
LintNames.unnecessary_library_name: [
RemoveLibraryName.new,
],
LintNames.unnecessary_new: [
RemoveUnnecessaryNew.new,
],

View file

@ -170,6 +170,7 @@ class LintNames {
static const String unnecessary_late = 'unnecessary_late';
static const String unnecessary_library_directive =
'unnecessary_library_directive';
static const String unnecessary_library_name = 'unnecessary_library_name';
static const String unnecessary_new = 'unnecessary_new';
static const String unnecessary_null_aware_assignments =
'unnecessary_null_aware_assignments';

View file

@ -0,0 +1,47 @@
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analysis_server/src/services/linter/lint_names.dart';
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
import 'fix_processor.dart';
void main() {
defineReflectiveSuite(() {
defineReflectiveTests(RemoveLibraryNameTest);
});
}
@reflectiveTest
class RemoveLibraryNameTest extends FixProcessorLintTest {
@override
FixKind get kind => DartFixKind.REMOVE_LIBRARY_NAME;
@override
String get lintCode => LintNames.unnecessary_library_name;
Future<void> test_namedLibrary_libraryIdentifier() async {
await resolveTestCode('''
/// A library.
library l.m.n;
''');
await assertHasFix('''
/// A library.
library;
''');
}
Future<void> test_namedLibrary_simpleId() async {
await resolveTestCode('''
/// A library.
library l;
''');
await assertHasFix('''
/// A library.
library;
''');
}
}

View file

@ -181,6 +181,7 @@ import 'remove_interpolation_braces_test.dart' as remove_interpolation_braces;
import 'remove_invocation_test.dart' as remove_invocation;
import 'remove_late_test.dart' as remove_late;
import 'remove_leading_underscore_test.dart' as remove_leading_underscore;
import 'remove_library_name_test.dart' as remove_library_name;
import 'remove_method_declaration_test.dart' as remove_method_declaration;
import 'remove_name_from_combinator_test.dart' as remove_name_from_combinator;
import 'remove_name_from_declaration_clause_test.dart'
@ -438,6 +439,7 @@ void main() {
remove_invocation.main();
remove_late.main();
remove_leading_underscore.main();
remove_library_name.main();
remove_method_declaration.main();
remove_name_from_combinator.main();
remove_name_from_declaration_clause.main();