Skip to content

Commit 679b70e

Browse files
fix(linter): fix useImportExtensions handling of index files (#7209)
Co-authored-by: Arend van Beelen jr. <arend@arendjr.nl>
1 parent 8a8e2e6 commit 679b70e

File tree

4 files changed

+210
-162
lines changed

4 files changed

+210
-162
lines changed

.changeset/petite-pans-fetch.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
"@biomejs/biome": patch
3+
---
4+
5+
Resolved an overcorrection in [`useImportExtensions`](https://biomejs.dev/linter/rules/use-import-extensions/) when importing explicit index files.
6+
7+
Imports that explicitly reference an index file are now preserved and no longer rewritten to nested index paths.
8+
9+
#### Example
10+
11+
```diff
12+
// Before
13+
- import "./sub/index";
14+
+ import "./sub/index/index.js";
15+
16+
// After
17+
- import "./sub/index";
18+
+ import "./sub/index.js";
19+
```

crates/biome_js_analyze/src/lint/correctness/use_import_extensions.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,9 @@ fn get_extensionless_import(
251251
};
252252

253253
let is_index_file = resolved_stem.is_some_and(|stem| stem == "index");
254+
let import_path_ends_with_index = path.file_name().is_some_and(|name| name == "index");
254255

255-
let new_path = if is_index_file {
256+
let new_path = if is_index_file && !import_path_ends_with_index {
256257
let mut path_parts = path.as_str().split('/');
257258

258259
// Remove trailing slash and useless path segment.

crates/biome_js_analyze/tests/specs/correctness/useImportExtensions/invalid.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import "./sub/foo";
22
import "./sub/bar/";
3+
import "./sub/index";
34

45
// Guaranteed resolve to 'index.js' file
56
import './sub/bar/../'

0 commit comments

Comments
 (0)