Skip to content

Commit

Permalink
Fix newly enforced package:pedantic lints (#78)
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
natebosch authored Dec 6, 2019
1 parent f3b63aa commit e3a922d
Show file tree
Hide file tree
Showing 28 changed files with 475 additions and 408 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ language: dart

dart:
- dev
- 2.0.0
- 2.2.0

dart_task:
- test
Expand Down
46 changes: 27 additions & 19 deletions benchmark/path_set.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import 'package:path/path.dart' as p;

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

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

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

final PathSet pathSet = PathSet(root);

Expand All @@ -30,14 +30,14 @@ abstract class PathSetBenchmark extends BenchmarkBase {
///
/// Each virtual directory contains ten entries: either subdirectories or
/// files.
void walkTree(int depth, callback(String path)) {
recurse(String path, remainingDepth) {
void walkTree(int depth, void Function(String) callback) {
void recurse(String path, remainingDepth) {
for (var i = 0; i < 10; i++) {
var padded = i.toString().padLeft(2, '0');
if (remainingDepth == 0) {
callback(p.join(path, "file_$padded.txt"));
callback(p.join(path, 'file_$padded.txt'));
} else {
var subdir = p.join(path, "subdirectory_$padded");
var subdir = p.join(path, 'subdirectory_$padded');
recurse(subdir, remainingDepth - 1);
}
}
Expand All @@ -48,16 +48,18 @@ abstract class PathSetBenchmark extends BenchmarkBase {
}

class AddBenchmark extends PathSetBenchmark {
AddBenchmark() : super("add()");
AddBenchmark() : super('add()');

final List<String> paths = [];

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

@override
void run() {
for (var path in paths) {
pathSet.add(path);
Expand All @@ -66,10 +68,11 @@ class AddBenchmark extends PathSetBenchmark {
}

class ContainsBenchmark extends PathSetBenchmark {
ContainsBenchmark() : super("contains()");
ContainsBenchmark() : super('contains()');

final List<String> paths = [];

@override
void setup() {
// Add a bunch of paths to the set.
walkTree(3, (path) {
Expand All @@ -80,48 +83,52 @@ class ContainsBenchmark extends PathSetBenchmark {
// Add some non-existent paths to test the false case.
for (var i = 0; i < 100; i++) {
paths.addAll([
"/nope",
"/root/nope",
"/root/subdirectory_04/nope",
"/root/subdirectory_04/subdirectory_04/nope",
"/root/subdirectory_04/subdirectory_04/subdirectory_04/nope",
"/root/subdirectory_04/subdirectory_04/subdirectory_04/nope/file_04.txt",
'/nope',
'/root/nope',
'/root/subdirectory_04/nope',
'/root/subdirectory_04/subdirectory_04/nope',
'/root/subdirectory_04/subdirectory_04/subdirectory_04/nope',
'/root/subdirectory_04/subdirectory_04/subdirectory_04/nope/file_04.txt',
]);
}
}

@override
void run() {
var contained = 0;
for (var path in paths) {
if (pathSet.contains(path)) contained++;
}

if (contained != 10000) throw "Wrong result: $contained";
if (contained != 10000) throw 'Wrong result: $contained';
}
}

class PathsBenchmark extends PathSetBenchmark {
PathsBenchmark() : super("toSet()");
PathsBenchmark() : super('toSet()');

@override
void setup() {
walkTree(3, pathSet.add);
}

@override
void run() {
var count = 0;
for (var _ in pathSet.paths) {
count++;
}

if (count != 10000) throw "Wrong result: $count";
if (count != 10000) throw 'Wrong result: $count';
}
}

class RemoveBenchmark extends PathSetBenchmark {
RemoveBenchmark() : super("remove()");
RemoveBenchmark() : super('remove()');

final List<String> paths = [];

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

@override
void run() {
for (var path in paths) {
pathSet.remove(path);
}
}
}

main() {
void main() {
AddBenchmark().report();
ContainsBenchmark().report();
PathsBenchmark().report();
Expand Down
4 changes: 2 additions & 2 deletions example/watch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ library watch;
import 'package:path/path.dart' as p;
import 'package:watcher/watcher.dart';

main(List<String> arguments) {
void main(List<String> arguments) {
if (arguments.length != 1) {
print("Usage: watch <directory path>");
print('Usage: watch <directory path>');
return;
}

Expand Down
13 changes: 13 additions & 0 deletions lib/src/constructable_file_system_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,54 +5,67 @@
import 'dart:io';

abstract class _ConstructableFileSystemEvent implements FileSystemEvent {
@override
final bool isDirectory;
@override
final String path;
@override
int get type;

_ConstructableFileSystemEvent(this.path, this.isDirectory);
}

class ConstructableFileSystemCreateEvent extends _ConstructableFileSystemEvent
implements FileSystemCreateEvent {
@override
final type = FileSystemEvent.create;

ConstructableFileSystemCreateEvent(String path, bool isDirectory)
: super(path, isDirectory);

@override
String toString() => "FileSystemCreateEvent('$path')";
}

class ConstructableFileSystemDeleteEvent extends _ConstructableFileSystemEvent
implements FileSystemDeleteEvent {
@override
final type = FileSystemEvent.delete;

ConstructableFileSystemDeleteEvent(String path, bool isDirectory)
: super(path, isDirectory);

@override
String toString() => "FileSystemDeleteEvent('$path')";
}

class ConstructableFileSystemModifyEvent extends _ConstructableFileSystemEvent
implements FileSystemModifyEvent {
@override
final bool contentChanged;
@override
final type = FileSystemEvent.modify;

ConstructableFileSystemModifyEvent(
String path, bool isDirectory, this.contentChanged)
: super(path, isDirectory);

@override
String toString() =>
"FileSystemModifyEvent('$path', contentChanged=$contentChanged)";
}

class ConstructableFileSystemMoveEvent extends _ConstructableFileSystemEvent
implements FileSystemMoveEvent {
@override
final String destination;
@override
final type = FileSystemEvent.move;

ConstructableFileSystemMoveEvent(
String path, bool isDirectory, this.destination)
: super(path, isDirectory);

@override
String toString() => "FileSystemMoveEvent('$path', '$destination')";
}
2 changes: 1 addition & 1 deletion lib/src/directory_watcher.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import 'directory_watcher/polling.dart';
/// in the directory has changed.
abstract class DirectoryWatcher implements Watcher {
/// The directory whose contents are being monitored.
@Deprecated("Expires in 1.0.0. Use DirectoryWatcher.path instead.")
@Deprecated('Expires in 1.0.0. Use DirectoryWatcher.path instead.')
String get directory;

/// Creates a new [DirectoryWatcher] monitoring [directory].
Expand Down
21 changes: 14 additions & 7 deletions lib/src/directory_watcher/linux.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import '../watch_event.dart';
/// (issue 14424).
class LinuxDirectoryWatcher extends ResubscribableWatcher
implements DirectoryWatcher {
@override
String get directory => path;

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

class _LinuxDirectoryWatcher
implements DirectoryWatcher, ManuallyClosedWatcher {
@override
String get directory => _files.root;
@override
String get path => _files.root;

@override
Stream<WatchEvent> get events => _eventsController.stream;
final _eventsController = StreamController<WatchEvent>.broadcast();

@override
bool get isReady => _readyCompleter.isCompleted;

@override
Future get ready => _readyCompleter.future;
final _readyCompleter = Completer();

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

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

_LinuxDirectoryWatcher(String path) : _files = PathSet(path) {
_nativeEvents.add(Directory(path)
Expand Down Expand Up @@ -93,6 +99,7 @@ class _LinuxDirectoryWatcher
}, cancelOnError: true);
}

@override
void close() {
for (var subscription in _subscriptions) {
subscription.cancel();
Expand Down Expand Up @@ -128,9 +135,9 @@ class _LinuxDirectoryWatcher

/// The callback that's run when a batch of changes comes in.
void _onBatch(List<FileSystemEvent> batch) {
var files = Set<String>();
var dirs = Set<String>();
var changed = Set<String>();
var files = <String>{};
var dirs = <String>{};
var changed = <String>{};

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

/// Like [Stream.listen], but automatically adds the subscription to
/// [_subscriptions] so that it can be canceled when [close] is called.
void _listen<T>(Stream<T> stream, void onData(T event),
{Function onError, void onDone(), bool cancelOnError}) {
void _listen<T>(Stream<T> stream, void Function(T) onData,
{Function onError, void Function() onDone, bool cancelOnError}) {
StreamSubscription subscription;
subscription = stream.listen(onData, onError: onError, onDone: () {
_subscriptions.remove(subscription);
Expand Down
21 changes: 14 additions & 7 deletions lib/src/directory_watcher/mac_os.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import '../watch_event.dart';
/// [Directory.watch].
class MacOSDirectoryWatcher extends ResubscribableWatcher
implements DirectoryWatcher {
@override
String get directory => path;

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

class _MacOSDirectoryWatcher
implements DirectoryWatcher, ManuallyClosedWatcher {
@override
String get directory => path;
@override
final String path;

@override
Stream<WatchEvent> get events => _eventsController.stream;
final _eventsController = StreamController<WatchEvent>.broadcast();

@override
bool get isReady => _readyCompleter.isCompleted;

@override
Future get ready => _readyCompleter.future;
final _readyCompleter = Completer();

Expand All @@ -64,7 +70,7 @@ class _MacOSDirectoryWatcher

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

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

@override
void close() {
if (_watchSubscription != null) _watchSubscription.cancel();
if (_initialListSubscription != null) _initialListSubscription.cancel();
Expand Down Expand Up @@ -181,19 +188,19 @@ class _MacOSDirectoryWatcher
// directory's full contents will be examined anyway, so we ignore such
// events. Emitting them could cause useless or out-of-order events.
var directories = unionAll(batch.map((event) {
if (!event.isDirectory) return Set<String>();
if (!event.isDirectory) return <String>{};
if (event is FileSystemMoveEvent) {
return Set<String>.from([event.path, event.destination]);
return {event.path, event.destination};
}
return Set<String>.from([event.path]);
return {event.path};
}));

isInModifiedDirectory(String path) =>
bool isInModifiedDirectory(String path) =>
directories.any((dir) => path != dir && path.startsWith(dir));

addEvent(String path, FileSystemEvent event) {
void addEvent(String path, FileSystemEvent event) {
if (isInModifiedDirectory(path)) return;
eventsForPaths.putIfAbsent(path, () => Set<FileSystemEvent>()).add(event);
eventsForPaths.putIfAbsent(path, () => <FileSystemEvent>{}).add(event);
}

for (var event in batch) {
Expand Down
Loading

0 comments on commit e3a922d

Please sign in to comment.