Skip to content

Commit f2eff56

Browse files
authoredApr 5, 2025··
fix(linter): fix rule_id for some diagnostics formats (#10251)
closes #10204
1 parent 331ab7a commit f2eff56

File tree

4 files changed

+14
-11
lines changed

4 files changed

+14
-11
lines changed
 

Diff for: ‎apps/oxlint/src/output_formatter/checkstyle.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ fn format_checkstyle(diagnostics: &[Error]) -> String {
5252
Severity::Error => "error",
5353
_ => "warning",
5454
};
55-
let message = rule_id.as_ref().map_or_else(|| xml_escape(message), |rule_id| Cow::Owned(format!("{} ({rule_id})", xml_escape(message))));
56-
let source = rule_id.as_ref().map_or_else(|| Cow::Borrowed(""), |rule_id| Cow::Owned(format!("eslint.rules.{rule_id}")));
55+
let message = xml_escape(message);
56+
let source = rule_id.as_ref().map_or(Cow::Borrowed(""), |v| xml_escape(v));
5757
let line = format!(r#"<error line="{}" column="{}" severity="{severity}" message="{message}" source="{source}" />"#, start.line, start.column);
5858
acc.push_str(&line);
5959
acc

Diff for: ‎apps/oxlint/src/snapshots/fixtures__output_formatter_diagnostic_--format=checkstyle test.js@oxlint.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ source: apps/oxlint/src/tester.rs
55
arguments: --format=checkstyle test.js
66
working directory: fixtures/output_formatter_diagnostic
77
----------
8-
<?xml version="1.0" encoding="utf-8"?><checkstyle version="4.3"><file name="test.js"><error line="5" column="1" severity="error" message="`debugger` statement is not allowed" source="" /><error line="1" column="10" severity="warning" message="Function &apos;foo&apos; is declared but never used." source="" /><error line="1" column="17" severity="warning" message="Parameter &apos;b&apos; is declared but never used. Unused parameters should start with a &apos;_&apos;." source="" /></file></checkstyle>
8+
<?xml version="1.0" encoding="utf-8"?><checkstyle version="4.3"><file name="test.js"><error line="5" column="1" severity="error" message="`debugger` statement is not allowed" source="eslint(no-debugger)" /><error line="1" column="10" severity="warning" message="Function &apos;foo&apos; is declared but never used." source="eslint(no-unused-vars)" /><error line="1" column="17" severity="warning" message="Parameter &apos;b&apos; is declared but never used. Unused parameters should start with a &apos;_&apos;." source="eslint(no-unused-vars)" /></file></checkstyle>
99
----------
1010
CLI result: LintFoundErrors
1111
----------

Diff for: ‎apps/oxlint/src/snapshots/fixtures__output_formatter_diagnostic_--format=github test.js@oxlint.snap

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ source: apps/oxlint/src/tester.rs
55
arguments: --format=github test.js
66
working directory: fixtures/output_formatter_diagnostic
77
----------
8-
::error file=test.js,line=5,endLine=5,col=1,endColumn=10,title=oxlint::`debugger` statement is not allowed
9-
::warning file=test.js,line=1,endLine=1,col=10,endColumn=13,title=oxlint::Function 'foo' is declared but never used.
10-
::warning file=test.js,line=1,endLine=1,col=17,endColumn=18,title=oxlint::Parameter 'b' is declared but never used. Unused parameters should start with a '_'.
8+
::error file=test.js,line=5,endLine=5,col=1,endColumn=10,title=eslint(no-debugger)::`debugger` statement is not allowed
9+
::warning file=test.js,line=1,endLine=1,col=10,endColumn=13,title=eslint(no-unused-vars)::Function 'foo' is declared but never used.
10+
::warning file=test.js,line=1,endLine=1,col=17,endColumn=18,title=eslint(no-unused-vars)::Parameter 'b' is declared but never used. Unused parameters should start with a '_'.
1111
----------
1212
CLI result: LintFoundErrors
1313
----------

Diff for: ‎crates/oxc_diagnostics/src/reporter.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ impl DiagnosticResult {
9191
}
9292
}
9393

94+
#[derive(Debug)]
9495
pub struct Info {
9596
pub start: InfoPosition,
9697
pub end: InfoPosition,
@@ -100,6 +101,7 @@ pub struct Info {
100101
pub rule_id: Option<String>,
101102
}
102103

104+
#[derive(Debug)]
103105
pub struct InfoPosition {
104106
pub line: usize,
105107
pub column: usize,
@@ -112,7 +114,8 @@ impl Info {
112114
let mut filename = String::new();
113115
let mut message = String::new();
114116
let mut severity = Severity::Warning;
115-
let mut rule_id = None;
117+
let rule_id = diagnostic.code().map(|code| code.to_string());
118+
116119
if let Some(mut labels) = diagnostic.labels() {
117120
if let Some(source) = diagnostic.source_code() {
118121
if let Some(label) = labels.next() {
@@ -136,11 +139,11 @@ impl Info {
136139
severity = Severity::Error;
137140
}
138141
let msg = diagnostic.to_string();
142+
139143
// Our messages usually comes with `eslint(rule): message`
140-
(rule_id, message) = msg.split_once(':').map_or_else(
141-
|| (None, msg.to_string()),
142-
|(id, msg)| (Some(id.to_string()), msg.trim().to_string()),
143-
);
144+
message = msg
145+
.split_once(':')
146+
.map_or_else(|| msg.to_string(), |(_, msg)| msg.trim().to_string());
144147
}
145148
}
146149
}

0 commit comments

Comments
 (0)
Please sign in to comment.