Skip to content

Commit 348ad97

Browse files
committed
fix(linter): skip no-unused-vars on astro files (#11303)
fixes #11301
1 parent 590c27b commit 348ad97

File tree

2 files changed

+45
-3
lines changed
  • crates/oxc_linter/src/rules/eslint/no_unused_vars

2 files changed

+45
-3
lines changed

crates/oxc_linter/src/rules/eslint/no_unused_vars/mod.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,12 +215,15 @@ impl Rule for NoUnusedVars {
215215
}
216216

217217
fn should_run(&self, ctx: &ContextHost) -> bool {
218-
// ignore .d.ts and vue/svelte files.
218+
// ignore .d.ts and vue/svelte/astro files.
219219
// 1. declarations have side effects (they get merged together)
220-
// 2. vue/svelte scripts declare variables that get used in the template, which
220+
// 2. vue/svelte/astro scripts declare variables that get used in the template, which
221221
// we can't detect
222222
!ctx.source_type().is_typescript_definition()
223-
&& !ctx.file_path().extension().is_some_and(|ext| ext == "vue" || ext == "svelte")
223+
&& !ctx
224+
.file_path()
225+
.extension()
226+
.is_some_and(|ext| ext == "vue" || ext == "svelte" || ext == "astro")
224227
}
225228
}
226229

crates/oxc_linter/src/rules/eslint/no_unused_vars/tests/oxc.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Test cases created by oxc maintainers
22
3+
use std::path::PathBuf;
4+
35
use serde_json::json;
46

57
use super::NoUnusedVars;
@@ -1310,6 +1312,43 @@ fn test_report_vars_only_used_as_types() {
13101312
.test();
13111313
}
13121314

1315+
#[test]
1316+
fn test_should_run() {
1317+
let pass = vec![
1318+
(
1319+
r#"<script setup lang="ts"> import * as vue from 'vue' </script>"#,
1320+
None,
1321+
None,
1322+
Some(PathBuf::from("src/foo/bar.vue")),
1323+
),
1324+
(
1325+
r"---
1326+
import Welcome from '../components/Welcome.astro';
1327+
import Layout from '../layouts/Layout.astro';
1328+
---
1329+
<Layout>
1330+
<Welcome />
1331+
</Layout>",
1332+
None,
1333+
None,
1334+
Some(PathBuf::from("src/foo/bar.astro")),
1335+
),
1336+
(
1337+
r"<script>
1338+
import Nested from './Nested.svelte';
1339+
</script>
1340+
<Nested answer={42} />",
1341+
None,
1342+
None,
1343+
Some(PathBuf::from("src/foo/bar.svelte")),
1344+
),
1345+
];
1346+
1347+
Tester::new(NoUnusedVars::NAME, NoUnusedVars::PLUGIN, pass, vec![])
1348+
.intentionally_allow_no_fix_tests()
1349+
.test();
1350+
}
1351+
13131352
// #[test]
13141353
// fn test_template() {
13151354
// let pass = vec![];

0 commit comments

Comments
 (0)