Skip to content

Commit 37f7206

Browse files
pqCommit Queue
authored and
Commit Queue
committed
[wildcards] Support wildcard import prefixes
From the [spec](https://github.com/dart-lang/language/blob/main/working/wildcards/feature-specification.md#imports), > Import prefixes named _ are non-binding but will provide access to the non-private extensions in that library. Fixes: #56245 Change-Id: Ia38f0fa9f1f2daba8ad93ec844fa0b23ed77d2f8 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/375762 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Phil Quitslund <[email protected]>
1 parent d84619e commit 37f7206

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed

pkg/analyzer/lib/src/dart/element/extensions.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ extension ElementExtension on Element {
122122
bool get isWildcardVariable =>
123123
name == '_' &&
124124
(this is LocalVariableElement ||
125+
this is PrefixElement ||
125126
this is TypeParameterElement ||
126127
(this is ParameterElement &&
127128
this is! FieldFormalParameterElement &&

pkg/analyzer/lib/src/dart/element/scope.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,11 @@ class LibraryOrAugmentationScope extends EnclosedScope {
128128
) : super(importScope) {
129129
extensions.addAll(importScope.extensions);
130130

131-
_container.prefixes.forEach(_addGetter);
131+
for (var prefix in _container.prefixes) {
132+
if (!prefix.isWildcardVariable) {
133+
_addGetter(prefix);
134+
}
135+
}
132136
_container.library.units.forEach(_addUnitElements);
133137

134138
// Add implicit 'dart:core' declarations.

pkg/analyzer/test/src/dart/resolution/library_import_prefix_test.dart

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,75 @@ main() {
140140
assertElement(pRef, findElement.prefix('p'));
141141
assertTypeNull(pRef);
142142
}
143+
144+
test_wildcardResolution() async {
145+
newFile('$testPackageLibPath/a.dart', r'''
146+
extension ExtendedString on String {
147+
bool get stringExt => true;
148+
}
149+
150+
var a = 0;
151+
''');
152+
153+
newFile('$testPackageLibPath/b.dart', r'''
154+
extension ExtendedString2 on String {
155+
bool get stringExt2 => true;
156+
}
157+
''');
158+
159+
// Import prefixes named `_` provide access to non-private extensions
160+
// in the imported library but are non-binding.
161+
await assertErrorsInCode(r'''
162+
import 'a.dart' as _;
163+
import 'b.dart' as _;
164+
165+
f() {
166+
''.stringExt;
167+
''.stringExt2;
168+
_.a;
169+
}
170+
''', [
171+
// String extensions are found but `_` is not bound.
172+
error(CompileTimeErrorCode.UNDEFINED_IDENTIFIER, 86, 1),
173+
]);
174+
}
175+
176+
test_wildcardResolution_preWildcards() async {
177+
newFile('$testPackageLibPath/a.dart', r'''
178+
extension ExtendedString on String {
179+
bool get stringExt => true;
180+
}
181+
182+
var a = 0;
183+
''');
184+
185+
newFile('$testPackageLibPath/b.dart', r'''
186+
extension ExtendedString on String {
187+
bool get stringExt2 => true;
188+
}
189+
''');
190+
191+
await assertNoErrorsInCode(r'''
192+
// @dart = 3.4
193+
// (pre wildcard-variables)
194+
195+
import 'a.dart' as _;
196+
import 'b.dart' as _;
197+
198+
f() {
199+
''.stringExt;
200+
''.stringExt2;
201+
_.a;
202+
}
203+
''');
204+
205+
// `_` is bound so `a` resolves to the int declared in `a.dart`.
206+
var node = findNode.simple('a;');
207+
assertResolvedNodeText(node, r'''
208+
SimpleIdentifier
209+
token: a
210+
staticElement: package:test/a.dart::@getter::a
211+
staticType: int
212+
''');
213+
}
143214
}

0 commit comments

Comments
 (0)