Skip to content

Commit 2bc06aa

Browse files
committed
Updates to Dart 3 and fixes some code crimes
Signed-off-by: Hans Kokx <[email protected]>
1 parent 5062338 commit 2bc06aa

14 files changed

+130
-89
lines changed

analysis_options.yaml

+62-28
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,64 @@
1-
# This file configures the static analysis results for your project (errors,
2-
# warnings, and lints).
3-
#
4-
# This enables the 'recommended' set of lints from `package:lints`.
5-
# This set helps identify many issues that may lead to problems when running
6-
# or consuming Dart code, and enforces writing Dart using a single, idiomatic
7-
# style and format.
8-
#
9-
# If you want a smaller set of lints you can change this to specify
10-
# 'package:lints/core.yaml'. These are just the most critical lints
11-
# (the recommended set includes the core lints).
12-
# The core lints are also what is used by pub.dev for scoring packages.
1+
include: package:flutter_lints/flutter.yaml
132

14-
include: package:lints/recommended.yaml
3+
analyzer:
4+
errors:
5+
# treat missing required parameters as an error (not a hint)
6+
missing_required_param: error
7+
# treat missing returns as an error (not a hint)
8+
missing_return: error
9+
invalid_annotation_target: ignore
10+
exclude:
11+
- lib/**/*.g.dart
12+
- lib/*.g.dart
13+
- lib/**/*.graphql.dart
14+
- lib/**/*.freezed.dart
15+
- test/**/*.mocks.dart
16+
- bricks/**
17+
language:
18+
strict-casts: true
19+
# strict-raw-types: true # I want this one enabled, and we should work toward that. @hans
1520

16-
# Uncomment the following section to specify additional rules.
17-
18-
# linter:
19-
# rules:
20-
# - camel_case_types
21-
22-
# analyzer:
23-
# exclude:
24-
# - path/to/excluded/files/**
25-
26-
# For more information about the core and recommended set of lints, see
27-
# https://dart.dev/go/core-lints
28-
29-
# For additional information about configuring this file, see
30-
# https://dart.dev/guides/language/analysis-options
21+
linter:
22+
rules:
23+
avoid_dynamic_calls: true
24+
prefer_const_constructors: true
25+
prefer_const_declarations: true
26+
use_key_in_widget_constructors: true
27+
sort_child_properties_last: true
28+
prefer_final_in_for_each: true
29+
prefer_final_locals: true
30+
avoid_void_async: true
31+
unnecessary_parenthesis: true
32+
always_declare_return_types: true
33+
unawaited_futures: true
34+
only_throw_errors: true
35+
missing_whitespace_between_adjacent_strings: true
36+
sized_box_for_whitespace: true
37+
prefer_const_literals_to_create_immutables: true
38+
prefer_const_constructors_in_immutables: true
39+
no_logic_in_create_state: true
40+
avoid_unnecessary_containers: true
41+
avoid_print: true
42+
avoid_relative_lib_imports: true
43+
empty_statements: true
44+
no_adjacent_strings_in_list: true
45+
no_duplicate_case_values: true
46+
valid_regexps: true
47+
require_trailing_commas: true
48+
unnecessary_statements: true
49+
use_build_context_synchronously: true
50+
annotate_overrides: true
51+
avoid_function_literals_in_foreach_calls: true
52+
avoid_null_checks_in_equality_operators: true
53+
avoid_unused_constructor_parameters: true
54+
camel_case_extensions: true
55+
camel_case_types: true
56+
directives_ordering: true
57+
empty_constructor_bodies: true
58+
file_names: true
59+
prefer_final_fields: true
60+
prefer_is_empty: true
61+
prefer_is_not_empty: true
62+
prefer_is_not_operator: true
63+
prefer_iterable_whereType: true
64+
sort_pub_dependencies: true

bin/dexa.dart

+22-17
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import 'dart:async';
12
import 'dart:io';
2-
import 'package:path/path.dart';
3-
import 'package:intl/intl.dart';
43
import 'dart:math';
5-
import 'package:mime/mime.dart';
6-
import 'dart:async';
4+
75
import 'package:args/args.dart';
6+
import 'package:intl/intl.dart';
7+
import 'package:mime/mime.dart';
8+
import 'package:path/path.dart';
89

910
part 'constants/ansi.dart';
1011
part 'constants/file_sizes.dart';
@@ -36,10 +37,10 @@ void main(List<String> arguments) async {
3637
// parser.addFlag("recursive", negatable: false, abbr: 'R');
3738
parser.addFlag("icons", negatable: false, abbr: 'I');
3839

39-
ArgResults argResults = parser.parse(arguments);
40+
final ArgResults argResults = parser.parse(arguments);
4041

4142
// Read parser flags
42-
Map<String, bool> args = {
43+
final Map<String, bool> args = {
4344
'longFileListing': (argResults["long"] == true) ? true : false,
4445
'humanReadableFileSize':
4546
(argResults["human-readable"] == true) ? true : false,
@@ -53,10 +54,10 @@ void main(List<String> arguments) async {
5354

5455
// List all files and directories in the given path, then sort with dirctories on top
5556
directory = await getPath(argResults);
56-
List<FileSystemEntity> fileList = [];
57-
List<FileSystemEntity> files =
57+
final List<FileSystemEntity> fileList = [];
58+
final List<FileSystemEntity> files =
5859
await listDirectoryContents(directory, type: FileSystemEntityType.file);
59-
List<FileSystemEntity> directories = await listDirectoryContents(
60+
final List<FileSystemEntity> directories = await listDirectoryContents(
6061
directory,
6162
type: FileSystemEntityType.directory,
6263
);
@@ -71,30 +72,34 @@ void main(List<String> arguments) async {
7172
displayHeaders(args: args, fileSizeDigits: maxFileSizeLengthInDigits);
7273
}
7374

74-
for (FileSystemEntity element in fileList) {
75+
for (final FileSystemEntity element in fileList) {
7576
String output = '';
7677

77-
String currentFile = element.uri.toFilePath(windows: Platform.isWindows);
78+
final String currentFile =
79+
element.uri.toFilePath(windows: Platform.isWindows);
7880

7981
try {
80-
FileStat fileStat = await FileStat.stat(currentFile);
82+
final FileStat fileStat = await FileStat.stat(currentFile);
8183

8284
if (args['longFileListing']!) {
8385
if (args['showHeaders']!) {
8486
output += fileType(fileStat);
8587
output += filePermissions(fileStat);
8688

87-
output += fileSize(fileStat,
88-
fileSizeDigits: maxFileSizeLengthInDigits ?? 0, args: args);
89+
output += fileSize(
90+
fileStat,
91+
fileSizeDigits: maxFileSizeLengthInDigits ?? 0,
92+
args: args,
93+
);
8994

9095
output += fileOwner(fileStat);
9196
output += fileModificationDate(fileStat);
9297
}
9398
}
9499

95100
if (args['showFileTypeIcon']!) {
96-
String fileToProcess = directory.path + currentFile;
97-
FileSystemEntityType type = fileStat.type;
101+
final String fileToProcess = directory.path + currentFile;
102+
final FileSystemEntityType type = fileStat.type;
98103
output +=
99104
showFileIcon(fileToProcess, type, headers: args['showHeaders']!);
100105
}
@@ -111,7 +116,7 @@ void main(List<String> arguments) async {
111116
stderr.write('$e');
112117
exit(2);
113118
} catch (_) {
114-
handleError(currentFile);
119+
await handleError(currentFile);
115120
}
116121
}
117122
}

bin/features/file_icon.dart

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ String showFileIcon(String file, FileSystemEntityType type, {bool? headers}) {
1818
if (mimeType != null) {
1919
// We have a mime type, so let's check if it's in the list of known file types.
2020
mimeType = mimeType.split('/')[1];
21-
Map? data = iconSet[mimeType];
21+
final Map? data = iconSet[mimeType];
2222

2323
// The file type has been found in the list of known file types.
2424
if (data != null) {
25-
icon = data['icon'];
26-
color = data['color'];
25+
icon = data['icon'] as String?;
26+
color = data['color'] as String?;
2727
}
2828
}
2929
break;

bin/features/file_owner.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ part of '../dexa.dart';
22

33
String fileOwner(FileStat fileStat) {
44
// TODO: This isn't actually possible at this time. https://github.com/dart-lang/sdk/issues/47478
5-
String output = "";
5+
const String output = "";
66
if (stdout.supportsAnsiEscapes) {
77
return output.color(AnsiColors.yellow).bold();
88
}

bin/features/file_size.dart

+10-10
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,34 @@ String fileSize(
99
if (args['humanReadableFileSize']!) {
1010
output = fileSizeHumanReadable(fileStat);
1111
} else {
12-
String nhrfs = nonHumanReadableFileSize(fileStat);
12+
final String nhrfs = nonHumanReadableFileSize(fileStat);
1313
int digitsToSubtract = 0;
1414
digitsToSubtract = nhrfs.split(RegExp(r'\d')).length - 7;
1515
if (nhrfs[0] == '-') {
1616
digitsToSubtract = 1;
1717
}
1818
output = " " * (fileSizeDigits - digitsToSubtract);
19-
output += ' ' + nhrfs;
19+
output += ' $nhrfs';
2020
}
2121
return output;
2222
}
2323

2424
String fileSizeHumanReadable(FileStat fileStat) {
25-
String? fileSizeString = _fileSizeString(fileStat.size);
25+
final String fileSizeString = _fileSizeString(fileStat.size);
2626

2727
if (fileStat.size == 0 || fileStat.type == FileSystemEntityType.directory) {
28-
return "-".dim() + " ";
28+
return "${"-".dim()} ";
2929
}
3030

3131
num size = fileStat.size;
32-
int nextIndex =
32+
final int nextIndex =
3333
fileSizes.entries.firstWhere((element) => element.key > size).key;
34-
int sizeDivisor =
34+
final int sizeDivisor =
3535
fileSizes.entries.where((element) => element.key < nextIndex).last.key;
3636

3737
size = (size / sizeDivisor).round();
3838

39-
String output = (size.toString() + fileSizeString) + " ";
39+
String output = "$size$fileSizeString ";
4040

4141
if (stdout.supportsAnsiEscapes) {
4242
output = output.color(AnsiColors.green).bold();
@@ -46,10 +46,10 @@ String fileSizeHumanReadable(FileStat fileStat) {
4646

4747
String nonHumanReadableFileSize(FileStat fileStat) {
4848
if (fileStat.size == 0) {
49-
return "-" + " ".dim();
49+
return "-${" ".dim()}";
5050
}
5151

52-
String output = fileStat.size.toString() + " ";
52+
String output = "${fileStat.size} ";
5353

5454
if (stdout.supportsAnsiEscapes) {
5555
output = output.color(AnsiColors.green).bold();
@@ -58,7 +58,7 @@ String nonHumanReadableFileSize(FileStat fileStat) {
5858
}
5959

6060
String _fileSizeString(int size) {
61-
String? fileSizeString = fileSizes.entries
61+
final String fileSizeString = fileSizes.entries
6262
.firstWhere((element) => element.key >= size)
6363
.value
6464
.toString();

bin/features/file_type.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ String fileType(FileStat fileStat) {
1414
type = type.bold();
1515
}
1616

17-
return (type);
17+
return type;
1818
}

bin/features/headers.dart

+5-5
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ void displayHeaders({
1111
padding = ' ';
1212
}
1313

14-
header += "Permissions".underline() + " ";
15-
header += "Size".underline() + (" " * (fileSizeDigits - 5)) + " " + padding;
14+
header += "${"Permissions".underline()} ";
15+
header += "${"Size".underline()}${" " * (fileSizeDigits - 5)} $padding";
1616
// header += "User".underline() + " ";
17-
header += "Date Modified".underline() + " " + padding;
17+
header += "${"Date Modified".underline()} $padding";
1818
if (args['showFileTypeIcon']!) {
19-
header += " " + "Icon".underline() + padding;
19+
header += " ${"Icon".underline()}$padding";
2020
}
21-
header += "Name".underline() + " " + padding;
21+
header += "${"Name".underline()} $padding";
2222

2323
header += "\n";
2424
stdout.write(header);

bin/features/modification_date.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
part of '../dexa.dart';
22

33
String fileModificationDate(FileStat fileStat) {
4-
DateTime modified = fileStat.modified;
4+
final DateTime modified = fileStat.modified;
55
final DateFormat formatter = DateFormat('yyyy-MM-dd HH:mm');
66
String output = formatter.format(modified).padRight(17, " ");
77
if (stdout.supportsAnsiEscapes) {

bin/features/permissions.dart

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ part of '../dexa.dart';
33
String filePermissions(FileStat fileStat) {
44
String output = '';
55
if (stdout.supportsAnsiEscapes) {
6-
List<String> permissions = fileStat.modeString().split("");
6+
final List<String> permissions = fileStat.modeString().split("");
77

8-
for (String i in permissions) {
8+
for (final String i in permissions) {
99
switch (i) {
1010
case "r":
1111
output += "r".color(AnsiColors.yellow);
@@ -21,7 +21,7 @@ String filePermissions(FileStat fileStat) {
2121
break;
2222
}
2323
}
24-
return output.bold() + ' ';
24+
return '${output.bold()} ';
2525
}
2626
return fileStat.modeString();
2727
}

bin/functions/gather_file_sizes.dart

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
part of '../dexa.dart';
22

33
Future<int> gatherDigitsOfMaxFileSize(List<FileSystemEntity> files) async {
4-
List<int> fileSizes = [0];
5-
for (FileSystemEntity entity in files) {
6-
String file = entity.uri.toFilePath(windows: Platform.isWindows);
7-
FileStat fileStat = await FileStat.stat(file);
8-
int size = fileStat.size;
4+
final List<int> fileSizes = [0];
5+
for (final FileSystemEntity entity in files) {
6+
final String file = entity.uri.toFilePath(windows: Platform.isWindows);
7+
final FileStat fileStat = await FileStat.stat(file);
8+
final int size = fileStat.size;
99
fileSizes.add(size);
1010
}
11-
int maxFileSize = fileSizes.reduce(max);
12-
int digits = maxFileSize.toString().length;
11+
final int maxFileSize = fileSizes.reduce(max);
12+
final int digits = maxFileSize.toString().length;
1313
return digits;
1414
}

bin/functions/get_path.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
part of '../dexa.dart';
22

33
Future<Directory> getPath(argResults) async {
4-
List<String?> path = argResults.rest;
4+
final List<String?> path = argResults.rest as List<String?>;
55
if (path.isEmpty) return Directory.current;
66

77
return Directory.fromUri(Uri.directory(path.first!));

bin/functions/handle_error.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
part of '../dexa.dart';
22

3-
void handleError(error) async {
4-
stderr.writeln(error.toString() + "\n");
3+
Future<void> handleError(error) async {
4+
stderr.writeln("$error\n");
55
exitCode = 2;
66
exit(exitCode);
77
}

bin/functions/list_directory_contents.dart

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
part of '../dexa.dart';
22

3-
Future<List<FileSystemEntity>> listDirectoryContents(Directory dir,
4-
{required FileSystemEntityType type}) {
5-
List<FileSystemEntity> files = <FileSystemEntity>[];
6-
Completer<List<FileSystemEntity>> completer =
3+
Future<List<FileSystemEntity>> listDirectoryContents(
4+
Directory dir, {
5+
required FileSystemEntityType type,
6+
}) {
7+
final List<FileSystemEntity> files = <FileSystemEntity>[];
8+
final Completer<List<FileSystemEntity>> completer =
79
Completer<List<FileSystemEntity>>();
8-
Stream<FileSystemEntity> lister = dir.list(recursive: false);
10+
final Stream<FileSystemEntity> lister = dir.list(recursive: false);
911

1012
lister.listen(
1113
(file) async {
12-
FileStat currentFileStat = await FileStat.stat(file.path);
14+
final FileStat currentFileStat = await FileStat.stat(file.path);
1315
if (currentFileStat.type == type) {
1416
files.add(file);
1517
}

0 commit comments

Comments
 (0)