-
-
Notifications
You must be signed in to change notification settings - Fork 485
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(lint): add noDocumentImportInPage
rule
#4265
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
use biome_analyze::{ | ||
context::RuleContext, declare_lint_rule, Ast, Rule, RuleDiagnostic, RuleSource, RuleSourceKind, | ||
}; | ||
use biome_console::markup; | ||
use biome_js_syntax::{JsFileSource, JsImport}; | ||
use biome_rowan::AstNode; | ||
|
||
declare_lint_rule! { | ||
/// Prevents importing `next/document` outside of `pages/_document.jsx` in Next.js projects. | ||
/// | ||
/// The `next/document` module is intended for customizing the document structure globally in Next.js. | ||
/// Importing it outside of `pages/_document.js` can cause unexpected behavior and break certain features of the framework. | ||
/// | ||
/// ## Examples | ||
/// | ||
/// ### Valid | ||
/// | ||
/// ```jsx | ||
/// import { Document, Html } from 'next/document' | ||
/// | ||
/// export default class MyDocument extends Document { | ||
/// render() { | ||
/// return ( | ||
/// <Html lang="en"> | ||
/// {/* */} | ||
/// </Html> | ||
/// ) | ||
/// } | ||
/// } | ||
/// ``` | ||
/// | ||
pub NoDocumentImportInPage { | ||
version: "next", | ||
name: "noDocumentImportInPage", | ||
language: "jsx", | ||
sources: &[RuleSource::EslintNext("no-document-import-in-page")], | ||
source_kind: RuleSourceKind::SameLogic, | ||
recommended: false, | ||
} | ||
} | ||
|
||
impl Rule for NoDocumentImportInPage { | ||
type Query = Ast<JsImport>; | ||
type State = (); | ||
type Signals = Option<Self::State>; | ||
type Options = (); | ||
|
||
fn run(ctx: &RuleContext<Self>) -> Self::Signals { | ||
if !ctx.source_type::<JsFileSource>().is_jsx() { | ||
return None; | ||
} | ||
|
||
let import = ctx.query(); | ||
let import_source = import.import_clause().ok()?.source().ok()?; | ||
let module_name = import_source.inner_string_text().ok()?; | ||
|
||
if module_name != "next/document" { | ||
return None; | ||
} | ||
|
||
let path = ctx.file_path(); | ||
|
||
if !path | ||
.ancestors() | ||
.filter_map(|a| a.file_name()) | ||
.any(|f| f == "pages") | ||
{ | ||
return None; | ||
} | ||
|
||
let file_name = path.file_stem()?.to_str()?; | ||
let parent_name = path.parent()?.file_stem()?.to_str()?; | ||
|
||
if parent_name == "_document" || file_name == "_document" { | ||
return None; | ||
} | ||
|
||
Some(()) | ||
} | ||
|
||
fn diagnostic(ctx: &RuleContext<Self>, _: &Self::State) -> Option<RuleDiagnostic> { | ||
return Some( | ||
RuleDiagnostic::new( | ||
rule_category!(), | ||
ctx.query().range(), | ||
markup! { | ||
"Don't use "<Emphasis>"next/document"</Emphasis>" outside of pages/_document.jsx to avoid unexpected behaviors." | ||
}, | ||
) | ||
.note(markup! { | ||
"Only import "<Emphasis>"next/document"</Emphasis>" within "<Emphasis>"pages/_document.jsx"</Emphasis>" to customize the global document structure." | ||
}) | ||
); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import Document from "next/document"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
source: crates/biome_js_analyze/tests/spec_tests.rs | ||
assertion_line: 86 | ||
expression: valid.jsx | ||
--- | ||
# Input | ||
```jsx | ||
import Document from "next/document"; | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import Document from "next/document"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
source: crates/biome_js_analyze/tests/spec_tests.rs | ||
assertion_line: 86 | ||
expression: _document.jsx | ||
--- | ||
# Input | ||
```jsx | ||
import Document from "next/document"; | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import Document from "next/document"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--- | ||
source: crates/biome_js_analyze/tests/spec_tests.rs | ||
assertion_line: 86 | ||
expression: invalid.jsx | ||
--- | ||
# Input | ||
```jsx | ||
import Document from "next/document"; | ||
``` | ||
|
||
# Diagnostics | ||
``` | ||
invalid.jsx:1:1 lint/nursery/noDocumentImportInPage ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
||
! Don't use next/document outside of pages/_document.jsx to avoid unexpected behaviors. | ||
|
||
> 1 │ import Document from "next/document"; | ||
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
i Only import next/document within pages/_document.jsx to customize the global document structure. | ||
|
||
|
||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import Document from "next/document"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
source: crates/biome_js_analyze/tests/spec_tests.rs | ||
assertion_line: 86 | ||
expression: valid.jsx | ||
--- | ||
# Input | ||
```jsx | ||
import Document from "next/document"; | ||
``` |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You might want to explain the users why this error occurs ( See Rule pllars in this case.
In eslint-plugin-next/no-document-import-in-page, they are not explaining why the error is triggered but providing the link Why This Error Occurred.
We could add an explanation IMHO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The explanation in their link is pretty vague 😅
"to avoid unexpected behaviors" was the best I could extract from it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, Next.js documentation is usually bad 😔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree