Skip to content
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(linter) eslint-plugin-next no-head-import-in-document #1957

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
2 changes: 2 additions & 0 deletions crates/oxc_linter/src/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ mod nextjs {
pub mod no_assign_module_variable;
pub mod no_async_client_component;
pub mod no_css_tags;
pub mod no_head_import_in_document;
pub mod no_img_element;
pub mod no_script_component_in_head;
pub mod no_sync_scripts;
Expand Down Expand Up @@ -525,6 +526,7 @@ oxc_macros::declare_all_lint_rules! {
nextjs::no_assign_module_variable,
nextjs::no_async_client_component,
nextjs::no_css_tags,
nextjs::no_head_import_in_document,
nextjs::no_img_element,
nextjs::no_script_component_in_head,
nextjs::no_sync_scripts,
Expand Down
252 changes: 252 additions & 0 deletions crates/oxc_linter/src/rules/nextjs/no_head_import_in_document.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
use oxc_ast::{ast::ModuleDeclaration, AstKind};
use oxc_diagnostics::{
miette::{self, Diagnostic},
thiserror::Error,
};
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;

use crate::{context::LintContext, rule::Rule, AstNode};

#[derive(Debug, Error, Diagnostic)]
#[error("eslint-plugin-next(no-head-import-in-document): Prevent usage of `next/head` in `pages/_document.js`.")]
#[diagnostic(
severity(warning),
help("See https://nextjs.org/docs/messages/no-head-import-in-document")
)]
struct NoHeadImportInDocumentDiagnostic(#[label] pub Span);

#[derive(Debug, Default, Clone)]
pub struct NoHeadImportInDocument;

declare_oxc_lint!(
/// ### What it does
///
///
/// ### Why is this bad?
///
///
/// ### Example
/// ```javascript
/// ```
NoHeadImportInDocument,
correctness
);

impl Rule for NoHeadImportInDocument {
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
let AstKind::ModuleDeclaration(ModuleDeclaration::ImportDeclaration(import_decl)) =
node.kind()
else {
return;
};

if import_decl.source.value.as_str() != "next/head" {
return;
}

let full_file_path = ctx.file_path();

if let Some(file_name) = full_file_path.file_name() {
if let Some(file_name) = file_name.to_str() {
// check `_document.*` case

if file_name.starts_with("_document.") {
ctx.diagnostic(NoHeadImportInDocumentDiagnostic(import_decl.span));
// check `_document/index.*` case
} else if file_name.starts_with("index") {
if let Some(p) = full_file_path.parent() {
if let Some(file_name) = p.file_name() {
if let Some(file_name) = file_name.to_str() {
if file_name.starts_with("_document") {
ctx.diagnostic(NoHeadImportInDocumentDiagnostic(
import_decl.span,
));
}
}
}
}
}
}
}
}
}

#[test]
fn test() {
use crate::tester::Tester;
use std::path::PathBuf;

let pass = vec![
(
r"import Document, { Html, Head, Main, NextScript } from 'next/document'
class MyDocument extends Document {
static async getInitialProps(ctx) {
//...
}
render() {
return (
<Html>
<Head>
</Head>
</Html>
)
}
}
export default MyDocument
",
None,
None,
Some(PathBuf::from("pages/_document.tsx")),
),
(
r#"import Head from "next/head";
export default function IndexPage() {
return (
<Head>
<title>My page title</title>
<meta name="viewport" content="initial-scale=1.0, width=device-width" />
</Head>
);
}
"#,
None,
None,
Some(PathBuf::from("pages/index.tsx")),
),
];

let fail = vec![
(
r"
import Document, { Html, Main, NextScript } from 'next/document'
import Head from 'next/head'
class MyDocument extends Document {
render() {
return (
<Html>
<Head />
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
}
export default MyDocument
",
None,
None,
Some(PathBuf::from("pages/_document.js")),
),
(
r"
import Document, { Html, Main, NextScript } from 'next/document'
import Head from 'next/head'
class MyDocument extends Document {
render() {
return (
<Html>
<Head />
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
}
export default MyDocument
",
None,
None,
Some(PathBuf::from("pages/_document.tsx")),
),
(
r"
import Document, { Html, Main, NextScript } from 'next/document'
import Head from 'next/head'
class MyDocument extends Document {
render() {
return (
<Html>
<Head />
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
}
export default MyDocument
",
None,
None,
Some(PathBuf::from("pages/_document.page.tsx")),
),
(
r"
import Document, { Html, Main, NextScript } from 'next/document'
import Head from 'next/head'
class MyDocument extends Document {
render() {
return (
<Html>
<Head />
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
}
export default MyDocument
",
None,
None,
Some(PathBuf::from("pages/_document/index.tsx")),
),
(
r"
import Document, { Html, Main, NextScript } from 'next/document'
import Head from 'next/head'
class MyDocument extends Document {
render() {
return (
<Html>
<Head />
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
}
export default MyDocument
",
None,
None,
Some(PathBuf::from("pages/_document/index.tsx")),
),
];

Tester::new_with_settings(NoHeadImportInDocument::NAME, pass, fail).test_and_snapshot();
}
50 changes: 50 additions & 0 deletions crates/oxc_linter/src/snapshots/no_head_import_in_document.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---
source: crates/oxc_linter/src/tester.rs
expression: no_head_import_in_document
---
eslint-plugin-next(no-head-import-in-document): Prevent usage of `next/head` in `pages/_document.js`.
╭─[no_head_import_in_document.tsx:2:1]
2import Document, { Html, Main, NextScript } from 'next/document'
3import Head from 'next/head'
· ────────────────────────────
4
╰────
help: See https://nextjs.org/docs/messages/no-head-import-in-document

eslint-plugin-next(no-head-import-in-document): Prevent usage of `next/head` in `pages/_document.js`.
╭─[no_head_import_in_document.tsx:2:1]
2import Document, { Html, Main, NextScript } from 'next/document'
3import Head from 'next/head'
· ────────────────────────────
4
╰────
help: See https://nextjs.org/docs/messages/no-head-import-in-document

eslint-plugin-next(no-head-import-in-document): Prevent usage of `next/head` in `pages/_document.js`.
╭─[no_head_import_in_document.tsx:2:1]
2import Document, { Html, Main, NextScript } from 'next/document'
3import Head from 'next/head'
· ────────────────────────────
4
╰────
help: See https://nextjs.org/docs/messages/no-head-import-in-document

eslint-plugin-next(no-head-import-in-document): Prevent usage of `next/head` in `pages/_document.js`.
╭─[no_head_import_in_document.tsx:2:1]
2import Document, { Html, Main, NextScript } from 'next/document'
3import Head from 'next/head'
· ────────────────────────────
4
╰────
help: See https://nextjs.org/docs/messages/no-head-import-in-document

eslint-plugin-next(no-head-import-in-document): Prevent usage of `next/head` in `pages/_document.js`.
╭─[no_head_import_in_document.tsx:2:1]
2import Document, { Html, Main, NextScript } from 'next/document'
3import Head from 'next/head'
· ────────────────────────────
4
╰────
help: See https://nextjs.org/docs/messages/no-head-import-in-document