-
-
Notifications
You must be signed in to change notification settings - Fork 485
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(lint): add
noDocumentImportInPage
rule (#4265)
Co-authored-by: unvalley <38400669+unvalley@users.noreply.github.com>
- Loading branch information
1 parent
4d3e6cd
commit 2342984
Showing
17 changed files
with
283 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
crates/biome_cli/src/execute/migrate/eslint_any_rule_to_biome.rs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
188 changes: 104 additions & 84 deletions
188
crates/biome_configuration/src/analyzer/linter/rules.rs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
crates/biome_js_analyze/src/lint/nursery/no_document_import_in_page.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
crates/biome_js_analyze/tests/specs/nursery/noDocumentImportInPage/app/valid.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import Document from "next/document"; |
9 changes: 9 additions & 0 deletions
9
crates/biome_js_analyze/tests/specs/nursery/noDocumentImportInPage/app/valid.jsx.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
``` |
1 change: 1 addition & 0 deletions
1
crates/biome_js_analyze/tests/specs/nursery/noDocumentImportInPage/pages/_document.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import Document from "next/document"; |
9 changes: 9 additions & 0 deletions
9
crates/biome_js_analyze/tests/specs/nursery/noDocumentImportInPage/pages/_document.jsx.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
``` |
1 change: 1 addition & 0 deletions
1
crates/biome_js_analyze/tests/specs/nursery/noDocumentImportInPage/pages/invalid.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import Document from "next/document"; |
23 changes: 23 additions & 0 deletions
23
crates/biome_js_analyze/tests/specs/nursery/noDocumentImportInPage/pages/invalid.jsx.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
``` |
1 change: 1 addition & 0 deletions
1
crates/biome_js_analyze/tests/specs/nursery/noDocumentImportInPage/valid.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import Document from "next/document"; |
9 changes: 9 additions & 0 deletions
9
crates/biome_js_analyze/tests/specs/nursery/noDocumentImportInPage/valid.jsx.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.