forked from oxc-project/oxc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(linter) eslint-plugin-next no-head-import-in-document (oxc-proje…
- Loading branch information
1 parent
8b06451
commit bc6e995
Showing
3 changed files
with
304 additions
and
0 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
252 changes: 252 additions & 0 deletions
252
crates/oxc_linter/src/rules/nextjs/no_head_import_in_document.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,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
50
crates/oxc_linter/src/snapshots/no_head_import_in_document.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,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] | ||
2 │ import Document, { Html, Main, NextScript } from 'next/document' | ||
3 │ import 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] | ||
2 │ import Document, { Html, Main, NextScript } from 'next/document' | ||
3 │ import 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] | ||
2 │ import Document, { Html, Main, NextScript } from 'next/document' | ||
3 │ import 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] | ||
2 │ import Document, { Html, Main, NextScript } from 'next/document' | ||
3 │ import 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] | ||
2 │ import Document, { Html, Main, NextScript } from 'next/document' | ||
3 │ import Head from 'next/head' | ||
· ──────────────────────────── | ||
4 │ | ||
╰──── | ||
help: See https://nextjs.org/docs/messages/no-head-import-in-document | ||
|
||
|