From 18d1c29a62c52edd57b09a8a0562abc8ebcd5411 Mon Sep 17 00:00:00 2001 From: Cameron Date: Tue, 9 Jan 2024 03:29:32 +0000 Subject: [PATCH] feat(linter) eslint-plugin-next no-script-component-in-head (#1954) --- crates/oxc_linter/src/rules.rs | 2 + .../nextjs/no_script_component_in_head.rs | 130 ++++++++++++++++++ .../no_script_component_in_head.snap | 14 ++ 3 files changed, 146 insertions(+) create mode 100644 crates/oxc_linter/src/rules/nextjs/no_script_component_in_head.rs create mode 100644 crates/oxc_linter/src/snapshots/no_script_component_in_head.snap diff --git a/crates/oxc_linter/src/rules.rs b/crates/oxc_linter/src/rules.rs index 1c616ff2c..fac448064 100644 --- a/crates/oxc_linter/src/rules.rs +++ b/crates/oxc_linter/src/rules.rs @@ -277,6 +277,7 @@ mod nextjs { pub mod no_async_client_component; pub mod no_css_tags; pub mod no_img_element; + pub mod no_script_component_in_head; pub mod no_sync_scripts; pub mod no_title_in_document_head; } @@ -525,6 +526,7 @@ oxc_macros::declare_all_lint_rules! { nextjs::no_async_client_component, nextjs::no_css_tags, nextjs::no_img_element, + nextjs::no_script_component_in_head, nextjs::no_sync_scripts, nextjs::no_title_in_document_head, } diff --git a/crates/oxc_linter/src/rules/nextjs/no_script_component_in_head.rs b/crates/oxc_linter/src/rules/nextjs/no_script_component_in_head.rs new file mode 100644 index 000000000..dc7186c99 --- /dev/null +++ b/crates/oxc_linter/src/rules/nextjs/no_script_component_in_head.rs @@ -0,0 +1,130 @@ +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-script-component-in-head): Prevent usage of `next/script` in `next/head` component.")] +#[diagnostic( + severity(warning), + help("See https://nextjs.org/docs/messages/no-script-component-in-head") +)] +struct NoScriptComponentInHeadDiagnostic(#[label] pub Span); + +#[derive(Debug, Default, Clone)] +pub struct NoScriptComponentInHead; + +declare_oxc_lint!( + /// ### What it does + /// + /// + /// ### Why is this bad? + /// + /// + /// ### Example + /// ```javascript + /// ``` + NoScriptComponentInHead, + correctness +); + +impl Rule for NoScriptComponentInHead { + 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 Some(import_specifiers) = &import_decl.specifiers else { return }; + + let Some(default_import) = import_specifiers.iter().find_map(|import_specifier| { + let ImportDeclarationSpecifier::ImportDefaultSpecifier(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() == "Script" { + ctx.diagnostic(NoScriptComponentInHeadDiagnostic( + jsx_opening_element.name.span(), + )); + } + } + } + } + } + } +} + +#[test] +fn test() { + use crate::tester::Tester; + + let pass = vec![ + r#"import Script from "next/script"; + const Head = ({children}) => children + + export default function Index() { + return ( + + + + ); + } + "#, + ]; + + let fail = vec![ + r#" + import Head from "next/head"; + import Script from "next/script"; + + export default function Index() { + return ( + + + + ); + }"#, + ]; + + Tester::new_without_config(NoScriptComponentInHead::NAME, pass, fail).test_and_snapshot(); +} diff --git a/crates/oxc_linter/src/snapshots/no_script_component_in_head.snap b/crates/oxc_linter/src/snapshots/no_script_component_in_head.snap new file mode 100644 index 000000000..96528e141 --- /dev/null +++ b/crates/oxc_linter/src/snapshots/no_script_component_in_head.snap @@ -0,0 +1,14 @@ +--- +source: crates/oxc_linter/src/tester.rs +expression: no_script_component_in_head +--- + ⚠ eslint-plugin-next(no-script-component-in-head): Prevent usage of `next/script` in `next/head` component. + ╭─[no_script_component_in_head.tsx:6:1] + 6 │ return ( + 7 │ + · ──── + 8 │ + ╰──── + help: See https://nextjs.org/docs/messages/no-script-component-in-head + +