diff --git a/crates/oxc_linter/src/rules/eslint/no_unused_vars/mod.rs b/crates/oxc_linter/src/rules/eslint/no_unused_vars/mod.rs index 4f6e38bac726b..dd673fa150760 100644 --- a/crates/oxc_linter/src/rules/eslint/no_unused_vars/mod.rs +++ b/crates/oxc_linter/src/rules/eslint/no_unused_vars/mod.rs @@ -215,12 +215,15 @@ impl Rule for NoUnusedVars { } fn should_run(&self, ctx: &ContextHost) -> bool { - // ignore .d.ts and vue/svelte files. + // ignore .d.ts and vue/svelte/astro files. // 1. declarations have side effects (they get merged together) - // 2. vue/svelte scripts declare variables that get used in the template, which + // 2. vue/svelte/astro scripts declare variables that get used in the template, which // we can't detect !ctx.source_type().is_typescript_definition() - && !ctx.file_path().extension().is_some_and(|ext| ext == "vue" || ext == "svelte") + && !ctx + .file_path() + .extension() + .is_some_and(|ext| ext == "vue" || ext == "svelte" || ext == "astro") } } diff --git a/crates/oxc_linter/src/rules/eslint/no_unused_vars/tests/oxc.rs b/crates/oxc_linter/src/rules/eslint/no_unused_vars/tests/oxc.rs index 047a4629d409b..f9b35bed1baed 100644 --- a/crates/oxc_linter/src/rules/eslint/no_unused_vars/tests/oxc.rs +++ b/crates/oxc_linter/src/rules/eslint/no_unused_vars/tests/oxc.rs @@ -1,5 +1,7 @@ //! Test cases created by oxc maintainers +use std::path::PathBuf; + use serde_json::json; use super::NoUnusedVars; @@ -1310,6 +1312,43 @@ fn test_report_vars_only_used_as_types() { .test(); } +#[test] +fn test_should_run() { + let pass = vec![ + ( + r#""#, + None, + None, + Some(PathBuf::from("src/foo/bar.vue")), + ), + ( + r"--- +import Welcome from '../components/Welcome.astro'; +import Layout from '../layouts/Layout.astro'; +--- + + +", + None, + None, + Some(PathBuf::from("src/foo/bar.astro")), + ), + ( + r" + ", + None, + None, + Some(PathBuf::from("src/foo/bar.svelte")), + ), + ]; + + Tester::new(NoUnusedVars::NAME, NoUnusedVars::PLUGIN, pass, vec![]) + .intentionally_allow_no_fix_tests() + .test(); +} + // #[test] // fn test_template() { // let pass = vec![];