Skip to content

Commit 19da943

Browse files
FMorschelCommit Queue
authored andcommitted
[DAS] Fixes wrong prefixes
[email protected] Fixes #57090 Change-Id: I4a6597b174216dcdb8ce1801437b539e7f47136c Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/396563 Reviewed-by: Samuel Rawlins <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]> Auto-Submit: Felipe Morschel <[email protected]> Reviewed-by: Phil Quitslund <[email protected]>
1 parent db021fa commit 19da943

File tree

4 files changed

+129
-10
lines changed

4 files changed

+129
-10
lines changed

pkg/analysis_server/lib/src/services/correction/dart/import_library.dart

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import 'package:analyzer_plugin/src/utilities/change_builder/change_builder_dart
2121
import 'package:analyzer_plugin/src/utilities/library.dart';
2222
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
2323
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
24+
import 'package:analyzer_plugin/utilities/range_factory.dart';
2425

2526
class ImportLibrary extends MultiCorrectionProducer {
2627
final _ImportKind _importKind;
@@ -212,7 +213,8 @@ class ImportLibrary extends MultiCorrectionProducer {
212213
continue;
213214
}
214215
_ImportLibraryCombinator? combinatorProducer;
215-
// Maybe update a "show" directive.
216+
var importPrefix = import.prefix?.element;
217+
// Maybe update a "show"/"hide" directive.
216218
var combinators = import.combinators;
217219
if (combinators.length == 1) {
218220
// Prepare library name - unit name or 'dart:name' for SDK library.
@@ -229,6 +231,7 @@ class ImportLibrary extends MultiCorrectionProducer {
229231
libraryName,
230232
combinator,
231233
name,
234+
removePrefix: importPrefix == null,
232235
context: context,
233236
);
234237
} else if (combinator is ShowElementCombinator) {
@@ -238,18 +241,19 @@ class ImportLibrary extends MultiCorrectionProducer {
238241
libraryName,
239242
combinator,
240243
name,
244+
removePrefix: importPrefix == null,
241245
context: context,
242246
);
243247
}
244248
}
245249
// Maybe apply a prefix.
246-
var prefix = import.prefix?.element;
247-
if (prefix != null) {
250+
if (importPrefix != null) {
248251
producers.add(
249252
_ImportLibraryPrefix(
250253
libraryElement,
251-
prefix,
254+
importPrefix,
252255
combinatorProducer,
256+
prefix,
253257
context: context,
254258
),
255259
);
@@ -593,12 +597,15 @@ class _ImportLibraryCombinator extends ResolvedCorrectionProducer {
593597

594598
final String _updatedName;
595599

600+
final bool _removePrefix;
601+
596602
_ImportLibraryCombinator(
597603
this._libraryName,
598604
this._combinator,
599605
this._updatedName, {
606+
bool removePrefix = false,
600607
required super.context,
601-
});
608+
}) : _removePrefix = removePrefix;
602609

603610
@override
604611
CorrectionApplicability get applicability =>
@@ -643,6 +650,20 @@ class _ImportLibraryCombinator extends ResolvedCorrectionProducer {
643650
SourceRange(offset - 1, length + 1),
644651
newCombinatorCode,
645652
);
653+
if (_removePrefix) {
654+
AstNode? prefix;
655+
if (node case NamedType(:var importPrefix?)) {
656+
prefix = importPrefix;
657+
} else if (node case PrefixedIdentifier(:var prefix)) {
658+
prefix = prefix;
659+
} else {
660+
return;
661+
}
662+
if (prefix == null) {
663+
return;
664+
}
665+
builder.addDeletion(range.node(prefix));
666+
}
646667
});
647668
}
648669
}
@@ -699,12 +720,14 @@ class _ImportLibraryContainingExtension extends ResolvedCorrectionProducer {
699720
class _ImportLibraryPrefix extends ResolvedCorrectionProducer {
700721
final LibraryElement _importedLibrary;
701722
final PrefixElement _importPrefix;
723+
final String? _nodePrefix;
702724
final _ImportLibraryCombinator? _editCombinator;
703725

704726
_ImportLibraryPrefix(
705727
this._importedLibrary,
706728
this._importPrefix,
707-
this._editCombinator, {
729+
this._editCombinator,
730+
this._nodePrefix, {
708731
required super.context,
709732
});
710733

@@ -735,9 +758,23 @@ class _ImportLibraryPrefix extends ResolvedCorrectionProducer {
735758

736759
await _editCombinator?.compute(builder);
737760

738-
await builder.addDartFileEdit(file, (builder) {
739-
builder.addSimpleInsertion(targetNode.offset, '$_prefixName.');
740-
});
761+
if (_nodePrefix == null) {
762+
await builder.addDartFileEdit(file, (builder) {
763+
builder.addSimpleInsertion(targetNode.offset, '$_prefixName.');
764+
});
765+
} else if (_nodePrefix != _prefixName) {
766+
AstNode prefix;
767+
if (targetNode case NamedType(:var importPrefix?)) {
768+
prefix = importPrefix;
769+
} else if (targetNode case PrefixedIdentifier(prefix: var prefixNode)) {
770+
prefix = prefixNode;
771+
} else {
772+
return;
773+
}
774+
await builder.addDartFileEdit(file, (builder) {
775+
builder.addSimpleReplacement(range.node(prefix), '$_prefixName.');
776+
});
777+
}
741778
}
742779
}
743780

pkg/analysis_server/test/src/services/correction/fix/fix_processor.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ abstract class FixProcessorTest extends BaseFixProcessorTest {
318318
/// [expected] output.
319319
Future<void> assertHasFix(
320320
String expected, {
321-
bool Function(AnalysisError)? errorFilter,
321+
bool Function(AnalysisError error)? errorFilter,
322322
String? target,
323323
int? expectedNumberOfFixesForKind,
324324
String? matchFixMessage,

pkg/analysis_server/test/src/services/correction/fix/import_library_hide_test.dart

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,31 @@ void f(String s, lib.C c) {
4242
''');
4343
}
4444

45+
Future<void> test_extension_hidden_class() async {
46+
newFile('$testPackageLibPath/lib.dart', '''
47+
class C {}
48+
extension E on String {
49+
int get m => 0;
50+
}
51+
''');
52+
await resolveTestCode('''
53+
import 'package:test/lib.dart' as lib hide C;
54+
import 'package:test/lib.dart' hide C;
55+
56+
void f(String s, lib.C c) {
57+
s.m;
58+
}
59+
''');
60+
await assertHasFix('''
61+
import 'package:test/lib.dart' as lib hide C;
62+
import 'package:test/lib.dart';
63+
64+
void f(String s, C c) {
65+
s.m;
66+
}
67+
''');
68+
}
69+
4570
Future<void> test_extension_hidden_getter() async {
4671
newFile('$testPackageLibPath/lib.dart', '''
4772
class C {}

pkg/analysis_server/test/src/services/correction/fix/import_library_prefix_test.dart

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:analysis_server/src/services/correction/fix.dart';
6+
import 'package:analyzer/src/error/codes.dart';
67
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
78
import 'package:test_reflective_loader/test_reflective_loader.dart';
89

@@ -47,6 +48,62 @@ void f(lib.C c) {
4748
''');
4849
}
4950

51+
Future<void> test_with_hideCombinator() async {
52+
newFile('$testPackageLibPath/lib.dart', '''
53+
class A {}
54+
class B {}
55+
''');
56+
await resolveTestCode(r'''
57+
import 'lib.dart' as lib hide A;
58+
void f() {
59+
lib.A? a;
60+
lib.B b;
61+
print('$a $b');
62+
}
63+
''');
64+
await assertHasFix(
65+
r'''
66+
import 'lib.dart' as lib;
67+
void f() {
68+
lib.A? a;
69+
lib.B b;
70+
print('$a $b');
71+
}
72+
''',
73+
errorFilter: (error) {
74+
return error.errorCode == CompileTimeErrorCode.UNDEFINED_CLASS;
75+
},
76+
);
77+
}
78+
79+
Future<void> test_with_hideCombinator_differentPrefix() async {
80+
newFile('$testPackageLibPath/lib.dart', '''
81+
class A {}
82+
class B {}
83+
''');
84+
await resolveTestCode(r'''
85+
import 'lib.dart' as lib hide A;
86+
void f() {
87+
lib2.A? a;
88+
lib.B b;
89+
print('$a $b');
90+
}
91+
''');
92+
await assertHasFix(
93+
r'''
94+
import 'lib.dart' as lib;
95+
void f() {
96+
lib.A? a;
97+
lib.B b;
98+
print('$a $b');
99+
}
100+
''',
101+
errorFilter: (error) {
102+
return error.errorCode == CompileTimeErrorCode.UNDEFINED_CLASS;
103+
},
104+
);
105+
}
106+
50107
Future<void> test_with_showCombinator() async {
51108
newFile('$testPackageLibPath/lib.dart', '''
52109
class A {}

0 commit comments

Comments
 (0)