Skip to content

Commit

Permalink
Fix directives_ordering to sort '/' absolute paths first
Browse files Browse the repository at this point in the history
Basically the code change just falls back to the behavior prior to commit aed089e45c35221ce2b82f3757132031f0344b8b when a leading slash is encountered.

Fixes https://github.com/dart-lang/linter/issues/4869

Change-Id: I61fc7915f44f043f591ea439d40d4016fc8b3d27
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/350861
Commit-Queue: Oleh Prypin <[email protected]>
Reviewed-by: Samuel Rawlins <[email protected]>
  • Loading branch information
oprypin authored and Commit Queue committed Feb 7, 2024
1 parent 5b8ba36 commit 9fdb0ca
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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('/');
Expand Down
4 changes: 3 additions & 1 deletion pkg/linter/lib/src/rules/directives_ordering.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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('/');
Expand Down
56 changes: 56 additions & 0 deletions pkg/linter/test/directives_ordering_test.dart
Original file line number Diff line number Diff line change
@@ -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<String> 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',
]);
});
});
}
1 change: 1 addition & 0 deletions pkg/linter/test/rules/directives_ordering_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit 9fdb0ca

Please sign in to comment.