Skip to content

Commit 2cf63ea

Browse files
committed
perf(linter): nextjs/no-document-import-in-page check the filepath before running the rule (#11962)
Now, check before, if the file is inside `pages/_document` folder, then run the rule. Before: Run over all imports, before checking the file path
1 parent c4a95a2 commit 2cf63ea

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ use oxc_diagnostics::OxcDiagnostic;
33
use oxc_macros::declare_oxc_lint;
44
use oxc_span::Span;
55

6-
use crate::{AstNode, context::LintContext, rule::Rule, utils::is_document_page};
6+
use crate::{
7+
AstNode,
8+
context::{ContextHost, LintContext},
9+
rule::Rule,
10+
utils::is_document_page,
11+
};
712

813
fn no_document_import_in_page_diagnostic(span: Span) -> OxcDiagnostic {
914
OxcDiagnostic::warn("`<Document />` from `next/document` should not be imported outside of `pages/_document.js`. See: https://nextjs.org/docs/messages/no-document-import-in-page").with_help("Prevent importing `next/document` outside of `pages/_document.js`.").with_label(span)
@@ -46,15 +51,15 @@ impl Rule for NoDocumentImportInPage {
4651
return;
4752
}
4853

54+
ctx.diagnostic(no_document_import_in_page_diagnostic(import_decl.span));
55+
}
56+
57+
fn should_run(&self, ctx: &ContextHost) -> bool {
4958
let Some(path) = ctx.file_path().to_str() else {
50-
return;
59+
return false;
5160
};
5261

53-
if is_document_page(path) {
54-
return;
55-
}
56-
57-
ctx.diagnostic(no_document_import_in_page_diagnostic(import_decl.span));
62+
!is_document_page(path)
5863
}
5964
}
6065

0 commit comments

Comments
 (0)