Deprecated restoreAbsolute(), use pathToUri().

Change-Id: Id705e4f0eda7beba4c3d2dd2f6ba4b82691ef1f8
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/220480
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Samuel Rawlins <srawlins@google.com>
This commit is contained in:
Konstantin Shcheglov 2021-11-18 21:17:17 +00:00 committed by commit-bot@chromium.org
parent 6dbbd4b7d7
commit 9d7df725f6
29 changed files with 276 additions and 155 deletions

View file

@ -125,7 +125,7 @@ class ExecutionDomainHandler implements RequestHandler {
if (source.uriKind != UriKind.FILE_URI) {
uri = source.uri.toString();
} else {
uri = sourceFactory.restoreUri(source).toString();
uri = sourceFactory.pathToUri(file).toString();
}
return ExecutionMapUriResult(uri: uri).toResponse(request.id);
} else if (uri != null) {

View file

@ -158,9 +158,7 @@ class MoveFileRefactoringImpl extends RefactoringImpl
var refDir = pathContext.dirname(reference.file);
// Try to keep package: URI
if (_isPackageReference(reference)) {
Source newSource =
NonExistingSource(newFile, pathos.toUri(newFile), UriKind.FILE_URI);
var restoredUri = driver.sourceFactory.restoreUri(newSource);
var restoredUri = driver.sourceFactory.pathToUri(newFile);
// If the new URI is not a package: URI, fall back to computing a relative
// URI below.
if (restoredUri?.isScheme('package') ?? false) {

View file

@ -2,6 +2,8 @@
* Deprecations and renames for `getXyz` methods in `AnalysisDriver`.
* Removed uppercase named constants from `double` in mock SDK.
* Deprecated `path` and `uri` from `AnalysisResult`.
* Deprecated `UriResolver.restoreAbsolute`, use `pathToUri` instead.
* Deprecated `SourceFactory.restoreAbsolute`, use `pathToUri` instead.
## 2.7.0
* Updated `ConstructorElement.displayName` to either `Class` or `Class.constructor`.

View file

@ -101,6 +101,17 @@ class SourceFactoryImpl implements SourceFactory {
return null;
}
@override
Uri? pathToUri(String path) {
for (var resolver in resolvers) {
var uri = resolver.pathToUri(path);
if (uri != null) {
return uri;
}
}
return null;
}
@override
Source? resolveUri(Source? containingSource, String? containedUri) {
if (containedUri == null) {
@ -128,21 +139,13 @@ class SourceFactoryImpl implements SourceFactory {
}
}
@Deprecated('Use pathToUri() instead')
@override
Uri? restoreUri(Source source) {
if (source is InSummarySource) {
return source.uri;
}
for (UriResolver resolver in resolvers) {
// First see if a resolver can restore the URI.
Uri? uri = resolver.restoreAbsolute(source);
if (uri != null) {
return uri;
}
}
return null;
return pathToUri(source.fullName);
}
/// Return a source object representing the URI that results from resolving

View file

@ -832,8 +832,7 @@ class FileSystemState {
var file = _pathToFile[path];
if (file == null) {
File resource = _resourceProvider.getFile(path);
Source fileSource = resource.createSource();
Uri uri = _sourceFactory.restoreUri(fileSource)!;
Uri uri = _sourceFactory.pathToUri(path)!;
file = _newFile(resource, path, uri);
}
return file;
@ -897,10 +896,8 @@ class FileSystemState {
bool hasUri(String path) {
bool? flag = _hasUriForPath[path];
if (flag == null) {
File resource = _resourceProvider.getFile(path);
Source fileSource = resource.createSource();
Uri? uri = _sourceFactory.restoreUri(fileSource);
Source? uriSource = _sourceFactory.forUri2(uri!);
Uri uri = _sourceFactory.pathToUri(path)!;
Source? uriSource = _sourceFactory.forUri2(uri);
flag = uriSource?.fullName == path;
_hasUriForPath[path] = flag;
}

View file

@ -5,7 +5,6 @@
import 'package:analyzer/dart/analysis/uri_converter.dart';
import 'package:analyzer/file_system/file_system.dart';
import 'package:analyzer/src/dart/analysis/driver.dart';
import 'package:analyzer/src/generated/source.dart';
import 'package:path/src/context.dart';
/// An implementation of a URI converter based on an analysis driver.
@ -32,8 +31,7 @@ class DriverBasedUriConverter implements UriConverter {
}
}
}
Source source = provider.getFile(path).createSource();
return driver.sourceFactory.restoreUri(source);
return driver.sourceFactory.pathToUri(path);
}
@override

View file

@ -208,9 +208,7 @@ class _UriConverterImpl implements UriConverter {
@override
Uri? pathToUri(String path, {String? containingPath}) {
var fileUri = resourceProvider.pathContext.toUri(path);
var fileSource = sourceFactory.forUri2(fileUri)!;
return sourceFactory.restoreUri(fileSource);
return sourceFactory.pathToUri(path);
}
@override

View file

@ -335,10 +335,7 @@ class FileSystemState {
return file;
}
var fileUri = _resourceProvider.pathContext.toUri(path);
var uri = _sourceFactory.restoreUri(
_FakeSource(path, fileUri),
);
var uri = _sourceFactory.pathToUri(path);
if (uri == null) {
throw StateError('Unable to convert path to URI: $path');
}
@ -589,19 +586,6 @@ class _ContentWithDigest {
});
}
class _FakeSource implements Source {
@override
final String fullName;
@override
final Uri uri;
_FakeSource(this.fullName, this.uri);
@override
dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
}
class _FileStateFiles {
final List<FileState> imported = [];
final List<FileState> exported = [];

View file

@ -128,6 +128,23 @@ abstract class AbstractDartSdk implements DartSdk {
return source;
}
@override
Uri? pathToUri(String path) {
var file = resourceProvider.getFile(path);
var uriStr = _getPath(file);
if (uriStr == null) {
return null;
}
try {
return Uri.parse(uriStr);
} on FormatException {
return null;
}
}
/// TODO(scheglov) This name is misleading, returns `dart:foo/bar.dart`.
String? _getPath(File file) {
List<SdkLibrary> libraries = libraryMap.sdkLibraries;
int length = libraries.length;

View file

@ -17,6 +17,11 @@ class ResourceUriResolver extends UriResolver {
ResourceProvider get provider => _provider;
@override
Uri pathToUri(String path) {
return _provider.pathContext.toUri(path);
}
@override
Source? resolveAbsolute(Uri uri) {
if (!isFileUri(uri)) {
@ -27,9 +32,9 @@ class ResourceUriResolver extends UriResolver {
return file.createSource(uri);
}
@Deprecated('Use pathToUri() instead')
@override
Uri restoreAbsolute(Source source) =>
_provider.pathContext.toUri(source.fullName);
Uri restoreAbsolute(Source source) => pathToUri(source.fullName);
/// Return `true` if the given [uri] is a `file` URI.
static bool isFileUri(Uri uri) => uri.scheme == FILE_SCHEME;

View file

@ -59,6 +59,10 @@ abstract class DartSdk {
/// Return the source representing the library with the given 'dart:' [uri],
/// or `null` if the given URI does not denote a library in this SDK.
Source? mapDartUri(String uri);
/// Return the `dart` URI representing the given [path] if the file is in
/// this SDK, or `null` if the file is not in this SDK.
Uri? pathToUri(String path);
}
/// Manages the DartSdk's that have been created. Clients need to create

View file

@ -60,6 +60,11 @@ class DartUriResolver extends UriResolver {
/// @return the [DartSdk] against which URIs are to be resolved.
DartSdk get dartSdk => _sdk;
@override
Uri? pathToUri(String path) {
return _sdk.pathToUri(path);
}
@override
Source? resolveAbsolute(Uri uri) {
if (!isDartUri(uri)) {
@ -68,12 +73,6 @@ class DartUriResolver extends UriResolver {
return _sdk.mapDartUri(uri.toString());
}
@override
Uri? restoreAbsolute(Source source) {
var dartSource = _sdk.fromFileUri(source.uri);
return dartSource?.uri;
}
/// Return `true` if the given URI is a `dart:` URI.
///
/// @param uri the URI being tested
@ -285,6 +284,13 @@ abstract class SourceFactory {
/// @return a source object representing the absolute URI
Source? forUri2(Uri absoluteUri);
/// Return the URI that should be used to reference the file at the absolute
/// [path], or `null` if there is no valid way to reference the file.
/// The file at that path is not required to exist.
///
/// Throws an [ArgumentError] if the [path] is not a valid path.
Uri? pathToUri(String path);
/// Return a source representing the URI that results from resolving the given
/// (possibly relative) [containedUri] against the URI associated with the
/// [containingSource], whether or not the resulting source exists, or `null`
@ -297,6 +303,7 @@ abstract class SourceFactory {
///
/// @param source the source to get URI for
/// @return the absolute URI representing the given source
@Deprecated('Use pathToUri() instead')
Uri? restoreUri(Source source);
}
@ -410,6 +417,14 @@ class UriKind implements Comparable<UriKind> {
/// used to resolve URI's for a source factory. Subclasses of this class are
/// expected to resolve a single scheme of absolute URI.
abstract class UriResolver {
/// Return the absolute URI that should be used to reference the file at the
/// absolute [path], or `null` if this resolver cannot reference this file.
/// The file at that path is not required to exist.
///
/// Throws an [ArgumentError] if the [path] is not a valid path.
/// ignore: deprecated_member_use_from_same_package
Uri? pathToUri(String path) => restoreAbsolute(_FakeSource(path));
/// Resolve the given absolute [uri]. Return a [Source] representing the file
/// to which it was resolved, whether or not the resulting source exists, or
/// `null` if it could not be resolved because the URI is invalid.
@ -419,5 +434,21 @@ abstract class UriResolver {
/// valid URI cannot be computed.
///
/// The computation should be based solely on [source.fullName].
Uri? restoreAbsolute(Source source) => null;
@Deprecated('Use pathToUri() instead')
Uri? restoreAbsolute(Source source) {
return pathToUri(source.fullName);
}
}
class _FakeSource implements Source {
@override
final String fullName;
_FakeSource(this.fullName);
@override
Uri get uri => pathos.toUri(fullName);
@override
dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
}

View file

@ -36,6 +36,22 @@ class PackageMapUriResolver extends UriResolver {
});
}
@override
Uri? pathToUri(String path) {
pathos.Context pathContext = resourceProvider.pathContext;
for (String pkgName in packageMap.keys) {
Folder pkgFolder = packageMap[pkgName]![0];
String pkgFolderPath = pkgFolder.path;
if (path.startsWith(pkgFolderPath + pathContext.separator)) {
String relPath = path.substring(pkgFolderPath.length + 1);
List<String> relPathComponents = pathContext.split(relPath);
String relUriPath = pathos.posix.joinAll(relPathComponents);
return Uri.parse('$PACKAGE_SCHEME:$pkgName/$relUriPath');
}
}
return null;
}
@override
Source? resolveAbsolute(Uri uri) {
if (!isPackageUri(uri)) {
@ -61,23 +77,6 @@ class PackageMapUriResolver extends UriResolver {
return null;
}
@override
Uri? restoreAbsolute(Source source) {
String sourcePath = source.fullName;
pathos.Context pathContext = resourceProvider.pathContext;
for (String pkgName in packageMap.keys) {
Folder pkgFolder = packageMap[pkgName]![0];
String pkgFolderPath = pkgFolder.path;
if (sourcePath.startsWith(pkgFolderPath + pathContext.separator)) {
String relPath = sourcePath.substring(pkgFolderPath.length + 1);
List<String> relPathComponents = pathContext.split(relPath);
String relUriPath = pathos.posix.joinAll(relPathComponents);
return Uri.parse('$PACKAGE_SCHEME:$pkgName/$relUriPath');
}
}
return null;
}
/// Returns `true` if [uri] is a `package` URI.
static bool isPackageUri(Uri uri) {
return uri.scheme == PACKAGE_SCHEME;

View file

@ -94,6 +94,9 @@ class InSummaryUriResolver extends UriResolver {
InSummaryUriResolver(this.resourceProvider, this._dataStore);
@override
Uri? pathToUri(String path) => null;
@override
Source? resolveAbsolute(Uri uri) {
String uriString = uri.toString();

View file

@ -90,4 +90,10 @@ class SummaryBasedDartSdk implements DartSdk {
Uri uri = Uri.parse(uriStr);
return _uriResolver.resolveAbsolute(uri);
}
@override
Uri? pathToUri(String path) {
// Libraries from summaries don't have corresponding Dart files.
return null;
}
}

View file

@ -3,6 +3,7 @@
// BSD-style license that can be found in the LICENSE file.
import 'package:analyzer/src/generated/source.dart';
import 'package:analyzer/src/summary/package_bundle_reader.dart';
import 'package:path/path.dart';
String fileUriToNormalizedPath(Context context, Uri fileUri) {
@ -34,5 +35,9 @@ Uri? rewriteFileToPackageUri(SourceFactory sourceFactory, Uri absoluteUri) {
return null;
}
return sourceFactory.restoreUri(source);
if (source is InSummarySource) {
return source.uri;
}
return sourceFactory.pathToUri(source.fullName);
}

View file

@ -52,6 +52,24 @@ class BazelPackageUriResolver extends UriResolver {
: _workspace = workspace,
_context = workspace.provider.pathContext;
@override
Uri? pathToUri(String path) {
// Search in each root.
for (var root in [
..._workspace.binPaths,
_workspace.genfiles,
_workspace.readonly,
_workspace.root
]) {
var uriParts = _restoreUriParts(root, path);
if (uriParts != null) {
return Uri.parse('package:${uriParts[0]}/${uriParts[1]}');
}
}
return null;
}
@override
Source? resolveAbsolute(Uri uri) {
var source = _sourceCache[uri];
@ -64,26 +82,6 @@ class BazelPackageUriResolver extends UriResolver {
return source;
}
@override
Uri? restoreAbsolute(Source source) {
String filePath = source.fullName;
// Search in each root.
for (var root in [
..._workspace.binPaths,
_workspace.genfiles,
_workspace.readonly,
_workspace.root
]) {
var uriParts = _restoreUriParts(root, filePath);
if (uriParts != null) {
return Uri.parse('package:${uriParts[0]}/${uriParts[1]}');
}
}
return null;
}
Source? _resolveAbsolute(Uri uri) {
if (uri.scheme == 'file') {
var path = fileUriToNormalizedPath(_context, uri);

View file

@ -59,6 +59,18 @@ class PackageBuildPackageUriResolver extends UriResolver {
Map<String, List<Folder>> get packageMap => _workspace.packageMap;
@override
Uri? pathToUri(String path) {
if (_context.isWithin(_workspace.root, path)) {
var uriParts = _restoreUriParts(path);
if (uriParts != null) {
return Uri.parse('package:${uriParts[0]}/${uriParts[1]}');
}
}
return _normalUriResolver.pathToUri(path);
}
@override
Source? resolveAbsolute(Uri uri) {
if (uri.scheme != 'package') {
@ -90,20 +102,6 @@ class PackageBuildPackageUriResolver extends UriResolver {
return basicResolverSource;
}
@override
Uri? restoreAbsolute(Source source) {
String filePath = source.fullName;
if (_context.isWithin(_workspace.root, filePath)) {
var uriParts = _restoreUriParts(filePath);
if (uriParts != null) {
return Uri.parse('package:${uriParts[0]}/${uriParts[1]}');
}
}
return _normalUriResolver.restoreAbsolute(source);
}
List<String>? _restoreUriParts(String filePath) {
String relative = _context.relative(filePath, from: _workspace.root);
List<String> components = _context.split(relative);

View file

@ -29,6 +29,12 @@ class ResourceUriResolverTest with ResourceProviderMixin {
expect(resolver, isNotNull);
}
void test_pathToUri() {
var path = convertPath('/test.dart');
var uri = toUri(path);
expect(resolver.pathToUri(path), uri);
}
void test_resolveAbsolute_file() {
var uri = toUri('/test.dart');
@ -59,6 +65,7 @@ class ResourceUriResolverTest with ResourceProviderMixin {
expect(source, isNull);
}
@Deprecated('Use pathToUri() instead')
void test_restoreAbsolute() {
var uri = toUri('/test.dart');

View file

@ -44,6 +44,18 @@ class DartUriResolverTest extends _SimpleDartSdkTest {
expect(DartUriResolver.isDartUri(uri), isFalse);
}
void test_pathToUri_library() {
var path = convertPath('/sdk/lib/core/core.dart');
var dartUri = resolver.pathToUri(path);
expect(dartUri.toString(), 'dart:core');
}
void test_pathToUri_part() {
var path = convertPath('/sdk/lib/core/int.dart');
var dartUri = resolver.pathToUri(path);
expect(dartUri.toString(), 'dart:core/int.dart');
}
void test_resolve_dart_library() {
var source = resolver.resolveAbsolute(Uri.parse('dart:core'));
expect(source, isNotNull);
@ -64,16 +76,18 @@ class DartUriResolverTest extends _SimpleDartSdkTest {
expect(result, isNull);
}
@Deprecated('Use pathToUri() instead')
void test_restoreAbsolute_library() {
_SourceMock source = _SourceMock();
source.uri = toUri('/sdk/lib/core/core.dart');
source.fullName = convertPath('/sdk/lib/core/core.dart');
var dartUri = resolver.restoreAbsolute(source);
expect(dartUri.toString(), 'dart:core');
}
@Deprecated('Use pathToUri() instead')
void test_restoreAbsolute_part() {
_SourceMock source = _SourceMock();
source.uri = toUri('/sdk/lib/core/int.dart');
source.fullName = convertPath('/sdk/lib/core/int.dart');
var dartUri = resolver.restoreAbsolute(source);
expect(dartUri.toString(), 'dart:core/int.dart');
}
@ -350,6 +364,9 @@ part of dart.core;
}
class _SourceMock implements Source {
@override
late final String fullName;
@override
late final Uri uri;

View file

@ -39,6 +39,16 @@ class SourceFactoryTest with ResourceProviderMixin {
expect(SourceFactory([]), isNotNull);
}
void test_pathToUri() {
File file1 = getFile("/some/file1.dart");
File file2 = getFile("/some/file2.dart");
Uri expected1 = Uri.parse("file:///my_file.dart");
SourceFactory factory =
SourceFactory([UriResolver_restoreUri(file1.path, expected1)]);
expect(factory.pathToUri(file1.path), expected1);
expect(factory.pathToUri(file2.path), isNull);
}
void test_resolveUri_absolute() {
UriResolver_absolute resolver = UriResolver_absolute();
SourceFactory factory = SourceFactory([resolver]);
@ -95,6 +105,7 @@ class SourceFactoryTest with ResourceProviderMixin {
expect(result.uri.toString(), 'package:package/dir/second.dart');
}
@Deprecated('Use pathToUri() instead')
void test_restoreUri() {
File file1 = getFile("/some/file1.dart");
File file2 = getFile("/some/file2.dart");
@ -102,7 +113,7 @@ class SourceFactoryTest with ResourceProviderMixin {
Source source2 = FileSource(file2);
Uri expected1 = Uri.parse("file:///my_file.dart");
SourceFactory factory =
SourceFactory([UriResolver_restoreUri(source1, expected1)]);
SourceFactory([UriResolver_restoreUri(file1.path, expected1)]);
expect(factory.restoreUri(source1), same(expected1));
expect(factory.restoreUri(source2), isNull);
}
@ -121,20 +132,20 @@ class UriResolver_absolute extends UriResolver {
}
class UriResolver_restoreUri extends UriResolver {
Source source1;
String path1;
Uri expected1;
UriResolver_restoreUri(this.source1, this.expected1);
UriResolver_restoreUri(this.path1, this.expected1);
@override
Source? resolveAbsolute(Uri uri) => null;
@override
Uri? restoreAbsolute(Source source) {
if (identical(source, source1)) {
Uri? pathToUri(String path) {
if (path == path1) {
return expected1;
}
return null;
}
@override
Source? resolveAbsolute(Uri uri) => null;
}
class UriResolver_SourceFactoryTest_test_fromEncoding_valid

View file

@ -39,6 +39,33 @@ class _PackageMapUriResolverTest {
expect(PackageMapUriResolver.isPackageUri(uri), isFalse);
}
void test_pathToUri() {
String pkgFileA = provider.convertPath('/pkgA/lib/libA.dart');
String pkgFileB = provider.convertPath('/pkgB/lib/src/libB.dart');
provider.newFile(pkgFileA, 'library lib_a;');
provider.newFile(pkgFileB, 'library lib_b;');
PackageMapUriResolver resolver =
PackageMapUriResolver(provider, <String, List<Folder>>{
'pkgA': <Folder>[provider.getFolder(provider.convertPath('/pkgA/lib'))],
'pkgB': <Folder>[provider.getFolder(provider.convertPath('/pkgB/lib'))]
});
{
var path = provider.convertPath('/pkgA/lib/libA.dart');
var uri = resolver.pathToUri(path);
expect(uri, Uri.parse('package:pkgA/libA.dart'));
}
{
var path = provider.convertPath('/pkgB/lib/src/libB.dart');
var uri = resolver.pathToUri(path);
expect(uri, Uri.parse('package:pkgB/src/libB.dart'));
}
{
var path = provider.convertPath('/no/such/file');
var uri = resolver.pathToUri(path);
expect(uri, isNull);
}
}
void test_resolve_multiple_folders() {
var a = provider.newFile(provider.convertPath('/aaa/a.dart'), '');
var b = provider.newFile(provider.convertPath('/bbb/b.dart'), '');
@ -144,6 +171,7 @@ class _PackageMapUriResolverTest {
expect(result, isNull);
}
@Deprecated('Use pathToUri() instead')
void test_restoreAbsolute() {
String pkgFileA = provider.convertPath('/pkgA/lib/libA.dart');
String pkgFileB = provider.convertPath('/pkgB/lib/src/libB.dart');

View file

@ -153,16 +153,21 @@ class BaseAnalysisDriverTest with ResourceProviderMixin {
void tearDown() {}
}
class _GeneratedUriResolverMock implements UriResolver {
class _GeneratedUriResolverMock extends UriResolver {
Source? Function(Uri)? resolveAbsoluteFunction;
Uri? Function(Source)? restoreAbsoluteFunction;
Uri? Function(String)? pathToUriFunction;
@override
noSuchMethod(Invocation invocation) {
throw StateError('Unexpected invocation of ${invocation.memberName}');
}
@override
Uri? pathToUri(String path) {
return pathToUriFunction?.call(path);
}
@override
Source? resolveAbsolute(Uri uri) {
if (resolveAbsoluteFunction != null) {
@ -170,12 +175,4 @@ class _GeneratedUriResolverMock implements UriResolver {
}
return null;
}
@override
Uri? restoreAbsolute(Source source) {
if (restoreAbsoluteFunction != null) {
return restoreAbsoluteFunction!(source);
}
return null;
}
}

View file

@ -1246,8 +1246,7 @@ bbb() {}
Source generatedSource = _SourceMock(generatedPath, uri);
generatedUriResolver.resolveAbsoluteFunction = (uri) => generatedSource;
generatedUriResolver.restoreAbsoluteFunction = (Source source) {
String path = source.fullName;
generatedUriResolver.pathToUriFunction = (path) {
if (path == templatePath || path == generatedPath) {
return uri;
} else {

View file

@ -446,9 +446,7 @@ class FeatureSetProviderTest with ResourceProviderMixin {
FeatureSet _getPathFeatureSet(String path) {
path = convertPath(path);
var fileUri = toUri(path);
var fileSource = sourceFactory.forUri2(fileUri)!;
var uri = sourceFactory.restoreUri(fileSource)!;
var uri = sourceFactory.pathToUri(path)!;
return provider.getFeatureSet(path, uri);
}

View file

@ -718,10 +718,10 @@ part of 'a.dart';
}
}
class _GeneratedUriResolverMock implements UriResolver {
class _GeneratedUriResolverMock extends UriResolver {
Source? Function(Uri)? resolveAbsoluteFunction;
Uri? Function(Source)? restoreAbsoluteFunction;
Uri? Function(String)? pathToUriFunction;
@override
noSuchMethod(Invocation invocation) {
@ -729,17 +729,14 @@ class _GeneratedUriResolverMock implements UriResolver {
}
@override
Source? resolveAbsolute(Uri uri) {
if (resolveAbsoluteFunction != null) {
return resolveAbsoluteFunction!(uri);
}
return null;
Uri? pathToUri(String path) {
return pathToUriFunction?.call(path);
}
@override
Uri? restoreAbsolute(Source source) {
if (restoreAbsoluteFunction != null) {
return restoreAbsoluteFunction!(source);
Source? resolveAbsolute(Uri uri) {
if (resolveAbsoluteFunction != null) {
return resolveAbsoluteFunction!(uri);
}
return null;
}

View file

@ -72,8 +72,7 @@ abstract class AbstractResynthesizeTest with ResourceProviderMixin {
Source addSource(String path, String contents) {
var file = newFile(path, content: contents);
var fileSource = file.createSource();
var uri = sourceFactory.restoreUri(fileSource)!;
var uri = sourceFactory.pathToUri(file.path)!;
return sourceFactory.forUri2(uri)!;
}

View file

@ -38,6 +38,12 @@ class BazelFileUriResolverTest with ResourceProviderMixin {
expect(workspace.isBazel, isTrue);
}
void test_pathToUri() {
Uri uri = toUri('/workspace/test.dart');
var source = resolver.resolveAbsolute(uri)!;
expect(resolver.pathToUri(source.fullName), uri);
}
void test_resolveAbsolute_doesNotExist() {
var source = _resolvePath('/workspace/foo.dart')!;
expect(source.exists(), isFalse);
@ -79,6 +85,7 @@ class BazelFileUriResolverTest with ResourceProviderMixin {
expect(source, isNull);
}
@Deprecated('Use pathToUri() instead')
void test_restoreAbsolute() {
Uri uri =
resourceProvider.pathContext.toUri(convertPath('/workspace/test.dart'));
@ -561,21 +568,25 @@ class BazelPackageUriResolverTest with ResourceProviderMixin {
{bool exists = true, bool restore = true}) {
Uri uri = Uri.parse(uriStr);
var source = resolver.resolveAbsolute(uri)!;
expect(source.fullName, convertPath(posixPath));
var path = source.fullName;
expect(path, convertPath(posixPath));
expect(source.uri, uri);
expect(source.exists(), exists);
// If enabled, test also "restoreAbsolute".
if (restore) {
var uri = resolver.restoreAbsolute(source);
expect(uri.toString(), uriStr);
expect(resolver.pathToUri(path), uri);
// ignore: deprecated_member_use_from_same_package
expect(resolver.restoreAbsolute(source), uri);
}
}
void _assertRestore(String posixPath, String? expectedUri) {
void _assertRestore(String posixPath, String? expectedUriStr) {
var expectedUri = expectedUriStr != null ? Uri.parse(expectedUriStr) : null;
String path = convertPath(posixPath);
_MockSource source = _MockSource(path);
var uri = resolver.restoreAbsolute(source);
expect(uri?.toString(), expectedUri);
expect(resolver.pathToUri(path), expectedUri);
// ignore: deprecated_member_use_from_same_package
expect(resolver.restoreAbsolute(source), expectedUri);
}
}

View file

@ -20,23 +20,25 @@ main() {
class MockUriResolver implements UriResolver {
Map<Uri, File> uriToFile = {};
Map<String, Uri> pathToUri = {};
Map<String, Uri> pathToUriMap = {};
void add(Uri uri, File file) {
uriToFile[uri] = file;
pathToUri[file.path] = uri;
pathToUriMap[file.path] = uri;
}
@override
noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
@override
Source? resolveAbsolute(Uri uri) {
return uriToFile[uri]?.createSource(uri);
Uri? pathToUri(String path) {
return pathToUriMap[path];
}
@override
Uri? restoreAbsolute(Source source) => pathToUri[source.fullName];
Source? resolveAbsolute(Uri uri) {
return uriToFile[uri]?.createSource(uri);
}
}
@reflectiveTest
@ -61,6 +63,12 @@ class PackageBuildFileUriResolverTest with ResourceProviderMixin {
expect(workspace.isBazel, isFalse);
}
void test_pathToUri() {
var uri = toUri('/workspace/test.dart');
var source = resolver.resolveAbsolute(uri)!;
expect(resolver.pathToUri(source.fullName), uri);
}
void test_resolveAbsolute_doesNotExist() {
var source = _resolvePath('/workspace/foo.dart')!;
expect(source, isNotNull);
@ -98,6 +106,7 @@ class PackageBuildFileUriResolverTest with ResourceProviderMixin {
expect(source, isNull);
}
@Deprecated('Use pathToUri() instead')
void test_restoreAbsolute() {
Uri uri =
resourceProvider.pathContext.toUri(convertPath('/workspace/test.dart'));
@ -207,13 +216,15 @@ class PackageBuildPackageUriResolverTest with ResourceProviderMixin {
Source _assertResolveUri(Uri uri, String posixPath,
{bool exists = true, bool restore = true}) {
var source = resolver.resolveAbsolute(uri)!;
expect(source.fullName, convertPath(posixPath));
var path = source.fullName;
expect(path, convertPath(posixPath));
expect(source.uri, uri);
expect(source.exists(), exists);
// If enabled, test also "restoreAbsolute".
if (restore) {
var restoredUri = resolver.restoreAbsolute(source);
expect(restoredUri.toString(), uri.toString());
expect(resolver.pathToUri(path), uri);
// ignore: deprecated_member_use_from_same_package
expect(resolver.restoreAbsolute(source), uri);
}
return source;
}