Skip to content

Commit 9cc87cf

Browse files
committed
fix: #85 - Fix panic when module specifier ends with a period and sorting import declarations
1 parent c8f1e4f commit 9cc87cf

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/parsing/sorting/module_specifiers.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,17 @@ fn get_module_specifier_info<'a>(text: &'a str) -> ModuleSpecifierInfo<'a> {
4545
if parts[0] == "." || parts[0] == ".." {
4646
let mut relative_count = 0;
4747
let mut start_index = 0;
48-
for part in parts {
48+
let parts_len = parts.len();
49+
for (i, part) in parts.into_iter().enumerate() {
4950
if part == ".." {
5051
relative_count += 1;
5152
} else if part != "." {
5253
break;
5354
}
54-
start_index += part.len() + 1;
55+
56+
// use 0 for last part since it does not have a slash after it
57+
let next_slash_width = if i == parts_len - 1 { 0 } else { 1 };
58+
start_index += part.len() + next_slash_width;
5559
}
5660

5761
ModuleSpecifierInfo {
@@ -83,6 +87,7 @@ mod test {
8387
assert_eq!(cmp_module_specifiers("'./a'", "'../a'", |a, b| a.cmp(b)), Ordering::Greater);
8488
assert_eq!(cmp_module_specifiers("'../../a'", "'../a'", |a, b| a.cmp(b)), Ordering::Less);
8589
assert_eq!(cmp_module_specifiers("'../a'", "'../../a'", |a, b| a.cmp(b)), Ordering::Greater);
90+
assert_eq!(cmp_module_specifiers("'..'", "'test'", |a, b| a.cmp(b)), Ordering::Greater);
8691
}
8792

8893
#[test]

tests/specs/issues/issue0085.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
~~ importDeclaration.sortNamedImports: caseSensitive ~~
2+
== should not panic when sorting a module specifier that ends with a period ==
3+
import { apiV1 } from "..";
4+
import { apiV2 } from ".";
5+
import { test } from "test";
6+
7+
[expect]
8+
import { test } from "test";
9+
import { apiV1 } from "..";
10+
import { apiV2 } from ".";

0 commit comments

Comments
 (0)