Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️ Add null safety support #49

Merged
merged 3 commits into from
Mar 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions bin/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'dart:io';

// 📦 Package imports:
import 'package:args/args.dart';
import 'package:colorize/colorize.dart';
import 'package:tint/tint.dart';
import 'package:yaml/yaml.dart';

// 🌎 Project imports:
Expand All @@ -12,7 +12,7 @@ import 'package:import_sorter/files.dart' as files;
import 'package:import_sorter/sort.dart' as sort;

void main(List<String> args) {
// Setting arguments
// Parsing arguments
final parser = ArgParser();
parser.addFlag('emojis', abbr: 'e', negatable: false);
parser.addFlag('ignore-config', negatable: false);
Expand Down Expand Up @@ -81,26 +81,27 @@ void main(List<String> args) {

// Sorting and writing to files
var filesFormatted = 0;
var totalImportsSorted = 0;
int totalImportsSorted = 0;

for (final filePath in dartFiles.keys) {
final sortedFile = sort.sortImports(dartFiles[filePath], packageName,
final file = dartFiles[filePath];
if (file == null) {
continue;
}

final sortedFile = sort.sortImports(file.readAsLinesSync(), packageName,
dependencies, emojis, exitOnChange, noComments);
final importsSorted = sortedFile[1];
final importsSorted = sortedFile.importsChanged;

filesFormatted++;
totalImportsSorted += importsSorted;

File(filePath).writeAsStringSync(sortedFile[0]);
final dirChunks = filePath.replaceAll("$currentPath/", '').split('/');
dartFiles[filePath]?.writeAsStringSync(sortedFile.sortedFile);
stdout.write(
'${filesFormatted == 1 ? '\n' : ''}┃ ${filesFormatted == dartFiles.keys.length ? '┗' : '┣'}━━ ✅ Sorted ${sortedFile[1]} out of ${sortedFile[2]} imports in ${dirChunks.getRange(0, dirChunks.length - 1).join('/')}/');
color(
dirChunks.last,
back: Styles.BOLD,
front: importsSorted == 0 ? Styles.YELLOW : Styles.GREEN,
isBold: true,
);
'${filesFormatted == 1 ? '\n' : ''}┃ ${filesFormatted == dartFiles.keys.length ? '┗' : '┣'}━━ ✅ Sorted ${sortedFile.importsChanged} out of ${sortedFile.numberOfImports} imports in ${file.path.replaceFirst(currentPath, '')}/');
String filename = file.path.split(Platform.pathSeparator).last;
filename = importsSorted == 0 ? filename.yellow() : filename.green();
stdout.write(filename + "\n");
}
stopwatch.stop();
stdout.write(
Expand Down
19 changes: 10 additions & 9 deletions lib/files.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import 'dart:io';

/// Get all the dart files for the project and the contents
Map<String, List<String>> dartFiles(String currentPath, List<String> args) {
final dartFiles = <String, List<String>>{};
Map<String, File> dartFiles(String currentPath, List<String> args) {
final dartFiles = <String, File>{};
final allContents = [
..._readDir(currentPath, 'lib'),
..._readDir(currentPath, 'bin'),
Expand All @@ -14,19 +14,21 @@ Map<String, List<String>> dartFiles(String currentPath, List<String> args) {

for (final fileOrDir in allContents) {
if (fileOrDir is File && fileOrDir.path.endsWith('.dart')) {
dartFiles[fileOrDir.path] = fileOrDir.readAsLinesSync();
dartFiles[fileOrDir.path] = fileOrDir;
}
}

// If there are only certain files given via args filter the others out
var onlyCertainFiles = false;
for (final arg in args) {
onlyCertainFiles = arg.endsWith("dart");
if (!onlyCertainFiles) {
onlyCertainFiles = arg.endsWith("dart");
}
}

if (onlyCertainFiles) {
final patterns = args.where((arg) => !arg.startsWith("-"));
final filesToRemove = [];
final filesToKeep = <String, File>{};

for (final fileName in dartFiles.keys) {
var keep = false;
Expand All @@ -36,12 +38,11 @@ Map<String, List<String>> dartFiles(String currentPath, List<String> args) {
break;
}
}
if (!keep) {
filesToRemove.add(fileName);
if (keep) {
filesToKeep[fileName] = File(fileName);
}
}

filesToRemove.forEach((file) => dartFiles.remove(file));
return filesToKeep;
}

return dartFiles;
Expand Down
37 changes: 19 additions & 18 deletions lib/sort.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
import 'dart:io';

// 📦 Package imports:
import 'package:colorize/colorize.dart';
import 'package:tint/tint.dart';

/// Sort the imports
/// Returns the sorted file as a string at
/// index 0 and the number of sorted imports
/// at index 1
List sortImports(
ImportSortData sortImports(
List<String> lines,
String package_name,
List dependencies,
Expand Down Expand Up @@ -88,18 +88,14 @@ List sortImports(
}
}

// If no import return original string of lines
// If no imports return original string of lines
if (noImports()) {
if (lines.length > 1) {
if (lines.last != '') {
return [
[...lines, ''].join('\n'),
0,
0
];
return ImportSortData([...lines, ''].join('\n'), 0, 0);
}
}
return [lines.join('\n'), 0, 0];
return ImportSortData(lines.join('\n'), 0, 0);
}

// Remove spaces
Expand Down Expand Up @@ -169,26 +165,31 @@ List sortImports(
projectImports.length;
if (exitIfChanged && original != sortedFile) {
stdout.write('\n┗━━🚨 ');
color(
'Please run import sorter!',
back: Styles.BOLD,
front: Styles.RED,
isBold: true,
);
stdout.write('Please run import sorter!'.bold().red());
exit(1);
}
if (original == sortedFile) {
return [original, 0, numberOfImports];
return ImportSortData(original, 0, numberOfImports);
}

return [
return ImportSortData(
sortedFile,
numberOfImports,
numberOfImports,
];
);
}

/// Get the number of times a string contains another
/// string
int _timesContained(String string, String looking) =>
string.split(looking).length - 1;

/// Data to return from a sort
class ImportSortData {
final String sortedFile;
final int importsChanged;
final int numberOfImports;

const ImportSortData(
this.sortedFile, this.importsChanged, this.numberOfImports);
}
6 changes: 3 additions & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ authors:
- Flutter Community <community@flutter.zone>

environment:
sdk: '>=2.7.0 <3.0.0'
sdk: '>=2.12.0 <3.0.0'

dependencies:
args: ^2.0.0
colorize: ^2.0.0
tint: ^2.0.0
yaml: ^3.1.0

dev_dependencies:
test: ^1.15.5
test: ^1.16.7

import_sorter:
emojis: true
18 changes: 9 additions & 9 deletions test/sort_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void main(List<String> args) async {
emojis,
false,
noComments,
)[0],
).sortedFile,
'',
);
},
Expand All @@ -90,7 +90,7 @@ void main(List<String> args) async {
noComments,
);
expect(
sortedImports[0],
sortedImports.sortedFile,
'$dartImportComment$dartImports\n$flutterImportComment$flutterImports\n$packageImportComment$packageImports\n$projectImportComment$projectImports\n',
);
},
Expand All @@ -106,7 +106,7 @@ void main(List<String> args) async {
false,
noComments,
);
expect('${sortedImports[0]}\n', '$sampleProgram\n');
expect('${sortedImports.sortedFile}\n', '$sampleProgram\n');
},
);
test(
Expand All @@ -121,7 +121,7 @@ void main(List<String> args) async {
noComments,
);
expect(
sortedImports[0],
sortedImports.sortedFile,
'$dartImportComment$dartImports\n$sampleProgram\n',
);
},
Expand All @@ -138,7 +138,7 @@ void main(List<String> args) async {
noComments,
);
expect(
sortedImports[0],
sortedImports.sortedFile,
'$flutterImportComment$flutterImports\n$sampleProgram\n',
);
},
Expand All @@ -155,7 +155,7 @@ void main(List<String> args) async {
noComments,
);
expect(
sortedImports[0],
sortedImports.sortedFile,
'$packageImportComment$packageImports\n$sampleProgram\n',
);
},
Expand All @@ -172,7 +172,7 @@ void main(List<String> args) async {
noComments,
);
expect(
sortedImports[0],
sortedImports.sortedFile,
'$projectImportComment$projectImports\n$sampleProgram\n',
);
},
Expand All @@ -190,7 +190,7 @@ void main(List<String> args) async {
noComments,
);
expect(
sortedImports[0],
sortedImports.sortedFile,
'$dartImportComment$dartImports\n$flutterImportComment$flutterImports\n$packageImportComment$packageImports\n$projectImportComment$projectImports\n$sampleProgram\n',
);
},
Expand All @@ -208,7 +208,7 @@ void main(List<String> args) async {
noComments,
);
expect(
sortedImports[0],
sortedImports.sortedFile,
'library import_sorter;\n\n$dartImportComment$dartImports\n$flutterImportComment$flutterImports\n$packageImportComment$packageImports\n$projectImportComment$projectImports\n$sampleProgram\n',
);
},
Expand Down