Skip to content

Commit feff6aa

Browse files
authored
Add more pathToX functions. (#298)
1 parent 7232910 commit feff6aa

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
* Added `LibraryReader.pathToUrl(Uri|String)`, which computes the `import` or
44
`export` path necessary to reach the provided URL from the current library.
5+
Also added `pathToAsset` and `pathToElement` as convenience functions.
56

67
## 0.7.3
78

lib/src/library.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'package:analyzer/dart/ast/standard_resolution_map.dart';
77
import 'package:analyzer/dart/element/element.dart';
88
// ignore: implementation_imports
99
import 'package:analyzer/src/dart/resolver/scope.dart';
10+
import 'package:build/build.dart';
1011
import 'package:path/path.dart' as p;
1112

1213
import 'constants/reader.dart';
@@ -72,6 +73,18 @@ class LibraryReader {
7273
}
7374
}
7475

76+
/// Returns a [Uri] from the current library to the target [asset].
77+
///
78+
/// This is a typed convenience function for using [pathToUrl], and the same
79+
/// API restrictions hold around supported schemes and relative paths.
80+
Uri pathToAsset(AssetId asset) => pathToUrl(asset.uri);
81+
82+
/// Returns a [Uri] from the current library to the target [element].
83+
///
84+
/// This is a typed convenience function for using [pathToUrl], and the same
85+
/// API restrictions hold around supported schemes and relative paths.
86+
Uri pathToElement(Element element) => pathToUrl(element.source.uri);
87+
7588
/// Returns a [Uri] from the current library to the one provided.
7689
///
7790
/// If possible, a `package:` or `dart:` URL scheme will be used to reference
@@ -85,6 +98,9 @@ class LibraryReader {
8598
///
8699
/// May throw [ArgumentError] if it is not possible to resolve a path.
87100
Uri pathToUrl(dynamic toUrlOrString) {
101+
if (toUrlOrString == null) {
102+
throw new ArgumentError.notNull('toUrlOrString');
103+
}
88104
final to = toUrlOrString is Uri
89105
? toUrlOrString
90106
: Uri.parse(toUrlOrString as String);

0 commit comments

Comments
 (0)