Skip to content

Commit e3a922d

Browse files
authored
Fix newly enforced package:pedantic lints (flutter#78)
- always_declare_return_types - annotate_overrides - prefer_collection_literals - prefer_conditional_assignment - prefer_final_fields - prefer_if_null_operators - prefer_single_quotes - use_function_type_syntax_for_parameters Bump min SDK to 2.2.0 to allow Set literals.
1 parent f3b63aa commit e3a922d

28 files changed

+475
-408
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ language: dart
22

33
dart:
44
- dev
5-
- 2.0.0
5+
- 2.2.0
66

77
dart_task:
88
- test

benchmark/path_set.dart

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ import 'package:path/path.dart' as p;
1313

1414
import 'package:watcher/src/path_set.dart';
1515

16-
final String root = Platform.isWindows ? r"C:\root" : "/root";
16+
final String root = Platform.isWindows ? r'C:\root' : '/root';
1717

1818
/// Base class for benchmarks on [PathSet].
1919
abstract class PathSetBenchmark extends BenchmarkBase {
20-
PathSetBenchmark(String method) : super("PathSet.$method");
20+
PathSetBenchmark(String method) : super('PathSet.$method');
2121

2222
final PathSet pathSet = PathSet(root);
2323

@@ -30,14 +30,14 @@ abstract class PathSetBenchmark extends BenchmarkBase {
3030
///
3131
/// Each virtual directory contains ten entries: either subdirectories or
3232
/// files.
33-
void walkTree(int depth, callback(String path)) {
34-
recurse(String path, remainingDepth) {
33+
void walkTree(int depth, void Function(String) callback) {
34+
void recurse(String path, remainingDepth) {
3535
for (var i = 0; i < 10; i++) {
3636
var padded = i.toString().padLeft(2, '0');
3737
if (remainingDepth == 0) {
38-
callback(p.join(path, "file_$padded.txt"));
38+
callback(p.join(path, 'file_$padded.txt'));
3939
} else {
40-
var subdir = p.join(path, "subdirectory_$padded");
40+
var subdir = p.join(path, 'subdirectory_$padded');
4141
recurse(subdir, remainingDepth - 1);
4242
}
4343
}
@@ -48,16 +48,18 @@ abstract class PathSetBenchmark extends BenchmarkBase {
4848
}
4949

5050
class AddBenchmark extends PathSetBenchmark {
51-
AddBenchmark() : super("add()");
51+
AddBenchmark() : super('add()');
5252

5353
final List<String> paths = [];
5454

55+
@override
5556
void setup() {
5657
// Make a bunch of paths in about the same order we expect to get them from
5758
// Directory.list().
5859
walkTree(3, paths.add);
5960
}
6061

62+
@override
6163
void run() {
6264
for (var path in paths) {
6365
pathSet.add(path);
@@ -66,10 +68,11 @@ class AddBenchmark extends PathSetBenchmark {
6668
}
6769

6870
class ContainsBenchmark extends PathSetBenchmark {
69-
ContainsBenchmark() : super("contains()");
71+
ContainsBenchmark() : super('contains()');
7072

7173
final List<String> paths = [];
7274

75+
@override
7376
void setup() {
7477
// Add a bunch of paths to the set.
7578
walkTree(3, (path) {
@@ -80,48 +83,52 @@ class ContainsBenchmark extends PathSetBenchmark {
8083
// Add some non-existent paths to test the false case.
8184
for (var i = 0; i < 100; i++) {
8285
paths.addAll([
83-
"/nope",
84-
"/root/nope",
85-
"/root/subdirectory_04/nope",
86-
"/root/subdirectory_04/subdirectory_04/nope",
87-
"/root/subdirectory_04/subdirectory_04/subdirectory_04/nope",
88-
"/root/subdirectory_04/subdirectory_04/subdirectory_04/nope/file_04.txt",
86+
'/nope',
87+
'/root/nope',
88+
'/root/subdirectory_04/nope',
89+
'/root/subdirectory_04/subdirectory_04/nope',
90+
'/root/subdirectory_04/subdirectory_04/subdirectory_04/nope',
91+
'/root/subdirectory_04/subdirectory_04/subdirectory_04/nope/file_04.txt',
8992
]);
9093
}
9194
}
9295

96+
@override
9397
void run() {
9498
var contained = 0;
9599
for (var path in paths) {
96100
if (pathSet.contains(path)) contained++;
97101
}
98102

99-
if (contained != 10000) throw "Wrong result: $contained";
103+
if (contained != 10000) throw 'Wrong result: $contained';
100104
}
101105
}
102106

103107
class PathsBenchmark extends PathSetBenchmark {
104-
PathsBenchmark() : super("toSet()");
108+
PathsBenchmark() : super('toSet()');
105109

110+
@override
106111
void setup() {
107112
walkTree(3, pathSet.add);
108113
}
109114

115+
@override
110116
void run() {
111117
var count = 0;
112118
for (var _ in pathSet.paths) {
113119
count++;
114120
}
115121

116-
if (count != 10000) throw "Wrong result: $count";
122+
if (count != 10000) throw 'Wrong result: $count';
117123
}
118124
}
119125

120126
class RemoveBenchmark extends PathSetBenchmark {
121-
RemoveBenchmark() : super("remove()");
127+
RemoveBenchmark() : super('remove()');
122128

123129
final List<String> paths = [];
124130

131+
@override
125132
void setup() {
126133
// Make a bunch of paths. Do this here so that we don't spend benchmarked
127134
// time synthesizing paths.
@@ -136,14 +143,15 @@ class RemoveBenchmark extends PathSetBenchmark {
136143
paths.shuffle(random);
137144
}
138145

146+
@override
139147
void run() {
140148
for (var path in paths) {
141149
pathSet.remove(path);
142150
}
143151
}
144152
}
145153

146-
main() {
154+
void main() {
147155
AddBenchmark().report();
148156
ContainsBenchmark().report();
149157
PathsBenchmark().report();

example/watch.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ library watch;
88
import 'package:path/path.dart' as p;
99
import 'package:watcher/watcher.dart';
1010

11-
main(List<String> arguments) {
11+
void main(List<String> arguments) {
1212
if (arguments.length != 1) {
13-
print("Usage: watch <directory path>");
13+
print('Usage: watch <directory path>');
1414
return;
1515
}
1616

lib/src/constructable_file_system_event.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,54 +5,67 @@
55
import 'dart:io';
66

77
abstract class _ConstructableFileSystemEvent implements FileSystemEvent {
8+
@override
89
final bool isDirectory;
10+
@override
911
final String path;
12+
@override
1013
int get type;
1114

1215
_ConstructableFileSystemEvent(this.path, this.isDirectory);
1316
}
1417

1518
class ConstructableFileSystemCreateEvent extends _ConstructableFileSystemEvent
1619
implements FileSystemCreateEvent {
20+
@override
1721
final type = FileSystemEvent.create;
1822

1923
ConstructableFileSystemCreateEvent(String path, bool isDirectory)
2024
: super(path, isDirectory);
2125

26+
@override
2227
String toString() => "FileSystemCreateEvent('$path')";
2328
}
2429

2530
class ConstructableFileSystemDeleteEvent extends _ConstructableFileSystemEvent
2631
implements FileSystemDeleteEvent {
32+
@override
2733
final type = FileSystemEvent.delete;
2834

2935
ConstructableFileSystemDeleteEvent(String path, bool isDirectory)
3036
: super(path, isDirectory);
3137

38+
@override
3239
String toString() => "FileSystemDeleteEvent('$path')";
3340
}
3441

3542
class ConstructableFileSystemModifyEvent extends _ConstructableFileSystemEvent
3643
implements FileSystemModifyEvent {
44+
@override
3745
final bool contentChanged;
46+
@override
3847
final type = FileSystemEvent.modify;
3948

4049
ConstructableFileSystemModifyEvent(
4150
String path, bool isDirectory, this.contentChanged)
4251
: super(path, isDirectory);
4352

53+
@override
4454
String toString() =>
4555
"FileSystemModifyEvent('$path', contentChanged=$contentChanged)";
4656
}
4757

4858
class ConstructableFileSystemMoveEvent extends _ConstructableFileSystemEvent
4959
implements FileSystemMoveEvent {
60+
@override
5061
final String destination;
62+
@override
5163
final type = FileSystemEvent.move;
5264

5365
ConstructableFileSystemMoveEvent(
5466
String path, bool isDirectory, this.destination)
5567
: super(path, isDirectory);
5668

69+
@override
5770
String toString() => "FileSystemMoveEvent('$path', '$destination')";
5871
}

lib/src/directory_watcher.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import 'directory_watcher/polling.dart';
1414
/// in the directory has changed.
1515
abstract class DirectoryWatcher implements Watcher {
1616
/// The directory whose contents are being monitored.
17-
@Deprecated("Expires in 1.0.0. Use DirectoryWatcher.path instead.")
17+
@Deprecated('Expires in 1.0.0. Use DirectoryWatcher.path instead.')
1818
String get directory;
1919

2020
/// Creates a new [DirectoryWatcher] monitoring [directory].

lib/src/directory_watcher/linux.dart

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import '../watch_event.dart';
2525
/// (issue 14424).
2626
class LinuxDirectoryWatcher extends ResubscribableWatcher
2727
implements DirectoryWatcher {
28+
@override
2829
String get directory => path;
2930

3031
LinuxDirectoryWatcher(String directory)
@@ -33,20 +34,25 @@ class LinuxDirectoryWatcher extends ResubscribableWatcher
3334

3435
class _LinuxDirectoryWatcher
3536
implements DirectoryWatcher, ManuallyClosedWatcher {
37+
@override
3638
String get directory => _files.root;
39+
@override
3740
String get path => _files.root;
3841

42+
@override
3943
Stream<WatchEvent> get events => _eventsController.stream;
4044
final _eventsController = StreamController<WatchEvent>.broadcast();
4145

46+
@override
4247
bool get isReady => _readyCompleter.isCompleted;
4348

49+
@override
4450
Future get ready => _readyCompleter.future;
4551
final _readyCompleter = Completer();
4652

4753
/// A stream group for the [Directory.watch] events of [path] and all its
4854
/// subdirectories.
49-
var _nativeEvents = StreamGroup<FileSystemEvent>();
55+
final _nativeEvents = StreamGroup<FileSystemEvent>();
5056

5157
/// All known files recursively within [path].
5258
final PathSet _files;
@@ -60,7 +66,7 @@ class _LinuxDirectoryWatcher
6066
///
6167
/// These are gathered together so that they may all be canceled when the
6268
/// watcher is closed.
63-
final _subscriptions = Set<StreamSubscription>();
69+
final _subscriptions = <StreamSubscription>{};
6470

6571
_LinuxDirectoryWatcher(String path) : _files = PathSet(path) {
6672
_nativeEvents.add(Directory(path)
@@ -93,6 +99,7 @@ class _LinuxDirectoryWatcher
9399
}, cancelOnError: true);
94100
}
95101

102+
@override
96103
void close() {
97104
for (var subscription in _subscriptions) {
98105
subscription.cancel();
@@ -128,9 +135,9 @@ class _LinuxDirectoryWatcher
128135

129136
/// The callback that's run when a batch of changes comes in.
130137
void _onBatch(List<FileSystemEvent> batch) {
131-
var files = Set<String>();
132-
var dirs = Set<String>();
133-
var changed = Set<String>();
138+
var files = <String>{};
139+
var dirs = <String>{};
140+
var changed = <String>{};
134141

135142
// inotify event batches are ordered by occurrence, so we treat them as a
136143
// log of what happened to a file. We only emit events based on the
@@ -250,8 +257,8 @@ class _LinuxDirectoryWatcher
250257

251258
/// Like [Stream.listen], but automatically adds the subscription to
252259
/// [_subscriptions] so that it can be canceled when [close] is called.
253-
void _listen<T>(Stream<T> stream, void onData(T event),
254-
{Function onError, void onDone(), bool cancelOnError}) {
260+
void _listen<T>(Stream<T> stream, void Function(T) onData,
261+
{Function onError, void Function() onDone, bool cancelOnError}) {
255262
StreamSubscription subscription;
256263
subscription = stream.listen(onData, onError: onError, onDone: () {
257264
_subscriptions.remove(subscription);

lib/src/directory_watcher/mac_os.dart

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import '../watch_event.dart';
2424
/// [Directory.watch].
2525
class MacOSDirectoryWatcher extends ResubscribableWatcher
2626
implements DirectoryWatcher {
27+
@override
2728
String get directory => path;
2829

2930
MacOSDirectoryWatcher(String directory)
@@ -32,14 +33,19 @@ class MacOSDirectoryWatcher extends ResubscribableWatcher
3233

3334
class _MacOSDirectoryWatcher
3435
implements DirectoryWatcher, ManuallyClosedWatcher {
36+
@override
3537
String get directory => path;
38+
@override
3639
final String path;
3740

41+
@override
3842
Stream<WatchEvent> get events => _eventsController.stream;
3943
final _eventsController = StreamController<WatchEvent>.broadcast();
4044

45+
@override
4146
bool get isReady => _readyCompleter.isCompleted;
4247

48+
@override
4349
Future get ready => _readyCompleter.future;
4450
final _readyCompleter = Completer();
4551

@@ -64,7 +70,7 @@ class _MacOSDirectoryWatcher
6470

6571
/// The subscriptions to [Directory.list] calls for listing the contents of a
6672
/// subdirectory that was moved into the watched directory.
67-
final _listSubscriptions = Set<StreamSubscription<FileSystemEntity>>();
73+
final _listSubscriptions = <StreamSubscription<FileSystemEntity>>{};
6874

6975
/// The timer for tracking how long we wait for an initial batch of bogus
7076
/// events (see issue 14373).
@@ -85,6 +91,7 @@ class _MacOSDirectoryWatcher
8591
.then((_) => _readyCompleter.complete());
8692
}
8793

94+
@override
8895
void close() {
8996
if (_watchSubscription != null) _watchSubscription.cancel();
9097
if (_initialListSubscription != null) _initialListSubscription.cancel();
@@ -181,19 +188,19 @@ class _MacOSDirectoryWatcher
181188
// directory's full contents will be examined anyway, so we ignore such
182189
// events. Emitting them could cause useless or out-of-order events.
183190
var directories = unionAll(batch.map((event) {
184-
if (!event.isDirectory) return Set<String>();
191+
if (!event.isDirectory) return <String>{};
185192
if (event is FileSystemMoveEvent) {
186-
return Set<String>.from([event.path, event.destination]);
193+
return {event.path, event.destination};
187194
}
188-
return Set<String>.from([event.path]);
195+
return {event.path};
189196
}));
190197

191-
isInModifiedDirectory(String path) =>
198+
bool isInModifiedDirectory(String path) =>
192199
directories.any((dir) => path != dir && path.startsWith(dir));
193200

194-
addEvent(String path, FileSystemEvent event) {
201+
void addEvent(String path, FileSystemEvent event) {
195202
if (isInModifiedDirectory(path)) return;
196-
eventsForPaths.putIfAbsent(path, () => Set<FileSystemEvent>()).add(event);
203+
eventsForPaths.putIfAbsent(path, () => <FileSystemEvent>{}).add(event);
197204
}
198205

199206
for (var event in batch) {

0 commit comments

Comments
 (0)