diff --git a/pkg/analysis_server/lib/src/services/correction/organize_imports.dart b/pkg/analysis_server/lib/src/services/correction/organize_imports.dart index a735f2cf723a..c89559fd2664 100644 --- a/pkg/analysis_server/lib/src/services/correction/organize_imports.dart +++ b/pkg/analysis_server/lib/src/services/correction/organize_imports.dart @@ -371,7 +371,9 @@ class _DirectiveInfo implements Comparable<_DirectiveInfo> { /// Consider finding a way to share this code! static int _compareUri(String a, String b) { if (!a.startsWith('package:') || !b.startsWith('package:')) { - return a.compareTo(b); + if (!a.startsWith('/') && !b.startsWith('/')) { + return a.compareTo(b); + } } var indexA = a.indexOf('/'); var indexB = b.indexOf('/'); diff --git a/pkg/linter/lib/src/rules/directives_ordering.dart b/pkg/linter/lib/src/rules/directives_ordering.dart index 0208dd6ce3d5..7ed9cbfc52f1 100644 --- a/pkg/linter/lib/src/rules/directives_ordering.dart +++ b/pkg/linter/lib/src/rules/directives_ordering.dart @@ -117,7 +117,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); + if (!a.startsWith('/') && !b.startsWith('/')) { + return a.compareTo(b); + } } var indexA = a.indexOf('/'); var indexB = b.indexOf('/'); diff --git a/pkg/linter/test/directives_ordering_test.dart b/pkg/linter/test/directives_ordering_test.dart new file mode 100644 index 000000000000..22ffa28809ff --- /dev/null +++ b/pkg/linter/test/directives_ordering_test.dart @@ -0,0 +1,56 @@ +// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import 'package:linter/src/rules/directives_ordering.dart'; +import 'package:test/test.dart'; + +void main() { + group(compareDirectives, () { + void checkImportGroup(List correctlyOrderedImports) { + for (int i = 0; i < correctlyOrderedImports.length; i++) { + var a = correctlyOrderedImports[i]; + expect(compareDirectives(a, a), 0, + reason: '"$a" sorts the same as itself'); + + for (int j = i + 1; j < correctlyOrderedImports.length; j++) { + var b = correctlyOrderedImports[j]; + expect(compareDirectives(a, b), lessThan(0), + reason: '"$a" sorts before "$b"'); + expect(compareDirectives(b, a), greaterThan(0), + reason: '"$b" sorts after "$a"'); + } + } + } + + test('dart: imports', () { + checkImportGroup(const [ + 'dart:aaa', + 'dart:bbb', + ]); + }); + + test('package: imports', () { + checkImportGroup(const [ + 'package:aa/bb.dart', + 'package:aaa/aaa.dart', + 'package:aaa/ccc.dart', + 'package:bbb/bbb.dart', + ]); + }); + + test('relative imports', () { + checkImportGroup(const [ + '/foo5.dart', + '../../foo4.dart', + '../foo2/a.dart', + '../foo3.dart', + './foo2.dart', + 'a.dart', + 'aaa/aaa.dart', + 'bbb/bbb.dart', + 'foo1.dart', + ]); + }); + }); +} diff --git a/pkg/linter/test/rules/directives_ordering_test.dart b/pkg/linter/test/rules/directives_ordering_test.dart index 0c4871571f28..1c6dae70237b 100644 --- a/pkg/linter/test/rules/directives_ordering_test.dart +++ b/pkg/linter/test/rules/directives_ordering_test.dart @@ -213,6 +213,7 @@ import 'foo4.dart'; test_sortDirectiveSectionsAlphabetically_dotInRelativePath_import_ok() async { await assertNoDiagnostics(r''' +import '/foo5.dart'; import '../../foo4.dart'; import '../foo3.dart'; import './foo2.dart';