Skip to content

Commit c8ea37d

Browse files
committed
fix(linter): unicorn/filename-case report only on the first js block for astro, vue and svelte files (#14599)
before: reported for each `<script>` block, the diagnostic after: only for the first `<script>` block.
1 parent e560cc1 commit c8ea37d

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

crates/oxc_linter/src/context/host.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,11 @@ impl<'a> ContextHost<'a> {
194194
&mut self.sub_hosts[self.current_sub_host_index.get()]
195195
}
196196

197+
// Whether the current sub host is the first one.
198+
pub fn is_first_sub_host(&self) -> bool {
199+
self.current_sub_host_index.get() == 0
200+
}
201+
197202
/// Shared reference to the [`Semantic`] analysis of current script block.
198203
#[inline]
199204
pub fn semantic(&self) -> &Semantic<'a> {

crates/oxc_linter/src/rules/unicorn/filename_case.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ use oxc_macros::declare_oxc_lint;
66
use oxc_span::Span;
77
use serde_json::Value;
88

9-
use crate::{context::LintContext, rule::Rule};
9+
use crate::{
10+
context::{ContextHost, LintContext},
11+
rule::Rule,
12+
};
1013

1114
fn filename_case_diagnostic(message: String, help_message: String) -> OxcDiagnostic {
1215
OxcDiagnostic::warn(message).with_label(Span::default()).with_help(help_message)
@@ -258,6 +261,10 @@ impl Rule for FilenameCase {
258261

259262
ctx.diagnostic(filename_case_diagnostic(message, help_message));
260263
}
264+
265+
fn should_run(&self, ctx: &ContextHost<'_>) -> bool {
266+
ctx.is_first_sub_host()
267+
}
261268
}
262269

263270
#[test]
@@ -441,6 +448,8 @@ fn test() {
441448
serde_json::json!([{ "case": "snakeCase", "multipleFileExtensions": false }]),
442449
),
443450
("", None, None, Some(PathBuf::from("FooBar.tsx"))),
451+
// should only report once
452+
("<script></script><script setup></script>", None, None, Some(PathBuf::from("FooBar.vue"))),
444453
];
445454

446455
Tester::new(FilenameCase::NAME, FilenameCase::PLUGIN, pass, fail).test_and_snapshot();

crates/oxc_linter/src/snapshots/unicorn_filename_case.snap

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,10 @@ source: crates/oxc_linter/src/tester.rs
160160
╭─[filename_case.tsx:1:1]
161161
╰────
162162
help: Rename the file to 'foo-bar.tsx'
163+
164+
eslint-plugin-unicorn(filename-case): Filename should be in kebab case
165+
╭─[filename_case.tsx:1:9]
166+
1 │ <script></script><script setup></script>
167+
· ▲
168+
╰────
169+
help: Rename the file to 'foo-bar.vue'

0 commit comments

Comments
 (0)