Skip to content

Commit

Permalink
refactor(linter): move DiagnosticsReporters to oxlint
Browse files Browse the repository at this point in the history
  • Loading branch information
Sysix committed Jan 13, 2025
1 parent 7b0403a commit 4bdd341
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 3 deletions.
28 changes: 28 additions & 0 deletions apps/oxlint/src/output_formatter/checkstyle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,31 @@ fn xml_escape_impl<F: Fn(u8) -> bool>(raw: &str, escape_chars: F) -> Cow<str> {
Cow::Borrowed(raw)
}
}

#[cfg(test)]
mod test {
use oxc_diagnostics::{reporter::DiagnosticReporter, NamedSource, OxcDiagnostic};
use oxc_span::Span;

use super::CheckstyleReporter;

#[test]
fn reporter() {
let mut reporter = CheckstyleReporter::default();

let error = OxcDiagnostic::warn("error message")
.with_label(Span::new(0, 8))
.with_source_code(NamedSource::new("file://test.ts", "debugger;"));

let first_result = reporter.render_error(error);

// reporter keeps it in memory
assert!(first_result.is_none());

// report not gives us all diagnostics at ones
let second_result = reporter.finish();

assert!(second_result.is_some());
assert_eq!(second_result.unwrap(), "<?xml version=\"1.0\" encoding=\"utf-8\"?><checkstyle version=\"4.3\"><file name=\"file://test.ts\"><error line=\"1\" column=\"1\" severity=\"warning\" message=\"error message\" source=\"\" /></file></checkstyle>");
}
}
33 changes: 32 additions & 1 deletion apps/oxlint/src/output_formatter/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ impl DiagnosticReporter for GraphicalReporter {

#[cfg(test)]
mod test {
use crate::output_formatter::{default::DefaultOutputFormatter, InternalFormatter};
use crate::output_formatter::{
default::{DefaultOutputFormatter, GraphicalReporter},
InternalFormatter,
};
use miette::NamedSource;
use oxc_diagnostics::{reporter::DiagnosticReporter, OxcDiagnostic};
use oxc_span::Span;

#[test]
fn all_rules() {
Expand All @@ -60,4 +66,29 @@ mod test {
formatter.all_rules(&mut writer);
assert!(!writer.is_empty());
}

#[test]
fn reporter_finish() {
let mut reporter = GraphicalReporter::default();

let result = reporter.finish();

assert!(result.is_none());
}

#[test]
fn reporter_error() {
let mut reporter = GraphicalReporter::default();
let error = OxcDiagnostic::warn("error message")
.with_label(Span::new(0, 8))
.with_source_code(NamedSource::new("file://test.ts", "debugger;"));

let result = reporter.render_error(error);

assert!(result.is_some());
assert_eq!(
result.unwrap(),
"\n \u{1b}[38;2;244;191;117;1m⚠\u{1b}[0m \u{1b}[38;2;244;191;117;1merror message\u{1b}[0m\n ╭─[\u{1b}[38;2;92;157;255;1mfile://test.ts\u{1b}[0m:1:1]\n \u{1b}[2m1\u{1b}[0m │ debugger;\n · \u{1b}[38;2;246;87;248m────────\u{1b}[0m\n ╰────\n"
);
}
}
32 changes: 31 additions & 1 deletion apps/oxlint/src/output_formatter/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use oxc_diagnostics::{

use crate::output_formatter::InternalFormatter;

#[derive(Debug, Default)]
#[derive(Debug)]
pub struct GithubOutputFormatter;

impl InternalFormatter for GithubOutputFormatter {
Expand Down Expand Up @@ -81,3 +81,33 @@ fn escape_property(value: &str) -> String {
}
result
}

#[cfg(test)]
mod test {
use oxc_diagnostics::{reporter::DiagnosticReporter, NamedSource, OxcDiagnostic};
use oxc_span::Span;

use super::GithubReporter;

#[test]
fn reporter_finish() {
let mut reporter = GithubReporter;

let result = reporter.finish();

assert!(result.is_none());
}

#[test]
fn reporter_error() {
let mut reporter = GithubReporter;
let error = OxcDiagnostic::warn("error message")
.with_label(Span::new(0, 8))
.with_source_code(NamedSource::new("file://test.ts", "debugger;"));

let result = reporter.render_error(error);

assert!(result.is_some());
assert_eq!(result.unwrap(), "::warning file=file%3A//test.ts,line=1,endLine=1,col=1,endColumn=1,title=oxlint::error message\n");
}
}
31 changes: 31 additions & 0 deletions apps/oxlint/src/output_formatter/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,34 @@ fn format_json(diagnostics: &mut Vec<Error>) -> String {
.join(",\n");
format!("[\n{messages}\n]")
}

#[cfg(test)]
mod test {
use oxc_diagnostics::{reporter::DiagnosticReporter, NamedSource, OxcDiagnostic};
use oxc_span::Span;

use super::JsonReporter;

#[test]
fn reporter() {
let mut reporter = JsonReporter::default();

let error = OxcDiagnostic::warn("error message")
.with_label(Span::new(0, 8))
.with_source_code(NamedSource::new("file://test.ts", "debugger;"));

let first_result = reporter.render_error(error);

// reporter keeps it in memory
assert!(first_result.is_none());

// report not gives us all diagnostics at ones
let second_result = reporter.finish();

assert!(second_result.is_some());
assert_eq!(
second_result.unwrap(),
"[\n\t{\"message\": \"error message\",\"severity\": \"warning\",\"causes\": [],\"filename\": \"file://test.ts\",\"labels\": [{\"span\": {\"offset\": 0,\"length\": 8}}],\"related\": []}\n]"
);
}
}
2 changes: 1 addition & 1 deletion apps/oxlint/src/output_formatter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl OutputFormatter {
match format {
OutputFormat::Json => Box::<JsonOutputFormatter>::default(),
OutputFormat::Checkstyle => Box::<CheckStyleOutputFormatter>::default(),
OutputFormat::Github => Box::<GithubOutputFormatter>::default(),
OutputFormat::Github => Box::new(GithubOutputFormatter),
OutputFormat::Unix => Box::<UnixOutputFormatter>::default(),
OutputFormat::Default => Box::new(DefaultOutputFormatter),
}
Expand Down
30 changes: 30 additions & 0 deletions apps/oxlint/src/output_formatter/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,33 @@ fn format_unix(diagnostic: &Error) -> String {
rule_id.map_or_else(|| Cow::Borrowed(""), |rule_id| Cow::Owned(format!("/{rule_id}")));
format!("{filename}:{line}:{column}: {message} [{severity}{rule_id}]\n")
}

#[cfg(test)]
mod test {
use oxc_diagnostics::{reporter::DiagnosticReporter, NamedSource, OxcDiagnostic};
use oxc_span::Span;

use super::UnixReporter;

#[test]
fn reporter_finish() {
let mut reporter = UnixReporter::default();

let result = reporter.finish();

assert!(result.is_none());
}

#[test]
fn reporter_error() {
let mut reporter = UnixReporter::default();
let error = OxcDiagnostic::warn("error message")
.with_label(Span::new(0, 8))
.with_source_code(NamedSource::new("file://test.ts", "debugger;"));

let result = reporter.render_error(error);

assert!(result.is_some());
assert_eq!(result.unwrap(), "file://test.ts:1:1: error message [Warning]\n");
}
}

0 comments on commit 4bdd341

Please sign in to comment.