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

Convert project imports to relative imports #62

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions bin/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ void main(List<String> args) {
if (file == null) {
continue;
}

final String libFolder = file.path.substring(file.path.indexOf('lib') + 4);
final sortedFile = sort.sortImports(
file.readAsLinesSync(), packageName, emojis, exitOnChange, noComments);
file.readAsLinesSync(), packageName, emojis, exitOnChange, noComments,filePath: libFolder);
if (!sortedFile.updated) {
continue;
}
Expand Down
35 changes: 34 additions & 1 deletion lib/sort.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,32 @@
// 🎯 Dart imports:
import 'dart:io';

// Sorts the project imports to relative imports
// Returns relative import line String
String convertToRelativeImport(String line,String packageName,String path) {
List<String> fileBits = path.split(RegExp(r'[/\\]'));
List<String> importBits = line.split('/');
importBits.removeAt(0);
dynamic doubleDotAmount = 0;
int index;
for(index=0;index<fileBits.length-1;index++) {
if (fileBits[index] == importBits[index]) {
continue;
}
doubleDotAmount = fileBits.length - index -1;
break;
}
List<String> array = List.filled(doubleDotAmount, "..", growable: true);
for(int i =0;i < array.length;i++){
array.insertAll(array.length, importBits.sublist(index));
break;
}
if(doubleDotAmount == 0 ) {
return "import '${importBits.sublist(index).join('/')}";
}
return " import '${array.join('/')}";
}

/// Sort the imports
/// Returns the sorted file as a string at
/// index 0 and the number of sorted imports
Expand Down Expand Up @@ -56,8 +82,15 @@ ImportSortData sortImports(
} else if (lines[i].contains('package:flutter/')) {
flutterImports.add(lines[i]);
} else if (lines[i].contains('package:$package_name/')) {
projectImports.add(lines[i]);
if(filePath != null) {
String import = convertToRelativeImport(lines[i], package_name,filePath);
projectImports.add(import);
}
else {
projectImports.add(lines[i]);
}
} else if (lines[i].contains('package:')) {

packageImports.add(lines[i]);
} else {
projectRelativeImports.add(lines[i]);
Expand Down
40 changes: 40 additions & 0 deletions test/sort_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,46 @@ void main(List<String> args) async {
);
},
);
test(
'Check if convertToRelativeImport returns relative import if importline in file is in same path',
() {
expect(
convertToRelativeImport(
'import \'package:test/widgets/dropdown.dart\'',
'test',
'widgets/label_dropdown.dart'
),
'import \'dropdown.dart\''
);
}
);
test(
'Check if convertToRelativeImport returns relative path if imported file is in parents sibling folder',
() {
expect(
convertToRelativeImport(
"import 'package:test/widgets/dropdown.dart';",
'test',
'screens/screen_one.dart'
),
" import '../widgets/dropdown.dart';"
);
}
);
test(
'Check if convertToRelativeImport returns relative import if imported file in file is in grandparent folder',
() {
expect(
convertToRelativeImport(
'import \'package:test/main.dart\';',
'test',
'widgets/config/config.dart'
),
' import \'../../main.dart\';'
);
}
);

}

void main() {
Expand Down