Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Unified Diff: pkg/analyzer/lib/source/package_map_resolver.dart

Issue 2812093002: Simplify PackageMapUriResolver - one folder, return any file, or null. (Closed)
Patch Set: Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: pkg/analyzer/lib/source/package_map_resolver.dart
diff --git a/pkg/analyzer/lib/source/package_map_resolver.dart b/pkg/analyzer/lib/source/package_map_resolver.dart
index 056016fc32418d7c8833d6edd6b32a196c129d80..3eeda219d58253a953ccbdb87cf0f7c93d2ed955 100644
--- a/pkg/analyzer/lib/source/package_map_resolver.dart
+++ b/pkg/analyzer/lib/source/package_map_resolver.dart
@@ -41,6 +41,13 @@ class PackageMapUriResolver extends UriResolver {
PackageMapUriResolver(this.resourceProvider, this.packageMap) {
asserts.notNull(resourceProvider);
asserts.notNull(packageMap);
+ packageMap.forEach((name, folders) {
+ if (folders.length != 1) {
+ throw new ArgumentError(
+ 'Exactly one folder must be specified for a package.'
+ 'Found $name = $folders');
+ }
+ });
}
@override
@@ -58,74 +65,31 @@ class PackageMapUriResolver extends UriResolver {
// <pkgName>/<relPath>
String pkgName = path.substring(0, index);
String relPath = path.substring(index + 1);
- // Try to find an existing file.
+ // If the package is known, return the corresponding file.
List<Folder> packageDirs = packageMap[pkgName];
if (packageDirs != null) {
- for (Folder packageDir in packageDirs) {
- if (packageDir.exists) {
- Resource result = packageDir.getChild(relPath);
- if (result is File && result.exists) {
- return result.createSource(uri);
- }
- }
- }
+ Folder packageDir = packageDirs.single;
+ File file = packageDir.getChildAssumingFile(relPath);
+ return file.createSource(uri);
}
- // Return a NonExistingSource instance.
- // This helps provide more meaningful error messages to users
- // (a missing file error, as opposed to an invalid URI error).
- String fullPath = packageDirs != null && packageDirs.isNotEmpty
- ? packageDirs.first.canonicalizePath(relPath)
- : relPath;
- return new NonExistingSource(fullPath, uri, UriKind.PACKAGE_URI);
+ return null;
}
@override
Uri restoreAbsolute(Source source) {
String sourcePath = source.fullName;
- Uri bestMatch;
- int bestMatchLength = -1;
pathos.Context pathContext = resourceProvider.pathContext;
for (String pkgName in packageMap.keys) {
- List<Folder> pkgFolders = packageMap[pkgName];
- for (int i = 0; i < pkgFolders.length; i++) {
- Folder pkgFolder = pkgFolders[i];
- String pkgFolderPath = pkgFolder.path;
- if (pkgFolderPath.length > bestMatchLength &&
- sourcePath.startsWith(pkgFolderPath + pathContext.separator)) {
- String relPath = sourcePath.substring(pkgFolderPath.length + 1);
- if (_isReversibleTranslation(pkgFolders, i, relPath)) {
- List<String> relPathComponents = pathContext.split(relPath);
- String relUriPath = pathos.posix.joinAll(relPathComponents);
- bestMatch = Uri.parse('$PACKAGE_SCHEME:$pkgName/$relUriPath');
- bestMatchLength = pkgFolderPath.length;
- }
- }
- }
- }
- return bestMatch;
- }
-
- /**
- * A translation from file path to package URI has just been found for
- * using the [packageDirIndex]th element of [packageDirs], and appending the
- * relative path [relPath]. Determine whether the translation is reversible;
- * that is, whether translating the package URI pack to a file path will
- * produce the file path we started with.
- */
- bool _isReversibleTranslation(
- List<Folder> packageDirs, int packageDirIndex, String relPath) {
- // The translation is reversible provided there is no prior element of
- // [packageDirs] containing a file matching [relPath].
- for (int i = 0; i < packageDirIndex; i++) {
- Folder packageDir = packageDirs[i];
- if (packageDir.exists) {
- Resource result = packageDir.getChild(relPath);
- if (result is File && result.exists) {
- return false;
- }
+ 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 true;
+ return null;
}
/**
« no previous file with comments | « pkg/analysis_server/test/context_manager_test.dart ('k') | pkg/analyzer/test/source/package_map_resolver_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698