Skip to main content

react_auditor/rules/nextjs/
no_sync_script.rs

1use oxc_ast::ast::Program;
2use oxc_ast_visit::Visit;
3use oxc_semantic::Semantic;
4
5use crate::rules::{Rule, RuleFinding, RuleMeta, Severity};
6
7pub struct NoSyncScript;
8
9const RULE_META: RuleMeta = RuleMeta {
10    id: "no-sync-script",
11    default_severity: Severity::Warning,
12    category: "nextjs",
13    description: "Use `strategy=\"afterInteractive\"` on `<Script>` from `next/script`",
14};
15
16impl Rule for NoSyncScript {
17    fn meta(&self) -> &RuleMeta {
18        &RULE_META
19    }
20
21    fn run(&self, program: &Program, _semantic: &Semantic, source_text: &str) -> Vec<RuleFinding> {
22        let mut collector = SyncScriptCollector {
23            findings: Vec::new(),
24            source: source_text,
25            script_imported: false,
26        };
27        collector.visit_program(program);
28        collector.findings
29    }
30}
31
32struct SyncScriptCollector<'a> {
33    findings: Vec<RuleFinding>,
34    source: &'a str,
35    script_imported: bool,
36}
37
38impl<'a> SyncScriptCollector<'a> {
39    fn add_finding(&mut self, start: usize, msg: String) {
40        let line = self.source[..start].lines().count().max(1);
41        let col = start - self.source[..start].rfind('\n').map(|i| i + 1).unwrap_or(0);
42        self.findings.push(RuleFinding {
43            line,
44            column: col + 1,
45            message: msg,
46        });
47    }
48}
49
50impl<'a> Visit<'a> for SyncScriptCollector<'a> {
51    fn visit_import_declaration(&mut self, decl: &oxc_ast::ast::ImportDeclaration<'a>) {
52        if decl.source.value.as_str() == "next/script" {
53            self.script_imported = true;
54        }
55    }
56
57    fn visit_jsx_opening_element(&mut self, el: &oxc_ast::ast::JSXOpeningElement<'a>) {
58        if !self.script_imported {
59            return;
60        }
61
62        let is_script = matches!(
63            &el.name,
64            oxc_ast::ast::JSXElementName::IdentifierReference(id)
65                if id.name.as_str() == "Script"
66        );
67
68        if !is_script {
69            return;
70        }
71
72        let mut has_strategy = false;
73        for attr_item in &el.attributes {
74            if let oxc_ast::ast::JSXAttributeItem::Attribute(attr) = attr_item
75                && let oxc_ast::ast::JSXAttributeName::Identifier(id) = &attr.name
76                && id.name.as_str() == "strategy"
77            {
78                has_strategy = true;
79                if let Some(val) = &attr.value
80                    && let oxc_ast::ast::JSXAttributeValue::StringLiteral(s) = val
81                    && s.value.as_str() == "afterInteractive"
82                {
83                    return;
84                }
85            }
86        }
87
88        if has_strategy {
89            self.add_finding(
90                el.span.start as usize,
91                "`<Script>` with `strategy` should use `\"afterInteractive\"` for better performance"
92                    .to_string(),
93            );
94        } else {
95            self.add_finding(
96                el.span.start as usize,
97                "`<Script>` from `next/script` should have `strategy=\"afterInteractive\"`"
98                    .to_string(),
99            );
100        }
101    }
102}