Skip to content

Commit 3f5bac9

Browse files
FMorschelCommit Queue
authored andcommitted
[DAS] Fixes import library for extension members in pattern matching
Bug: #61883 Change-Id: Ib661e1278d6aae30f554373e3aefc79020f81684 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/473860 Reviewed-by: Samuel Rawlins <srawlins@google.com> Auto-Submit: Felipe Morschel <git@fmorschel.dev> Commit-Queue: Brian Wilkerson <brianwilkerson@google.com> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
1 parent 1d91605 commit 3f5bac9

2 files changed

Lines changed: 69 additions & 0 deletions

File tree

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,18 @@ class ImportLibrary extends MultiCorrectionProducer {
102102
};
103103
}
104104

105+
DartType? _getTypeForPattern(AstNode node) {
106+
var parent = node.parent;
107+
while (parent is DartPattern || parent?.parent is DartPattern) {
108+
if (parent is ObjectPattern) {
109+
return parent.type.type;
110+
}
111+
parent = parent?.parent;
112+
}
113+
// We don't know how to answer this.
114+
return null;
115+
}
116+
105117
Future<(_ImportLibraryCombinator?, _ImportLibraryCombinatorMultiple?)>
106118
_importEditCombinators(
107119
LibraryImport import,
@@ -442,6 +454,15 @@ class ImportLibrary extends MultiCorrectionProducer {
442454
node.operator.type == TokenType.TILDE) {
443455
targetType = node.operand.staticType;
444456
}
457+
} else if (node is PatternFieldName) {
458+
memberName = node.name?.lexeme ?? '';
459+
if (memberName.isEmpty) {
460+
return const [];
461+
}
462+
targetType = _getTypeForPattern(node);
463+
} else if (node is DeclaredVariablePattern) {
464+
memberName = node.name.lexeme;
465+
targetType = _getTypeForPattern(node);
445466
} else {
446467
return const [];
447468
}

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,54 @@ void f() {
848848
await assertNoFix();
849849
}
850850

851+
Future<void> test_pattern() async {
852+
newFile('$testPackageLibPath/a.dart', '''
853+
extension IntExt on int {
854+
int get foo => 0;
855+
}
856+
''');
857+
858+
await resolveTestCode('''
859+
void f(Object o) {
860+
if (o case int(foo: int())) {}
861+
}
862+
''');
863+
864+
await assertHasFix('''
865+
import 'package:test/a.dart';
866+
867+
void f(Object o) {
868+
if (o case int(foo: int())) {}
869+
}
870+
''');
871+
}
872+
873+
Future<void> test_pattern_simplified() async {
874+
newFile('$testPackageLibPath/a.dart', '''
875+
extension IntExt on int {
876+
int get foo => 0;
877+
}
878+
''');
879+
880+
await resolveTestCode('''
881+
void f(Object o) {
882+
if (o case int(:var foo)) {
883+
print(foo);
884+
}
885+
}
886+
''');
887+
888+
await assertHasFix('''
889+
import 'package:test/a.dart';
890+
891+
void f(Object o) {
892+
if (o case int(:var foo)) {
893+
print(foo);
894+
}
895+
}
896+
''');
897+
}
898+
851899
Future<void> test_relativeDirective() async {
852900
newFile('$testPackageLibPath/a.dart', '''
853901
class Foo {}

0 commit comments

Comments
 (0)