-
-
Notifications
You must be signed in to change notification settings - Fork 368
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-title-in-document-head
- Loading branch information
Showing
3 changed files
with
162 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
146 changes: 146 additions & 0 deletions
146
crates/oxc_linter/src/rules/nextjs/no_title_in_document_head.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,146 @@ | ||
use oxc_ast::{ | ||
ast::{ImportDeclarationSpecifier, JSXChild, JSXElementName, ModuleDeclaration}, | ||
AstKind, | ||
}; | ||
use oxc_diagnostics::{ | ||
miette::{self, Diagnostic}, | ||
thiserror::Error, | ||
}; | ||
use oxc_macros::declare_oxc_lint; | ||
use oxc_span::{GetSpan, Span}; | ||
|
||
use crate::{context::LintContext, rule::Rule, AstNode}; | ||
|
||
#[derive(Debug, Error, Diagnostic)] | ||
#[error("eslint-plugin-next(no-title-in-document-head): Prevent usage of `<title>` with `Head` component from `next/document`.")] | ||
#[diagnostic( | ||
severity(warning), | ||
help("See https://nextjs.org/docs/messages/no-title-in-document-head") | ||
)] | ||
struct NoTitleInDocumentHeadDiagnostic(#[label] pub Span); | ||
|
||
#[derive(Debug, Default, Clone)] | ||
pub struct NoTitleInDocumentHead; | ||
|
||
declare_oxc_lint!( | ||
/// ### What it does | ||
/// | ||
/// | ||
/// ### Why is this bad? | ||
/// | ||
/// | ||
/// ### Example | ||
/// ```javascript | ||
/// ``` | ||
NoTitleInDocumentHead, | ||
correctness | ||
); | ||
|
||
impl Rule for NoTitleInDocumentHead { | ||
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/document" { | ||
return; | ||
} | ||
|
||
let Some(import_specifiers) = &import_decl.specifiers else { return }; | ||
|
||
let Some(default_import) = import_specifiers.iter().find_map(|import_specifier| { | ||
let ImportDeclarationSpecifier::ImportSpecifier(import_default_specifier) = | ||
import_specifier | ||
else { | ||
return None; | ||
}; | ||
|
||
Some(import_default_specifier) | ||
}) else { | ||
return; | ||
}; | ||
|
||
for reference in | ||
ctx.semantic().symbol_references(default_import.local.symbol_id.get().unwrap()) | ||
{ | ||
let node = ctx.nodes().get_node(reference.node_id()); | ||
|
||
let AstKind::JSXElementName(_) = node.kind() else { continue }; | ||
let parent_node = ctx.nodes().parent_node(node.id()).unwrap(); | ||
let AstKind::JSXOpeningElement(jsx_opening_element) = parent_node.kind() else { | ||
continue; | ||
}; | ||
let Some(AstKind::JSXElement(jsx_element)) = ctx.nodes().parent_kind(parent_node.id()) | ||
else { | ||
continue; | ||
}; | ||
|
||
for child in &jsx_element.children { | ||
if let JSXChild::Element(child_element) = child { | ||
if let JSXElementName::Identifier(child_element_name) = | ||
&child_element.opening_element.name | ||
{ | ||
if child_element_name.name.as_str() == "title" { | ||
ctx.diagnostic(NoTitleInDocumentHeadDiagnostic( | ||
jsx_opening_element.name.span(), | ||
)); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn test() { | ||
use crate::tester::Tester; | ||
|
||
let pass = vec![ | ||
r#"import Head from "next/head"; | ||
class Test { | ||
render() { | ||
return ( | ||
<Head> | ||
<title>My page title</title> | ||
</Head> | ||
); | ||
} | ||
}"#, | ||
r#"import Document, { Html, Head } from "next/document"; | ||
class MyDocument extends Document { | ||
render() { | ||
return ( | ||
<Html> | ||
<Head> | ||
</Head> | ||
</Html> | ||
); | ||
} | ||
} | ||
export default MyDocument; | ||
"#, | ||
]; | ||
|
||
let fail = vec![ | ||
r#" | ||
import { Head } from "next/document"; | ||
class Test { | ||
render() { | ||
return ( | ||
<Head> | ||
<title>My page title</title> | ||
</Head> | ||
); | ||
} | ||
}"#, | ||
]; | ||
|
||
Tester::new_without_config(NoTitleInDocumentHead::NAME, pass, fail).test_and_snapshot(); | ||
} |
14 changes: 14 additions & 0 deletions
14
crates/oxc_linter/src/snapshots/no_title_in_document_head.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,14 @@ | ||
--- | ||
source: crates/oxc_linter/src/tester.rs | ||
expression: no_title_in_document_head | ||
--- | ||
⚠ eslint-plugin-next(no-title-in-document-head): Prevent usage of `<title>` with `Head` component from `next/document`. | ||
╭─[no_title_in_document_head.tsx:6:1] | ||
6 │ return ( | ||
7 │ <Head> | ||
· ──── | ||
8 │ <title>My page title</title> | ||
╰──── | ||
help: See https://nextjs.org/docs/messages/no-title-in-document-head | ||