Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion crates/oxc_linter/src/rules/import/first.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ declare_oxc_lint!(
);

fn is_relative_path(path: &str) -> bool {
path.starts_with("./")
// A path is considered relative if it starts with "/", "./", or "../"
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#module_specifier_resolution
path.starts_with("./") || path.starts_with("../") || path.starts_with('/')
}

/// <https://github.com/import-js/eslint-plugin-import/blob/v2.29.1/docs/rules/first.md>
Expand Down Expand Up @@ -169,6 +171,17 @@ fn test() {
export { x, y }",
None,
),
// Relative imports with absolute-first
(
r"import { y } from 'bar';
import { x } from '../foo';",
Some(json!(["absolute-first"])),
),
(
r"import { y } from 'bar';
import { x } from '/foo';",
Some(json!(["absolute-first"])),
),
];

let fail = vec![
Expand Down Expand Up @@ -208,6 +221,17 @@ fn test() {
import F3 = require('mod');",
None,
),
// Relative imports with absolute-first
(
r"import { x } from '../foo';
import { y } from 'bar';",
Some(json!(["absolute-first"])),
),
(
r"import { x } from '/foo';
import { y } from 'bar';",
Some(json!(["absolute-first"])),
),
];

Tester::new(First::NAME, First::PLUGIN, pass, fail)
Expand Down
16 changes: 16 additions & 0 deletions crates/oxc_linter/src/snapshots/import_first.snap
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,19 @@ source: crates/oxc_linter/src/tester.rs
· ───────────────────────────
╰────
help: Move import statement to the top of the file

⚠ eslint-plugin-import(first): Relative imports before absolute imports are prohibited
╭─[index.ts:2:33]
1 │ import { x } from '../foo';
2 │ import { y } from 'bar';
· ─────
╰────
help: Move absolute import above relative import

⚠ eslint-plugin-import(first): Relative imports before absolute imports are prohibited
╭─[index.ts:2:33]
1 │ import { x } from '/foo';
2 │ import { y } from 'bar';
· ─────
╰────
help: Move absolute import above relative import
Loading