Skip to content

Commit

Permalink
Fix directives_ordering for a leading dot in path (dart-lang/linter#4538
Browse files Browse the repository at this point in the history
)

It used to sort imports as follows:

    import './foo1.dart';
    import '../../foo2.dart';
    import '../foo3.dart';
    import 'foo4.dart';

This change corrects it to the following:

    import '../../foo1.dart';
    import '../foo2.dart';
    import './foo3.dart';
    import 'foo4.dart';
  • Loading branch information
oprypin authored Jul 12, 2023
1 parent 4f6cede commit 246eabd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/src/rules/directives_ordering.dart
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ const _importKeyword = 'import';
///
/// Package is everything until the first `/`.
int compareDirectives(String a, String b) {
if (!a.startsWith('package:') || !b.startsWith('package:')) {
return a.compareTo(b);
}
var indexA = a.indexOf('/');
var indexB = b.indexOf('/');
if (indexA == -1 || indexB == -1) return a.compareTo(b);
Expand Down
20 changes: 20 additions & 0 deletions test/rules/directives_ordering_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -274,4 +274,24 @@ import 'a.dart';
lint(102, 16),
]);
}

test_sortDirectiveSectionsAlphabetically_dotInRelativePath_import() async {
await assertDiagnostics(r'''
import './foo1.dart';
import '../../foo2.dart';
import '../foo3.dart';
import 'foo4.dart';
// ignore_for_file: unused_import, uri_does_not_exist
''', [lint(22, 25)]);
}

test_sortDirectiveSectionsAlphabetically_dotInRelativePath_import_ok() async {
await assertNoDiagnostics(r'''
import '../../foo4.dart';
import '../foo3.dart';
import './foo2.dart';
import 'foo1.dart';
// ignore_for_file: unused_import, uri_does_not_exist
''');
}
}

0 comments on commit 246eabd

Please sign in to comment.