Skip to content

Commit d293cf8

Browse files
committed
⚡️ Resolve directives_ordering
1 parent 03b669b commit d293cf8

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

lib/src/utils/convert.dart

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ void writeImports(Set<String> imports, StringBuffer sb) {
4343
final List<String> otherImports = <String>[];
4444
final Set<String> distinctImports = imports.map((e) => e.trim()).toSet();
4545

46-
for (final String import in distinctImports) {
46+
for (final import in distinctImports) {
47+
if (import.isEmpty) {
48+
continue;
49+
}
4750
if (import.isDartImport) {
4851
dartImports.add(import);
4952
} else if (import.isPackageImport) {
@@ -55,12 +58,25 @@ void writeImports(Set<String> imports, StringBuffer sb) {
5558

5659
dartImports.sort((String a, String b) => a.compareTo(b));
5760
packageImports.sort((String a, String b) => a.compareTo(b));
58-
otherImports.sort((String a, String b) => a.compareTo(b));
61+
otherImports.sort((String a, String b) {
62+
if (a.startsWith("import '/") && b.startsWith("import '/")) {
63+
return a.compareTo(b);
64+
}
65+
if (b.startsWith("import '/")) {
66+
return 1;
67+
}
68+
if (a.startsWith("import '/")) {
69+
return -1;
70+
}
71+
return a.compareTo(b);
72+
});
73+
5974
final String output = <String>[
6075
dartImports.join('\n'),
6176
packageImports.join('\n'),
6277
otherImports.join('\n'),
6378
].join('\n\n').trim();
79+
6480
sb.write(output);
6581
}
6682

pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ dependencies:
3232
dev_dependencies:
3333
melos: ^6.0.0
3434
lints: any
35+
test: ^1.16.0
3536

3637
# Run `dart pub global activate --source path ./` for local development.
3738
executables:

test/utils_test.dart

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import 'package:ff_annotation_route/src/utils/convert.dart';
2+
import 'package:test/test.dart';
3+
4+
void main() {
5+
test('writeImports', () {
6+
final buffer = StringBuffer();
7+
writeImports(
8+
<String>{
9+
"import '/test_b.dart",
10+
"import '../test_c.dart",
11+
"import '/test_a.dart",
12+
"import './test_e.dart",
13+
"import 'test_d.dart",
14+
},
15+
buffer,
16+
);
17+
expect(
18+
buffer.toString(),
19+
[
20+
"import '/test_a.dart",
21+
"import '/test_b.dart",
22+
"import '../test_c.dart",
23+
"import './test_e.dart",
24+
"import 'test_d.dart",
25+
].join('\n'),
26+
);
27+
});
28+
}

0 commit comments

Comments
 (0)