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

Side by Side Diff: pkg/front_end/lib/src/fasta/translate_uri.dart

Issue 2977703002: Refactorings for TranslateUri and its tests. (Closed)
Patch Set: Replace `file` with just word 'file' in documentation. Created 3 years, 5 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 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. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library fasta.translate_uri; 5 library fasta.translate_uri;
6 6
7 import 'dart:async' show Future; 7 import 'dart:async' show Future;
8
9 import 'dart:convert' show JSON; 8 import 'dart:convert' show JSON;
10 9
11 import 'package:front_end/file_system.dart' 10 import 'package:front_end/file_system.dart'
12 show FileSystem, FileSystemException; 11 show FileSystem, FileSystemException;
13
14 import 'package:package_config/packages_file.dart' as packages_file show parse; 12 import 'package:package_config/packages_file.dart' as packages_file show parse;
15 13
16 import 'deprecated_problems.dart' show deprecated_inputError; 14 import 'deprecated_problems.dart' show deprecated_inputError;
17 15
16 /// Instances of [TranslateUri] translate absolute `dart` and `package` URIs
17 /// into corresponding file URIs in a [FileSystem]. Translated URIs are
18 /// typically `file:` URIs, but may use a different scheme if the compiler is
19 /// invoked with the `multiRoot` option or in unit tests that use a custom file
20 /// system.
18 class TranslateUri { 21 class TranslateUri {
19 final Map<String, Uri> packages; 22 /// Mapping from Dart library names (e.g. `math`) to file URIs.
20 final Map<String, Uri> dartLibraries; 23 final Map<String, Uri> dartLibraries;
21 24
22 // TODO(ahe): We probably want this to be `Map<String, Uri>`, that is, just 25 // TODO(ahe): We probably want this to be `Map<String, Uri>`, that is, just
23 // one patch library (with parts). 26 // one patch library (with parts).
24 final Map<String, List<Uri>> patches; 27 /// Mapping from Dart library names to the file URIs of patches to apply.
28 final Map<String, List<Uri>> dartPatches;
ahe 2017/07/12 09:16:27 Why is this renamed?
scheglov 2017/07/12 14:34:55 It makes the name consistent with [dartLibraries]
ahe 2017/07/12 15:16:47 But wouldn't "platform" be more accurate here? One
25 29
26 TranslateUri(this.packages, this.dartLibraries, this.patches); 30 /// Mapping from package names (e.g. `angular`) to the file URIs.
31 final Map<String, Uri> packages;
ahe 2017/07/12 09:16:28 Why is this moved?
scheglov 2017/07/12 14:34:55 The order highlights the relative order and stabil
ahe 2017/07/12 15:16:47 I actually put packages first because I felt it wa
27 32
33 TranslateUri(this.dartLibraries, this.dartPatches, this.packages);
ahe 2017/07/12 09:16:27 Why are the arguments rearranged on this construct
scheglov 2017/07/12 14:34:55 This makes order of arguments consistent with the
34
35 /// Returns `true` if [uri] is private to the platform libraries (and thus
36 /// not accessible from user code).
37 bool isPlatformImplementation(Uri uri) {
ahe 2017/07/12 09:16:28 Why is this moved?
scheglov 2017/07/12 14:34:55 It sorts before "translate". And it seems that it
ahe 2017/07/12 15:16:48 We haven't agreed that methods should be sorted by
38 if (uri.scheme != "dart") return false;
39 String path = uri.path;
40 return dartLibraries[path] == null || path.startsWith("_");
41 }
42
43 /// If the given [uri] is a `dart` or `package` URI, return the corresponding
44 /// file URI (possibly `null` if there is no corresponding file URI);
45 /// otherwise (e.g. when the [uri] is already a file URI) return `null`.
46 ///
47 /// This is the URIs only transformation, there is no guarantee that the
48 /// corresponding file exists in the file system.
28 Uri translate(Uri uri) { 49 Uri translate(Uri uri) {
29 if (uri.scheme == "dart") return translateDartUri(uri); 50 if (uri.scheme == "dart") return _translateDartUri(uri);
30 if (uri.scheme == "package") return translatePackageUri(uri); 51 if (uri.scheme == "package") return _translatePackageUri(uri);
31 return null; 52 return null;
32 } 53 }
33 54
34 Uri translateDartUri(Uri uri) { 55 /// Return the file URI that corresponds to the given `dart` URI, or `null`
56 /// if there is no corresponding Dart library registered.
57 Uri _translateDartUri(Uri uri) {
ahe 2017/07/12 09:16:27 Why is this made private?
scheglov 2017/07/12 14:34:55 It is not used outside, and this is leaking implem
ahe 2017/07/12 15:16:47 This API isn't public, so everything is essentiall
35 if (!uri.isScheme('dart')) return null; 58 if (!uri.isScheme('dart')) return null;
36 String path = uri.path; 59 String path = uri.path;
37 60
38 int index = path.indexOf('/'); 61 int index = path.indexOf('/');
39 if (index == -1) return dartLibraries[path]; 62 if (index == -1) return dartLibraries[path];
40 63
41 String libraryName = path.substring(0, index); 64 String libraryName = path.substring(0, index);
42 String relativePath = path.substring(index + 1); 65 String relativePath = path.substring(index + 1);
43 Uri libraryFileUri = dartLibraries[libraryName]; 66 Uri libraryFileUri = dartLibraries[libraryName];
44 return libraryFileUri?.resolve(relativePath); 67 return libraryFileUri?.resolve(relativePath);
45 } 68 }
46 69
47 Uri translatePackageUri(Uri uri) { 70 /// Return the file URI that corresponds to the given `package` URI, or
71 /// `null` if the `package` [uri] format is invalid, or there is no
72 /// corresponding package registered.
73 Uri _translatePackageUri(Uri uri) {
ahe 2017/07/12 09:16:28 Why is this made private?
scheglov 2017/07/12 14:34:55 It is not used outside, and this is leaking implem
48 int index = uri.path.indexOf("/"); 74 int index = uri.path.indexOf("/");
49 if (index == -1) return null; 75 if (index == -1) return null;
50 String name = uri.path.substring(0, index); 76 String name = uri.path.substring(0, index);
51 String path = uri.path.substring(index + 1); 77 String path = uri.path.substring(index + 1);
52 Uri root = packages[name]; 78 Uri root = packages[name];
53 if (root == null) return null; 79 if (root == null) return null;
54 return root.resolve(path); 80 return root.resolve(path);
55 } 81 }
56 82
57 /// Returns true if [uri] is private to the platform libraries (and thus not
58 /// accessible from user code).
59 bool isPlatformImplementation(Uri uri) {
60 if (uri.scheme != "dart") return false;
61 String path = uri.path;
62 return dartLibraries[path] == null || path.startsWith("_");
63 }
64
65 static Future<TranslateUri> parse(FileSystem fileSystem, Uri sdk, 83 static Future<TranslateUri> parse(FileSystem fileSystem, Uri sdk,
66 {Uri packages}) async { 84 {Uri packages}) async {
67 Uri librariesJson = sdk?.resolve("lib/libraries.json"); 85 Uri librariesJson = sdk?.resolve("lib/libraries.json");
68 86
69 // TODO(ahe): Provide a value for this file. 87 // TODO(ahe): Provide a value for this file.
70 Uri patches = null; 88 Uri patches = null;
71 89
72 packages ??= Uri.base.resolve(".packages"); 90 packages ??= Uri.base.resolve(".packages");
73 91
74 List<int> bytes; 92 List<int> bytes;
75 try { 93 try {
76 bytes = await fileSystem.entityForUri(packages).readAsBytes(); 94 bytes = await fileSystem.entityForUri(packages).readAsBytes();
77 } on FileSystemException catch (e) { 95 } on FileSystemException catch (e) {
78 deprecated_inputError(packages, -1, e.message); 96 deprecated_inputError(packages, -1, e.message);
79 } 97 }
80 98
81 Map<String, Uri> parsedPackages; 99 Map<String, Uri> parsedPackages;
82 try { 100 try {
83 parsedPackages = packages_file.parse(bytes, packages); 101 parsedPackages = packages_file.parse(bytes, packages);
84 } on FormatException catch (e) { 102 } on FormatException catch (e) {
85 return deprecated_inputError(packages, e.offset, e.message); 103 return deprecated_inputError(packages, e.offset, e.message);
86 } 104 }
87 return new TranslateUri( 105
88 parsedPackages, 106 var dartLibraries = await computeDartLibraries(fileSystem, librariesJson);
89 await computeLibraries(fileSystem, librariesJson), 107 return new TranslateUri(dartLibraries,
90 await computePatches(fileSystem, patches)); 108 await computeDartPatches(fileSystem, patches), parsedPackages);
91 } 109 }
92 } 110 }
93 111
94 Future<Map<String, Uri>> computeLibraries( 112 /// Read the JSON file with defined SDK libraries from the given [uri] in the
113 /// [fileSystem] and return the mapping from parsed Dart library names (e.g.
114 /// `math`) to file URIs.
115 Future<Map<String, Uri>> computeDartLibraries(
ahe 2017/07/12 09:16:27 Why is this renamed?
scheglov 2017/07/12 14:34:55 This highlights the fact that we parse definitions
ahe 2017/07/12 15:16:47 We parse definitions of platform libraries, and th
95 FileSystem fileSystem, Uri uri) async { 116 FileSystem fileSystem, Uri uri) async {
96 if (uri == null) return const <String, Uri>{}; 117 if (uri == null) return const <String, Uri>{};
97 Map<String, String> libraries = JSON 118 Map<String, String> libraries = JSON
98 .decode(await fileSystem.entityForUri(uri).readAsString())["libraries"]; 119 .decode(await fileSystem.entityForUri(uri).readAsString())["libraries"];
99 Map<String, Uri> result = <String, Uri>{}; 120 Map<String, Uri> result = <String, Uri>{};
100 libraries.forEach((String name, String path) { 121 libraries.forEach((String name, String path) {
101 result[name] = uri.resolveUri(new Uri.file(path)); 122 result[name] = uri.resolveUri(new Uri.file(path));
102 }); 123 });
103 return result; 124 return result;
104 } 125 }
105 126
106 Future<Map<String, List<Uri>>> computePatches( 127 Future<Map<String, List<Uri>>> computeDartPatches(
ahe 2017/07/12 09:16:27 Why is this renamed?
scheglov 2017/07/12 14:34:55 The new name highlights that we parse patches for
107 FileSystem fileSystem, Uri uri) async { 128 FileSystem fileSystem, Uri uri) async {
108 // TODO(ahe): Read patch information. 129 // TODO(ahe): Read patch information.
109 return const <String, List<Uri>>{}; 130 return const <String, List<Uri>>{};
110 } 131 }
OLDNEW
« no previous file with comments | « pkg/front_end/lib/src/fasta/target_implementation.dart ('k') | pkg/front_end/test/fasta/testing/suite.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698