Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit bd008dd

Browse files
srawlinscommit-bot@chromium.org
authored andcommitted
Allow empty URI strings in library directives
Fixes #38859 Change-Id: I1698b09bfee367634782f6f7c61f1dbda22edc0a Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/127600 Commit-Queue: Samuel Rawlins <srawlins@google.com> Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
1 parent 18b2d24 commit bd008dd

File tree

11 files changed

+77
-45
lines changed

11 files changed

+77
-45
lines changed

pkg/analyzer/lib/src/context/source.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,12 @@ class SourceFactoryImpl implements SourceFactory {
134134

135135
@override
136136
Source resolveUri(Source containingSource, String containedUri) {
137-
if (containedUri == null || containedUri.isEmpty) {
137+
if (containedUri == null) {
138138
return null;
139139
}
140+
if (containedUri.isEmpty) {
141+
return containingSource;
142+
}
140143
try {
141144
// Force the creation of an escaped URI to deal with spaces, etc.
142145
return _internalResolveUri(containingSource, Uri.parse(containedUri));

pkg/analyzer/lib/src/dart/analysis/library_analyzer.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,8 @@ class LibraryAnalyzer {
521521
return file.exists;
522522
}
523523
}
524-
return false;
524+
// A library can refer to itself with an empty URI.
525+
return source == _library.source;
525526
}
526527

527528
/**

pkg/analyzer/lib/src/dart/ast/ast.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10192,6 +10192,9 @@ abstract class UriBasedDirectiveImpl extends DirectiveImpl
1019210192
if (uriContent == null) {
1019310193
return UriValidationCode.INVALID_URI;
1019410194
}
10195+
if (uriContent.isEmpty) {
10196+
return null;
10197+
}
1019510198
if (isImport && uriContent.startsWith(_DART_EXT_SCHEME)) {
1019610199
return UriValidationCode.URI_WITH_DART_EXT_SCHEME;
1019710200
}

pkg/analyzer/lib/src/summary2/library_builder.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ class LibraryBuilder {
296296
directive.configurations,
297297
directive.uri.stringValue,
298298
);
299-
if (relativeUriStr == null || relativeUriStr.isEmpty) {
299+
if (relativeUriStr == null) {
300300
return null;
301301
}
302302
var relativeUri = Uri.parse(relativeUriStr);

pkg/analyzer/lib/src/summary2/linked_unit_context.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ class LinkedUnitContext {
137137
/// Return the [LibraryElement] referenced in the [node].
138138
LibraryElement directiveLibrary(UriBasedDirective node) {
139139
var uriStr = LazyDirective.getSelectedUri(node);
140-
if (uriStr == null || uriStr.isEmpty) return null;
140+
if (uriStr == null) return null;
141141
return bundleContext.elementFactory.libraryOfUri(uriStr);
142142
}
143143

pkg/analyzer/test/generated/compile_time_error_code.dart

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2354,31 +2354,6 @@ class A<E> {
23542354
]);
23552355
}
23562356

2357-
test_invalidUri_export() async {
2358-
await assertErrorsInCode('''
2359-
export 'ht:';
2360-
''', [
2361-
error(CompileTimeErrorCode.INVALID_URI, 7, 5),
2362-
]);
2363-
}
2364-
2365-
test_invalidUri_import() async {
2366-
await assertErrorsInCode('''
2367-
import 'ht:';
2368-
''', [
2369-
error(CompileTimeErrorCode.INVALID_URI, 7, 5),
2370-
]);
2371-
}
2372-
2373-
test_invalidUri_part() async {
2374-
await assertErrorsInCode(r'''
2375-
library lib;
2376-
part 'ht:';
2377-
''', [
2378-
error(CompileTimeErrorCode.INVALID_URI, 18, 5),
2379-
]);
2380-
}
2381-
23822357
test_isInConstInstanceCreation_restored() async {
23832358
// If ErrorVerifier._isInConstInstanceCreation is not properly restored on
23842359
// exit from visitInstanceCreationExpression, the error at (1) will be

pkg/analyzer/test/src/diagnostics/deferred_import_of_extension_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ void f() {
7474

7575
test_invalidUri() {
7676
assertErrorsInCode('''
77-
import '' deferred as foo;
77+
import 'ht:' deferred as foo;
7878
''', [
79-
error(CompileTimeErrorCode.INVALID_URI, 7, 2),
79+
error(CompileTimeErrorCode.INVALID_URI, 7, 5),
8080
]);
8181
}
8282
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:analyzer/src/error/codes.dart';
6+
import 'package:test_reflective_loader/test_reflective_loader.dart';
7+
8+
import '../dart/resolution/driver_resolution.dart';
9+
10+
main() {
11+
defineReflectiveSuite(() {
12+
defineReflectiveTests(InvalidUriTest);
13+
});
14+
}
15+
16+
@reflectiveTest
17+
class InvalidUriTest extends DriverResolutionTest {
18+
test_emptyUri() async {
19+
await assertNoErrorsInCode('''
20+
import '' as top;
21+
int x;
22+
class C {
23+
int x;
24+
int get y => top.x; // ref
25+
}
26+
''');
27+
assertElement(findNode.simple('x; // ref'), findElement.topGet('x'));
28+
}
29+
30+
test_invalidScheme_export() async {
31+
await assertErrorsInCode('''
32+
export 'ht:';
33+
''', [
34+
error(CompileTimeErrorCode.INVALID_URI, 7, 5),
35+
]);
36+
}
37+
38+
test_invalidScheme_import() async {
39+
await assertErrorsInCode('''
40+
import 'ht:';
41+
''', [
42+
error(CompileTimeErrorCode.INVALID_URI, 7, 5),
43+
]);
44+
}
45+
46+
test_invalidScheme_part() async {
47+
await assertErrorsInCode(r'''
48+
part 'ht:';
49+
''', [
50+
error(CompileTimeErrorCode.INVALID_URI, 5, 5),
51+
]);
52+
}
53+
}

pkg/analyzer/test/src/diagnostics/test_all.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ import 'invalid_required_optional_positional_param_test.dart'
160160
import 'invalid_required_positional_param_test.dart'
161161
as invalid_required_positional_param;
162162
import 'invalid_sealed_annotation_test.dart' as invalid_sealed_annotation;
163+
import 'invalid_uri_test.dart' as invalid_uri;
163164
import 'invalid_use_of_covariant_in_extension_test.dart'
164165
as invalid_use_of_covariant_in_extension;
165166
import 'invalid_use_of_never_value_test.dart' as invalid_use_of_never_value;
@@ -504,6 +505,7 @@ main() {
504505
invalid_required_optional_positional_param.main();
505506
invalid_required_positional_param.main();
506507
invalid_sealed_annotation.main();
508+
invalid_uri.main();
507509
invalid_use_of_covariant_in_extension.main();
508510
invalid_use_of_never_value.main();
509511
invalid_use_of_null_value.main();

pkg/analyzer/test/src/summary/resynthesize_common.dart

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6801,7 +6801,7 @@ Future<dynamic> f;
68016801
allowMissingFiles = true;
68026802
var library = await checkLibrary('''
68036803
@foo
6804-
import '';
6804+
import 'ht:';
68056805
''');
68066806
checkElementText(library, r'''
68076807
@
@@ -7878,19 +7878,18 @@ class C {
78787878
''');
78797879
}
78807880

7881-
test_invalidUri_part_emptyUri() async {
7881+
test_part_emptyUri() async {
78827882
allowMissingFiles = true;
78837883
var library = await checkLibrary(r'''
78847884
part '';
78857885
class B extends A {}
78867886
''');
78877887
checkElementText(library, r'''
7888-
part '<unresolved>';
7888+
part 'test.dart';
7889+
class B {
7890+
}
78897891
class B {
78907892
}
7891-
--------------------
7892-
unit: null
7893-
78947893
''');
78957894
}
78967895

0 commit comments

Comments
 (0)