Skip to content

Commit 4bccdd6

Browse files
authored
Fix newly enforced package:pedantic lints (flutter#123)
- always_declare_return_types - annotate_overrides - omit_local_variable_types - prefer_conditional_assignment - prefer_single_quotes - unnecessary_this - use_function_type_syntax_for_parameters
1 parent 519e91d commit 4bccdd6

27 files changed

+240
-225
lines changed

example/example.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import 'package:shelf/shelf.dart' as shelf;
66
import 'package:shelf/shelf_io.dart' as io;
77

8-
main() async {
8+
void main() async {
99
var handler = const shelf.Pipeline()
1010
.addMiddleware(shelf.logRequests())
1111
.addHandler(_echoRequest);

lib/shelf_io.dart

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export 'src/io_server.dart';
4141
Future<HttpServer> serve(Handler handler, address, int port,
4242
{SecurityContext securityContext, int backlog, bool shared = false}) async {
4343
backlog ??= 0;
44-
HttpServer server = await (securityContext == null
44+
var server = await (securityContext == null
4545
? HttpServer.bind(address, port, backlog: backlog, shared: shared)
4646
: HttpServer.bindSecure(address, port, securityContext,
4747
backlog: backlog, shared: shared));
@@ -121,10 +121,10 @@ Future handleRequest(HttpRequest request, Handler handler) async {
121121
}
122122

123123
var message = StringBuffer()
124-
..writeln("Got a response for hijacked request "
125-
"${shelfRequest.method} ${shelfRequest.requestedUri}:")
124+
..writeln('Got a response for hijacked request '
125+
'${shelfRequest.method} ${shelfRequest.requestedUri}:')
126126
..writeln(response.statusCode);
127-
response.headers.forEach((key, value) => message.writeln("$key: $value"));
127+
response.headers.forEach((key, value) => message.writeln('$key: $value'));
128128
throw Exception(message.toString().trim());
129129
}
130130

@@ -140,7 +140,7 @@ Request _fromHttpRequest(HttpRequest request) {
140140
// Remove the Transfer-Encoding header per the adapter requirements.
141141
headers.remove(HttpHeaders.transferEncodingHeader);
142142

143-
void onHijack(void callback(StreamChannel<List<int>> channel)) {
143+
void onHijack(void Function(StreamChannel<List<int>>) callback) {
144144
request.response
145145
.detachSocket(writeHeaders: false)
146146
.then((socket) => callback(StreamChannel(socket, socket)));
@@ -155,9 +155,9 @@ Request _fromHttpRequest(HttpRequest request) {
155155
}
156156

157157
Future _writeResponse(Response response, HttpResponse httpResponse) {
158-
if (response.context.containsKey("shelf.io.buffer_output")) {
158+
if (response.context.containsKey('shelf.io.buffer_output')) {
159159
httpResponse.bufferOutput =
160-
response.context["shelf.io.buffer_output"] as bool;
160+
response.context['shelf.io.buffer_output'] as bool;
161161
}
162162

163163
httpResponse.statusCode = response.statusCode;
@@ -210,9 +210,9 @@ Future _writeResponse(Response response, HttpResponse httpResponse) {
210210
Response _logError(Request request, String message, [StackTrace stackTrace]) {
211211
// Add information about the request itself.
212212
var buffer = StringBuffer();
213-
buffer.write("${request.method} ${request.requestedUri.path}");
213+
buffer.write('${request.method} ${request.requestedUri.path}');
214214
if (request.requestedUri.query.isNotEmpty) {
215-
buffer.write("?${request.requestedUri.query}");
215+
buffer.write('?${request.requestedUri.query}');
216216
}
217217
buffer.writeln();
218218
buffer.write(message);

lib/src/body.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class Body {
8484
Stream<List<int>> read() {
8585
if (_stream == null) {
8686
throw StateError("The 'read' method can only be called once on a "
87-
"shelf.Request/shelf.Response object.");
87+
'shelf.Request/shelf.Response object.');
8888
}
8989
var stream = _stream;
9090
_stream = null;

lib/src/cascade.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ class Cascade {
3939
/// considered unacceptable. If [shouldCascade] is passed, responses for which
4040
/// it returns `true` are considered unacceptable. [statusCodes] and
4141
/// [shouldCascade] may not both be passed.
42-
Cascade({Iterable<int> statusCodes, bool shouldCascade(Response response)})
42+
Cascade({Iterable<int> statusCodes, bool Function(Response) shouldCascade})
4343
: _shouldCascade = _computeShouldCascade(statusCodes, shouldCascade),
4444
_parent = null,
4545
_handler = null {
4646
if (statusCodes != null && shouldCascade != null) {
47-
throw ArgumentError("statusCodes and shouldCascade may not both be "
48-
"passed.");
47+
throw ArgumentError('statusCodes and shouldCascade may not both be '
48+
'passed.');
4949
}
5050
}
5151

@@ -65,7 +65,7 @@ class Cascade {
6565
Handler get handler {
6666
if (_handler == null) {
6767
throw StateError("Can't get a handler for a cascade with no inner "
68-
"handlers.");
68+
'handlers.');
6969
}
7070

7171
return (request) {
@@ -81,9 +81,9 @@ class Cascade {
8181
/// Computes the [Cascade._shouldCascade] function based on the user's
8282
/// parameters.
8383
_ShouldCascade _computeShouldCascade(
84-
Iterable<int> statusCodes, bool shouldCascade(Response response)) {
84+
Iterable<int> statusCodes, bool Function(Response) shouldCascade) {
8585
if (shouldCascade != null) return shouldCascade;
86-
if (statusCodes == null) statusCodes = [404, 405];
86+
statusCodes ??= [404, 405];
8787
statusCodes = statusCodes.toSet();
8888
return (response) => statusCodes.contains(response.statusCode);
8989
}

lib/src/hijack_exception.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
class HijackException implements Exception {
1313
const HijackException();
1414

15+
@override
1516
String toString() =>
1617
"A shelf request's underlying data stream was hijacked.\n"
17-
"This exception is used for control flow and should only be handled by a "
18-
"Shelf adapter.";
18+
'This exception is used for control flow and should only be handled by a '
19+
'Shelf adapter.';
1920
}

lib/src/io_server.dart

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,22 @@ class IOServer implements Server {
1818
/// Whether [mount] has been called.
1919
bool _mounted = false;
2020

21+
@override
2122
Uri get url {
2223
if (server.address.isLoopback) {
23-
return Uri(scheme: "http", host: "localhost", port: server.port);
24+
return Uri(scheme: 'http', host: 'localhost', port: server.port);
2425
}
2526

2627
// IPv6 addresses in URLs need to be enclosed in square brackets to avoid
2728
// URL ambiguity with the ":" in the address.
2829
if (server.address.type == InternetAddressType.IPv6) {
2930
return Uri(
30-
scheme: "http",
31-
host: "[${server.address.address}]",
31+
scheme: 'http',
32+
host: '[${server.address.address}]',
3233
port: server.port);
3334
}
3435

35-
return Uri(scheme: "http", host: server.address.address, port: server.port);
36+
return Uri(scheme: 'http', host: server.address.address, port: server.port);
3637
}
3738

3839
/// Calls [HttpServer.bind] and wraps the result in an [IOServer].
@@ -44,6 +45,7 @@ class IOServer implements Server {
4445

4546
IOServer(this.server);
4647

48+
@override
4749
void mount(Handler handler) {
4850
if (_mounted) {
4951
throw StateError("Can't mount two handlers for the same server.");
@@ -53,5 +55,6 @@ class IOServer implements Server {
5355
serveRequests(server, handler);
5456
}
5557

58+
@override
5659
Future close() => server.close();
5760
}

lib/src/message.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Body getBody(Message message) => message._body;
1717
/// The default set of headers for a message created with no body and no
1818
/// explicit headers.
1919
final _defaultHeaders =
20-
ShelfUnmodifiableMap<String>({"content-length": "0"}, ignoreKeyCase: true);
20+
ShelfUnmodifiableMap<String>({'content-length': '0'}, ignoreKeyCase: true);
2121

2222
/// Represents logic shared between [Request] and [Response].
2323
abstract class Message {
@@ -136,8 +136,7 @@ abstract class Message {
136136
///
137137
/// This calls [read] internally, which can only be called once.
138138
Future<String> readAsString([Encoding encoding]) {
139-
if (encoding == null) encoding = this.encoding;
140-
if (encoding == null) encoding = utf8;
139+
encoding ??= this.encoding ?? utf8;
141140
return encoding.decodeStream(read());
142141
}
143142

lib/src/middleware.dart

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,11 @@ typedef Middleware = Handler Function(Handler innerHandler);
4747
/// does it receive [HijackException]s. It can either return a new response or
4848
/// throw an error.
4949
Middleware createMiddleware(
50-
{FutureOr<Response> requestHandler(Request request),
51-
FutureOr<Response> responseHandler(Response response),
52-
FutureOr<Response> errorHandler(error, StackTrace stackTrace)}) {
53-
if (requestHandler == null) requestHandler = (request) => null;
54-
55-
if (responseHandler == null) responseHandler = (response) => response;
50+
{FutureOr<Response> Function(Request) requestHandler,
51+
FutureOr<Response> Function(Response) responseHandler,
52+
FutureOr<Response> Function(dynamic error, StackTrace) errorHandler}) {
53+
requestHandler ??= (request) => null;
54+
responseHandler ??= (response) => response;
5655

5756
void Function(Object, StackTrace) onError;
5857
if (errorHandler != null) {

lib/src/middleware/logger.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ import '../middleware.dart';
1919
/// The `isError` parameter indicates whether the message is caused by an error.
2020
///
2121
/// If [logger] is not passed, the message is just passed to [print].
22-
Middleware logRequests({void logger(String msg, bool isError)}) =>
22+
Middleware logRequests({void Function(String message, bool isError) logger}) =>
2323
(innerHandler) {
24-
if (logger == null) logger = _defaultLogger;
24+
logger ??= _defaultLogger;
2525

2626
return (request) {
2727
var startTime = DateTime.now();

lib/src/request.dart

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ class Request extends Message {
134134
body,
135135
Encoding encoding,
136136
Map<String, Object> context,
137-
void onHijack(void hijack(StreamChannel<List<int>> channel))})
137+
void Function(void Function(StreamChannel<List<int>>)) onHijack})
138138
: this._(method, requestedUri,
139139
protocolVersion: protocolVersion,
140140
headers: headers,
@@ -160,12 +160,11 @@ class Request extends Message {
160160
Encoding encoding,
161161
Map<String, Object> context,
162162
_OnHijack onHijack})
163-
: this.requestedUri = requestedUri,
164-
this.protocolVersion =
165-
protocolVersion == null ? '1.1' : protocolVersion,
166-
this.url = _computeUrl(requestedUri, handlerPath, url),
167-
this.handlerPath = _computeHandlerPath(requestedUri, handlerPath, url),
168-
this._onHijack = onHijack,
163+
: requestedUri = requestedUri,
164+
protocolVersion = protocolVersion ?? '1.1',
165+
url = _computeUrl(requestedUri, handlerPath, url),
166+
handlerPath = _computeHandlerPath(requestedUri, handlerPath, url),
167+
_onHijack = onHijack,
169168
super(body, encoding: encoding, headers: headers, context: context) {
170169
if (method.isEmpty) {
171170
throw ArgumentError.value(method, 'method', 'cannot be empty.');
@@ -214,6 +213,7 @@ class Request extends Message {
214213
/// request = request.change(path: "dir");
215214
/// print(request.handlerPath); // => /static/dir/
216215
/// print(request.url); // => file.html
216+
@override
217217
Request change(
218218
{Map<String, String> headers,
219219
Map<String, Object> context,
@@ -222,13 +222,13 @@ class Request extends Message {
222222
headers = updateMap(this.headers, headers);
223223
context = updateMap(this.context, context);
224224

225-
if (body == null) body = getBody(this);
225+
body ??= getBody(this);
226226

227227
var handlerPath = this.handlerPath;
228228
if (path != null) handlerPath += path;
229229

230-
return Request._(this.method, this.requestedUri,
231-
protocolVersion: this.protocolVersion,
230+
return Request._(method, requestedUri,
231+
protocolVersion: protocolVersion,
232232
headers: headers,
233233
handlerPath: handlerPath,
234234
body: body,
@@ -247,7 +247,7 @@ class Request extends Message {
247247
/// hijacking, such as the `dart:io` adapter. In addition, a given request may
248248
/// only be hijacked once. [canHijack] can be used to detect whether this
249249
/// request can be hijacked.
250-
void hijack(void callback(StreamChannel<List<int>> channel)) {
250+
void hijack(void Function(StreamChannel<List<int>>) callback) {
251251
if (_onHijack == null) {
252252
throw StateError("This request can't be hijacked.");
253253
}
@@ -272,8 +272,8 @@ class _OnHijack {
272272
/// Calls [this].
273273
///
274274
/// Throws a [StateError] if [this] has already been called.
275-
void run(void callback(StreamChannel<List<int>> channel)) {
276-
if (called) throw StateError("This request has already been hijacked.");
275+
void run(void Function(StreamChannel<List<int>>) callback) {
276+
if (called) throw StateError('This request has already been hijacked.');
277277
called = true;
278278
newFuture(() => _callback(callback));
279279
}
@@ -286,8 +286,8 @@ class _OnHijack {
286286
Uri _computeUrl(Uri requestedUri, String handlerPath, Uri url) {
287287
if (handlerPath != null &&
288288
handlerPath != requestedUri.path &&
289-
!handlerPath.endsWith("/")) {
290-
handlerPath += "/";
289+
!handlerPath.endsWith('/')) {
290+
handlerPath += '/';
291291
}
292292

293293
if (url != null) {
@@ -336,8 +336,8 @@ Uri _computeUrl(Uri requestedUri, String handlerPath, Uri url) {
336336
String _computeHandlerPath(Uri requestedUri, String handlerPath, Uri url) {
337337
if (handlerPath != null &&
338338
handlerPath != requestedUri.path &&
339-
!handlerPath.endsWith("/")) {
340-
handlerPath += "/";
339+
!handlerPath.endsWith('/')) {
340+
handlerPath += '/';
341341
}
342342

343343
if (handlerPath != null) {

0 commit comments

Comments
 (0)