Update resource package, fix analysis hints/lints (#43) * Update resource package, fix analysis hints/lints * format files * format file * Add new line at end of file
diff --git a/analysis_options.yaml b/analysis_options.yaml new file mode 100644 index 0000000..108d105 --- /dev/null +++ b/analysis_options.yaml
@@ -0,0 +1 @@ +include: package:pedantic/analysis_options.yaml
diff --git a/lib/resource.dart b/lib/resource.dart index 66bc06c..667b03a 100644 --- a/lib/resource.dart +++ b/lib/resource.dart
@@ -2,7 +2,7 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. -/// A [Resource] is data that can be read into a Dart program. +/// A `Resource` is data that can be read into a Dart program. /// /// A resource is identified by a URI. It can be loaded as bytes or data. /// The resource URI may be a `package:` URI.
diff --git a/lib/src/io_html.dart b/lib/src/io_html.dart index 1d1a2dc..3e555a7 100644 --- a/lib/src/io_html.dart +++ b/lib/src/io_html.dart
@@ -12,7 +12,7 @@ // TODO(lrn): Should file be run through XmlHTTPRequest too? if (uri.scheme == "http" || uri.scheme == "https") { // TODO: Stream in chunks if DOM has a way to do so. - List<int> response = await _httpGetBytes(uri); + var response = await _httpGetBytes(uri); yield response; return; } @@ -20,7 +20,7 @@ yield uri.data.contentAsBytes(); return; } - throw new UnsupportedError("Unsupported scheme: $uri"); + throw UnsupportedError("Unsupported scheme: $uri"); } /// Reads the bytes of a URI as a list of bytes. @@ -31,7 +31,7 @@ if (uri.scheme == "data") { return uri.data.contentAsBytes(); } - throw new UnsupportedError("Unsupported scheme: $uri"); + throw UnsupportedError("Unsupported scheme: $uri"); } /// Reads the bytes of a URI as a string. @@ -47,7 +47,7 @@ if (uri.scheme == "data") { return uri.data.contentAsString(encoding: encoding); } - throw new UnsupportedError("Unsupported scheme: $uri"); + throw UnsupportedError("Unsupported scheme: $uri"); } Future<List<int>> _httpGetBytes(Uri uri) {
diff --git a/lib/src/io_io.dart b/lib/src/io_io.dart index 4903ee5..0370ff9 100644 --- a/lib/src/io_io.dart +++ b/lib/src/io_io.dart
@@ -19,11 +19,11 @@ /// Read the bytes of a URI as a stream of bytes. Stream<List<int>> readAsStream(Uri uri) async* { if (uri.scheme == "file") { - yield* new File.fromUri(uri).openRead(); + yield* File.fromUri(uri).openRead(); return; } if (uri.scheme == "http" || uri.scheme == "https") { - HttpClientResponse response = await _httpGetBytes(uri); + var response = await _httpGetBytes(uri); _throwIfFailed(response, uri); yield* response; return; @@ -32,21 +32,21 @@ yield uri.data.contentAsBytes(); return; } - throw new UnsupportedError("Unsupported scheme: $uri"); + throw UnsupportedError("Unsupported scheme: $uri"); } /// Read the bytes of a URI as a list of bytes. Future<List<int>> readAsBytes(Uri uri) async { if (uri.scheme == "file") { - return new File.fromUri(uri).readAsBytes(); + return File.fromUri(uri).readAsBytes(); } if (uri.scheme == "http" || uri.scheme == "https") { - HttpClientResponse response = await _httpGetBytes(uri); + var response = await _httpGetBytes(uri); _throwIfFailed(response, uri); - int length = response.contentLength; + var length = response.contentLength; if (length < 0) length = 0; // Create empty buffer with capacity matching contentLength. - var buffer = new Uint8Buffer(length)..length = 0; + var buffer = Uint8Buffer(length)..length = 0; await for (var bytes in response) { buffer.addAll(bytes); } @@ -55,49 +55,50 @@ if (uri.scheme == "data") { return uri.data.contentAsBytes(); } - throw new UnsupportedError("Unsupported scheme: $uri"); + throw UnsupportedError("Unsupported scheme: $uri"); } /// Read the bytes of a URI as a string. Future<String> readAsString(Uri uri, Encoding encoding) async { if (uri.scheme == "file") { - if (encoding == null) encoding = utf8; - return new File.fromUri(uri).readAsString(encoding: encoding); + encoding ??= utf8; + return File.fromUri(uri).readAsString(encoding: encoding); } if (uri.scheme == "http" || uri.scheme == "https") { - HttpClientRequest request = await new HttpClient().getUrl(uri); + var request = await HttpClient().getUrl(uri); // Prefer text/plain, text/* if possible, otherwise take whatever is there. request.headers.set(HttpHeaders.acceptHeader, "text/plain, text/*, */*"); if (encoding != null) { request.headers.set(HttpHeaders.acceptCharsetHeader, encoding.name); } - HttpClientResponse response = await request.close(); + var response = await request.close(); _throwIfFailed(response, uri); encoding ??= Encoding.getByName(response.headers.contentType?.charset); if (encoding == null || encoding == latin1) { // Default to LATIN-1 if no encoding found. // Special case LATIN-1 since it is common and doesn't need decoding. - int length = response.contentLength; + var length = response.contentLength; if (length < 0) length = 0; // Create empty buffer with capacity matching contentLength. - var buffer = new Uint8Buffer(length)..length = 0; + var buffer = Uint8Buffer(length)..length = 0; await for (var bytes in response) { buffer.addAll(bytes); } var byteList = buffer.buffer.asUint8List(0, buffer.length); - return new String.fromCharCodes(byteList); + return String.fromCharCodes(byteList); } return response.cast<List<int>>().transform(encoding.decoder).join(); } if (uri.scheme == "data") { return uri.data.contentAsString(encoding: encoding); } - throw new UnsupportedError("Unsupported scheme: $uri"); + throw UnsupportedError("Unsupported scheme: $uri"); } -HttpClient _sharedHttpClient = new HttpClient()..maxConnectionsPerHost = 6; +HttpClient _sharedHttpClient = HttpClient()..maxConnectionsPerHost = 6; Future<HttpClientResponse> _httpGetBytes(Uri uri) async { + // ignore: omit_local_variable_types HttpClientRequest request = await _sharedHttpClient.getUrl(uri); request.headers .set(HttpHeaders.acceptHeader, "application/octet-stream, */*"); @@ -107,6 +108,6 @@ void _throwIfFailed(HttpClientResponse response, Uri uri) { var statusCode = response.statusCode; if (statusCode < HttpStatus.ok || statusCode > HttpStatus.noContent) { - throw new HttpException(response.reasonPhrase, uri: uri); + throw HttpException(response.reasonPhrase, uri: uri); } }
diff --git a/lib/src/io_none.dart b/lib/src/io_none.dart index 3c67eb6..bf34510 100644 --- a/lib/src/io_none.dart +++ b/lib/src/io_none.dart
@@ -11,7 +11,7 @@ yield uri.data.contentAsBytes(); return; } - throw new UnsupportedError("Unsupported scheme: $uri"); + throw UnsupportedError("Unsupported scheme: $uri"); } /// Read the bytes of a URI as a list of bytes. @@ -19,7 +19,7 @@ if (uri.scheme == "data") { return uri.data.contentAsBytes(); } - throw new UnsupportedError("Unsupported scheme: $uri"); + throw UnsupportedError("Unsupported scheme: $uri"); } /// Read the bytes of a URI as a string. @@ -27,5 +27,5 @@ if (uri.scheme == "data") { return uri.data.contentAsString(encoding: encoding); } - throw new UnsupportedError("Unsupported scheme: $uri"); + throw UnsupportedError("Unsupported scheme: $uri"); }
diff --git a/lib/src/resolve.dart b/lib/src/resolve.dart index 053899a..4858ccf 100644 --- a/lib/src/resolve.dart +++ b/lib/src/resolve.dart
@@ -10,10 +10,10 @@ if (uri.scheme == "package") { return Isolate.resolvePackageUri(uri).then((resolvedUri) { if (resolvedUri == null) { - throw new ArgumentError.value(uri, "uri", "Unknown package"); + throw ArgumentError.value(uri, "uri", "Unknown package"); } return resolvedUri; }); } - return new Future<Uri>.value(Uri.base.resolveUri(uri)); + return Future<Uri>.value(Uri.base.resolveUri(uri)); }
diff --git a/lib/src/resource.dart b/lib/src/resource.dart index d5c4208..258ff76 100644 --- a/lib/src/resource.dart +++ b/lib/src/resource.dart
@@ -20,7 +20,7 @@ final ResourceLoader _loader; /// The URI of the resource. - final _uri; + final dynamic _uri; /// Creates a resource object with the given [uri] as location. /// @@ -55,13 +55,13 @@ /// Reads the resource content as a stream of bytes. Stream<List<int>> openRead() async* { - Uri uri = await resolveUri(this.uri); + var uri = await resolveUri(this.uri); yield* _loader.openRead(uri); } /// Reads the resource content as a single list of bytes. Future<List<int>> readAsBytes() async { - Uri uri = await resolveUri(this.uri); + var uri = await resolveUri(this.uri); return _loader.readAsBytes(uri); } @@ -71,7 +71,7 @@ /// If no encoding is provided, an encoding is chosen depending on the /// protocol and/or available metadata. Future<String> readAsString({Encoding encoding}) async { - Uri uri = await resolveUri(this.uri); + var uri = await resolveUri(this.uri); return _loader.readAsString(uri, encoding: encoding); } }
diff --git a/lib/src/resource_loader.dart b/lib/src/resource_loader.dart index 3f282e8..30a39a3 100644 --- a/lib/src/resource_loader.dart +++ b/lib/src/resource_loader.dart
@@ -28,10 +28,10 @@ /// Relative URI references are accepted - they are resolved against /// [Uri.base] before being loaded. /// - /// This loader is automatically used by the [Resource] class + /// This loader is automatically used by the `Resource` class /// if no other loader is specified. static ResourceLoader get defaultLoader => - const PackageLoader(const DefaultLoader()); + const PackageLoader(DefaultLoader()); /// Reads the file located by [uri] as a stream of bytes. Stream<List<int>> openRead(Uri uri);
diff --git a/pubspec.yaml b/pubspec.yaml index 81c2baa..ee6b0b6 100644 --- a/pubspec.yaml +++ b/pubspec.yaml
@@ -11,3 +11,4 @@ typed_data: "^1.0.0" dev_dependencies: test: ^1.0.0 + pedantic: ^1.8.0
diff --git a/test/browser_http_test.dart b/test/browser_http_test.dart index a8f21d9..fd5ddb0 100644 --- a/test/browser_http_test.dart +++ b/test/browser_http_test.dart
@@ -4,7 +4,6 @@ @TestOn("browser") -import "dart:async"; import "dart:convert"; import "package:resource/resource.dart"; @@ -12,7 +11,7 @@ const content = "Rødgrød med fløde"; -main() { +void main() { // Assume test files located next to Uri.base. // This is how "pub run test" currently works. @@ -20,35 +19,35 @@ var loader = ResourceLoader.defaultLoader; // The HTTPXmlRequest loader defaults to UTF-8 encoding. var uri = Uri.base.resolve("testfile-utf8.txt"); - String string = await loader.readAsString(uri); + var string = await loader.readAsString(uri); expect(string, content); }); test("Latin-1 encoding", () async { var loader = ResourceLoader.defaultLoader; var uri = Uri.base.resolve("testfile-latin1.txt"); - String string = await loader.readAsString(uri, encoding: latin1); + var string = await loader.readAsString(uri, encoding: latin1); expect(string, content); }); test("UTF-8 encoding", () async { var loader = ResourceLoader.defaultLoader; var uri = Uri.base.resolve("testfile-utf8.txt"); - String string = await loader.readAsString(uri, encoding: utf8); + var string = await loader.readAsString(uri, encoding: utf8); expect(string, content); }); test("bytes", () async { var loader = ResourceLoader.defaultLoader; var uri = Uri.base.resolve("testfile-latin1.txt"); - List<int> bytes = await loader.readAsBytes(uri); + var bytes = await loader.readAsBytes(uri); expect(bytes, content.codeUnits); }); test("byte stream", () async { var loader = ResourceLoader.defaultLoader; var uri = Uri.base.resolve("testfile-latin1.txt"); - Stream<List<int>> bytes = loader.openRead(uri); + var bytes = loader.openRead(uri); var buffer = []; await bytes.forEach(buffer.addAll); expect(buffer, content.codeUnits);
diff --git a/test/loader_data_test.dart b/test/loader_data_test.dart index 19c898a..1e61316 100644 --- a/test/loader_data_test.dart +++ b/test/loader_data_test.dart
@@ -11,25 +11,25 @@ const content = "Rødgrød med fløde"; -main() { - testFile(Encoding encoding, bool base64) { +void main() { + void testFile(Encoding encoding, bool base64) { group("${encoding.name}${base64 ? " base64" : ""}", () { - var uri; + Uri uri; setUp(() { var dataUri = - new UriData.fromString(content, encoding: encoding, base64: base64); + UriData.fromString(content, encoding: encoding, base64: base64); uri = dataUri.uri; }); test("read string", () async { var loader = ResourceLoader.defaultLoader; - String string = await loader.readAsString(uri, encoding: encoding); + var string = await loader.readAsString(uri, encoding: encoding); expect(string, content); }); test("read bytes", () async { var loader = ResourceLoader.defaultLoader; - List<int> bytes = await loader.readAsBytes(uri); + var bytes = await loader.readAsBytes(uri); expect(bytes, encoding.encode(content)); });
diff --git a/test/loader_file_test.dart b/test/loader_file_test.dart index 30e33bb..cd10399 100644 --- a/test/loader_file_test.dart +++ b/test/loader_file_test.dart
@@ -12,32 +12,33 @@ const content = "Rødgrød med fløde"; -main() { - var dir; - int dirCounter = 0; +void main() { + Directory dir; + var dirCounter = 0; setUp(() { dir = Directory.systemTemp.createTempSync('testdir${dirCounter++}'); }); - testFile(Encoding encoding) { + void testFile(Encoding encoding) { group("${encoding.name}", () { - var file; - var uri; + File file; + Uri uri; setUp(() { var dirUri = dir.uri; uri = dirUri.resolve("file.txt"); - file = new File.fromUri(uri); + file = File.fromUri(uri); + // ignore: cascade_invocations file.writeAsBytesSync(encoding.encode(content)); }); test("read string", () async { var loader = ResourceLoader.defaultLoader; - String string = await loader.readAsString(uri, encoding: encoding); + var string = await loader.readAsString(uri, encoding: encoding); expect(string, content); }); test("read bytes", () async { var loader = ResourceLoader.defaultLoader; - List<int> bytes = await loader.readAsBytes(uri); + var bytes = await loader.readAsBytes(uri); expect(bytes, encoding.encode(content)); });
diff --git a/test/loader_http_test.dart b/test/loader_http_test.dart index 856971d..9f62230 100644 --- a/test/loader_http_test.dart +++ b/test/loader_http_test.dart
@@ -7,20 +7,21 @@ import "dart:convert"; import "dart:io"; +import 'package:pedantic/pedantic.dart'; import "package:resource/resource.dart"; import "package:test/test.dart"; const content = "Rødgrød med fløde"; -main() { - var server; - var uri; +void main() { + HttpServer server; + Uri uri; setUp(() async { var addr = (await InternetAddress.lookup("localhost"))[0]; server = await HttpServer.bind(addr, 0); - int port = server.port; + var port = server.port; uri = Uri.parse("http://localhost:$port/default.html"); - server.forEach((HttpRequest request) { + unawaited(server.forEach((HttpRequest request) { if (request.uri.path.endsWith(".not")) { request.response ..statusCode = HttpStatus.notFound @@ -30,7 +31,7 @@ var encodings = request.headers[HttpHeaders.acceptCharsetHeader]; var encoding = parseAcceptCharset(encodings); request.response.headers.contentType = - new ContentType("text", "plain", charset: encoding.name); + ContentType("text", "plain", charset: encoding.name); if (request.uri.query.contains("length")) { request.response.headers.contentLength = encoding.encode(content).length; @@ -41,52 +42,55 @@ }).catchError((e, stack) { print(e); print(stack); - }); + })); }); test("Default encoding", () async { var loader = ResourceLoader.defaultLoader; - String string = await loader.readAsString(uri); + var string = await loader.readAsString(uri); expect(string, content); }); test("Latin-1 encoding", () async { var loader = ResourceLoader.defaultLoader; - String string = await loader.readAsString(uri, encoding: latin1); + var string = await loader.readAsString(uri, encoding: latin1); expect(string, content); }); test("Latin-1 encoding w/ length", () async { // Regression test for issue #21. var loader = ResourceLoader.defaultLoader; - var newUri = uri.replace(query: "length"); // Request length set. - String string = await loader.readAsString(newUri, encoding: latin1); + // ignore: non_constant_identifier_names + var Uri = uri.replace(query: "length"); // Request length set. + var string = await loader.readAsString(Uri, encoding: latin1); expect(string, content); }); test("UTF-8 encoding", () async { var loader = ResourceLoader.defaultLoader; - var newUri = uri.replace(query: "length"); // Request length set. - String string = await loader.readAsString(newUri, encoding: utf8); + // ignore: non_constant_identifier_names + var Uri = uri.replace(query: "length"); // Request length set. + var string = await loader.readAsString(Uri, encoding: utf8); expect(string, content); }); test("UTF-8 encoding w/ length", () async { var loader = ResourceLoader.defaultLoader; - String string = await loader.readAsString(uri, encoding: utf8); + var string = await loader.readAsString(uri, encoding: utf8); expect(string, content); }); test("bytes", () async { var loader = ResourceLoader.defaultLoader; - List<int> bytes = await loader.readAsBytes(uri); // Sender uses Latin-1. + var bytes = await loader.readAsBytes(uri); // Sender uses Latin-1. expect(bytes, content.codeUnits); }); test("bytes w/ length", () async { var loader = ResourceLoader.defaultLoader; - var newUri = uri.replace(query: "length"); // Request length set. - List<int> bytes = await loader.readAsBytes(newUri); // Sender uses Latin-1. + // ignore: non_constant_identifier_names + var Uri = uri.replace(query: "length"); // Request length set. + var bytes = await loader.readAsBytes(Uri); // Sender uses Latin-1. expect(bytes, content.codeUnits); }); @@ -126,7 +130,7 @@ Encoding encoding = latin1; if (headers != null) { var weight = 0.0; - var pattern = new RegExp(r"([\w-]+)(;\s*q=[\d.]+)?"); + var pattern = RegExp(r"([\w-]+)(;\s*q=[\d.]+)?"); for (var acceptCharset in headers) { for (var match in pattern.allMatches(acceptCharset)) { var e = Encoding.getByName(match[1]);
diff --git a/test/resource_test.dart b/test/resource_test.dart index 7e7a88a..e06b4df 100644 --- a/test/resource_test.dart +++ b/test/resource_test.dart
@@ -10,8 +10,8 @@ import "package:resource/resource.dart"; import "package:test/test.dart"; -main() { - pkguri(path) => new Uri(scheme: "package", path: path); +void main() { + Uri pkguri(path) => Uri(scheme: "package", path: path); Future<Uri> resolve(Uri source) async { if (source.scheme == "package") { @@ -21,9 +21,9 @@ } group("loading", () { - testLoad(Uri uri) async { - LogLoader loader = new LogLoader(); - var resource = new Resource(uri, loader: loader); + Future testLoad(Uri uri) async { + var loader = LogLoader(); + var resource = Resource(uri, loader: loader); var res = await resource.openRead().toList(); var resolved = await resolve(uri); expect(res, [