Skip to content

Commit 1192ea2

Browse files
committed
fix(linter): correct nextjs/no-typos path handling (#14480)
Fixes path separator handling that broke on Windows. Now uses `Path::components()` to properly detect `pages/api` directory on both Unix and Windows, avoiding false matches like `pages/apic`.
1 parent 2796b16 commit 1192ea2

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

crates/oxc_linter/src/rules/nextjs/no_typos.rs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,17 @@ const THRESHOLD: usize = 1;
6161

6262
impl Rule for NoTypos {
6363
fn should_run(&self, ctx: &ContextHost) -> bool {
64-
let Some(path) = ctx.file_path().to_str() else {
65-
return false;
66-
};
67-
let Some(path_after_pages) = path.split("pages").nth(1) else {
68-
return false;
69-
};
70-
if path_after_pages.starts_with("/api") {
71-
return false;
64+
let path = ctx.file_path();
65+
let mut found_pages = false;
66+
for component in path.components() {
67+
if found_pages {
68+
return component.as_os_str() != "api";
69+
}
70+
if component.as_os_str() == "pages" {
71+
found_pages = true;
72+
}
7273
}
73-
true
74+
false
7475
}
7576

7677
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
@@ -222,17 +223,17 @@ fn test() {
222223
),
223224
// even though there is a typo match, this should not fail because a file is inside pages/api directory
224225
(
225-
r"
226-
export default function Page() {
227-
return <div></div>;
228-
}
229-
export const getStaticpaths = async () => {};
230-
export const getStaticProps = async () => {};
231-
",
226+
r"export const getStaticpaths = async () => {};",
232227
None,
233228
None,
234229
Some(PathBuf::from("pages/api/test.tsx")),
235230
),
231+
(
232+
r"export const getStaticpaths = async () => {};",
233+
None,
234+
None,
235+
Some(PathBuf::from("pages\\api\\test.tsx")),
236+
),
236237
];
237238

238239
let fail = vec![

0 commit comments

Comments
 (0)