Skip to content

Commit ec48af6

Browse files
bwilkersoncommit-bot@chromium.org
authored andcommitted
Support fix to import a library defining an extention
Change-Id: Ife2f48eb5de0311aacd0d811973b7c160fc311da Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/115721 Commit-Queue: Brian Wilkerson <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent 4fbd758 commit ec48af6

File tree

5 files changed

+194
-9
lines changed

5 files changed

+194
-9
lines changed

pkg/analysis_server/lib/src/services/correction/fix/dart/top_level_declarations.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class TopLevelDeclaration {
3535
/// Kind of a top-level declaration.
3636
///
3737
/// We don't need it to be precise, just enough to support quick fixes.
38-
enum TopLevelDeclarationKind { type, function, variable }
38+
enum TopLevelDeclarationKind { type, extension, function, variable }
3939

4040
class TopLevelDeclarationsProvider {
4141
final DeclarationsTracker tracker;
@@ -94,6 +94,9 @@ class TopLevelDeclarationsProvider {
9494
case DeclarationKind.MIXIN:
9595
return TopLevelDeclarationKind.type;
9696
break;
97+
case DeclarationKind.EXTENSION:
98+
return TopLevelDeclarationKind.extension;
99+
break;
97100
case DeclarationKind.FUNCTION:
98101
return TopLevelDeclarationKind.function;
99102
break;

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,7 @@ class FixProcessor extends BaseProcessor {
472472
await _addFix_createFunction_forFunctionType();
473473
await _addFix_createMixin();
474474
await _addFix_importLibrary_withType();
475+
await _addFix_importLibrary_withExtension();
475476
await _addFix_importLibrary_withFunction();
476477
await _addFix_importLibrary_withTopLevelVariable();
477478
await _addFix_createLocalVariable();
@@ -2471,6 +2472,14 @@ class FixProcessor extends BaseProcessor {
24712472
}
24722473
}
24732474

2475+
Future<void> _addFix_importLibrary_withExtension() async {
2476+
String extensionName = (node as SimpleIdentifier).name;
2477+
await _addFix_importLibrary_withElement(
2478+
extensionName,
2479+
const [ElementKind.EXTENSION],
2480+
const [TopLevelDeclarationKind.extension]);
2481+
}
2482+
24742483
Future<void> _addFix_importLibrary_withFunction() async {
24752484
if (node is SimpleIdentifier) {
24762485
if (node.parent is MethodInvocation) {

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import 'fix_processor.dart';
1111
main() {
1212
defineReflectiveSuite(() {
1313
defineReflectiveTests(ImportLibraryPrefixTest);
14+
defineReflectiveTests(ImportLibraryPrefixWithExtensionMethodsTest);
1415
});
1516
}
1617

@@ -55,3 +56,35 @@ main() {
5556
''');
5657
}
5758
}
59+
60+
@reflectiveTest
61+
class ImportLibraryPrefixWithExtensionMethodsTest extends FixProcessorTest {
62+
@override
63+
FixKind get kind => DartFixKind.IMPORT_LIBRARY_PREFIX;
64+
65+
void setUp() {
66+
createAnalysisOptionsFile(experiments: ['extension-methods']);
67+
super.setUp();
68+
}
69+
70+
test_withExtension() async {
71+
addSource('/home/test/lib/lib.dart', '''
72+
class C {}
73+
extension E on int {
74+
static String m() => '';
75+
}
76+
''');
77+
await resolveTestUnit('''
78+
import 'lib.dart' as p;
79+
void f(p.C c) {
80+
print(E.m());
81+
}
82+
''');
83+
await assertHasFix('''
84+
import 'lib.dart' as p;
85+
void f(p.C c) {
86+
print(p.E.m());
87+
}
88+
''');
89+
}
90+
}

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

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,51 @@ import 'fix_processor.dart';
1212
main() {
1313
defineReflectiveSuite(() {
1414
defineReflectiveTests(ImportLibraryProject1Test);
15+
defineReflectiveTests(ImportLibraryProject1WithExtensionMethodsTest);
1516
defineReflectiveTests(ImportLibraryProject2Test);
17+
defineReflectiveTests(ImportLibraryProject2WithExtensionMethodsTest);
1618
defineReflectiveTests(ImportLibraryProject3Test);
19+
defineReflectiveTests(ImportLibraryProject3WithExtensionMethodsTest);
1720
});
1821
}
1922

23+
@reflectiveTest
24+
class ImportLibraryProject1WithExtensionMethodsTest extends FixProcessorTest {
25+
@override
26+
FixKind get kind => DartFixKind.IMPORT_LIBRARY_PROJECT1;
27+
28+
void setUp() {
29+
createAnalysisOptionsFile(experiments: ['extension-methods']);
30+
super.setUp();
31+
}
32+
33+
test_lib() async {
34+
addPackageFile('my_pkg', 'a.dart', '''
35+
extension E on int {
36+
static String m() => '';
37+
}
38+
''');
39+
newFile('/home/test/pubspec.yaml', content: r'''
40+
dependencies:
41+
my_pkg: any
42+
''');
43+
44+
await resolveTestUnit('''
45+
f() {
46+
print(E.m());
47+
}
48+
''');
49+
50+
await assertHasFix('''
51+
import 'package:my_pkg/a.dart';
52+
53+
f() {
54+
print(E.m());
55+
}
56+
''', expectedNumberOfFixesForKind: 1);
57+
}
58+
}
59+
2060
@reflectiveTest
2161
class ImportLibraryProject1Test extends FixProcessorTest {
2262
@override
@@ -512,6 +552,42 @@ main() {
512552
}
513553
}
514554

555+
@reflectiveTest
556+
class ImportLibraryProject2WithExtensionMethodsTest extends FixProcessorTest {
557+
@override
558+
FixKind get kind => DartFixKind.IMPORT_LIBRARY_PROJECT2;
559+
560+
void setUp() {
561+
createAnalysisOptionsFile(experiments: ['extension-methods']);
562+
super.setUp();
563+
}
564+
565+
test_lib_src() async {
566+
addPackageFile('my_pkg', 'a.dart', "export 'src/b.dart';");
567+
addPackageFile('my_pkg', 'src/b.dart', '''
568+
extension E on int {
569+
static String m() => '';
570+
}
571+
''');
572+
newFile('/home/test/pubspec.yaml', content: r'''
573+
dependencies:
574+
my_pkg: any
575+
''');
576+
await resolveTestUnit('''
577+
f() {
578+
print(E.m());
579+
}
580+
''');
581+
await assertHasFix('''
582+
import 'package:my_pkg/a.dart';
583+
584+
f() {
585+
print(E.m());
586+
}
587+
''');
588+
}
589+
}
590+
515591
@reflectiveTest
516592
class ImportLibraryProject2Test extends FixProcessorTest {
517593
@override
@@ -564,6 +640,37 @@ main() {
564640
}
565641
}
566642

643+
@reflectiveTest
644+
class ImportLibraryProject3WithExtensionMethodsTest extends FixProcessorTest {
645+
@override
646+
FixKind get kind => DartFixKind.IMPORT_LIBRARY_PROJECT3;
647+
648+
void setUp() {
649+
createAnalysisOptionsFile(experiments: ['extension-methods']);
650+
super.setUp();
651+
}
652+
653+
test_inLibSrc_thisContextRoot() async {
654+
addSource('/home/test/lib/src/lib.dart', '''
655+
extension E on int {
656+
static String m() => '';
657+
}
658+
''');
659+
await resolveTestUnit('''
660+
f() {
661+
print(E.m());
662+
}
663+
''');
664+
await assertHasFix('''
665+
import 'package:test/src/lib.dart';
666+
667+
f() {
668+
print(E.m());
669+
}
670+
''');
671+
}
672+
}
673+
567674
@reflectiveTest
568675
class ImportLibraryProject3Test extends FixProcessorTest {
569676
@override

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

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import 'fix_processor.dart';
1111
main() {
1212
defineReflectiveSuite(() {
1313
defineReflectiveTests(ImportLibraryShowTest);
14+
defineReflectiveTests(ImportLibraryShowWithExtensionMethodsTest);
1415
});
1516
}
1617

@@ -24,39 +25,71 @@ class ImportLibraryShowTest extends FixProcessorTest {
2425
class A {}
2526
class B {}
2627
''');
27-
await resolveTestUnit('''
28+
await resolveTestUnit(r'''
2829
import 'lib.dart' show A;
2930
main() {
3031
A a;
3132
B b;
32-
print('\$a \$b');
33+
print('$a $b');
3334
}
3435
''');
35-
await assertHasFix('''
36+
await assertHasFix(r'''
3637
import 'lib.dart' show A, B;
3738
main() {
3839
A a;
3940
B b;
40-
print('\$a \$b');
41+
print('$a $b');
4142
}
4243
''');
4344
}
4445

4546
test_sdk() async {
46-
await resolveTestUnit('''
47+
await resolveTestUnit(r'''
4748
import 'dart:collection' show HashMap;
4849
main() {
4950
HashMap s = null;
5051
LinkedHashMap f = null;
51-
print('\$s \$f');
52+
print('$s $f');
5253
}
5354
''');
54-
await assertHasFix('''
55+
await assertHasFix(r'''
5556
import 'dart:collection' show HashMap, LinkedHashMap;
5657
main() {
5758
HashMap s = null;
5859
LinkedHashMap f = null;
59-
print('\$s \$f');
60+
print('$s $f');
61+
}
62+
''');
63+
}
64+
}
65+
66+
@reflectiveTest
67+
class ImportLibraryShowWithExtensionMethodsTest extends FixProcessorTest {
68+
@override
69+
FixKind get kind => DartFixKind.IMPORT_LIBRARY_SHOW;
70+
71+
void setUp() {
72+
createAnalysisOptionsFile(experiments: ['extension-methods']);
73+
super.setUp();
74+
}
75+
76+
test_package() async {
77+
addSource('/home/test/lib/lib.dart', '''
78+
class A {}
79+
extension E on int {
80+
static String m() => '';
81+
}
82+
''');
83+
await resolveTestUnit(r'''
84+
import 'lib.dart' show A;
85+
void f(A a) {
86+
print('$a ${E.m()}');
87+
}
88+
''');
89+
await assertHasFix(r'''
90+
import 'lib.dart' show A, E;
91+
void f(A a) {
92+
print('$a ${E.m()}');
6093
}
6194
''');
6295
}

0 commit comments

Comments
 (0)