-
-
Notifications
You must be signed in to change notification settings - Fork 491
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(linter): move DiagnosticsReporters to oxlint (#8454)
it was never the plan that oxc_diagnostics should be the `std::io::stdout` writer: https://github.com/oxc-project/oxc/blob/8fc238ac34462945cac66dfa3c5c5f7993cc7df4/crates/oxc_diagnostics/src/service.rs#L84-L85 This PR does refactor all reporters to `oxlint` crate and make the current implementation of `oxc_diagnostics` public for others to consume. I added some tests to reflect to expected output (and found some bugs^^) For the future I think the BufWriter for `std::io::stdout` should come from outside. Or maybe `std::fmt::stdout`? I do not know^^" I could not test 100% of the code, I hope I can fix this with the next PR which will include a own Tester for oxlint (like `oxc_linter`). Please be extra careful when reviewing it. --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
9ec4e24
commit b4c87e2
Showing
17 changed files
with
484 additions
and
326 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,104 @@ | ||
use std::io::Write; | ||
|
||
use oxc_diagnostics::{reporter::DiagnosticReporter, Error, GraphicalReportHandler}; | ||
use oxc_linter::table::RuleTable; | ||
|
||
use crate::output_formatter::InternalFormatter; | ||
|
||
#[derive(Debug)] | ||
pub struct DefaultOutputFormatter; | ||
|
||
impl DefaultOutputFormatter { | ||
pub fn all_rules<T: Write>(writer: &mut T) { | ||
impl InternalFormatter for DefaultOutputFormatter { | ||
fn all_rules(&mut self, writer: &mut dyn Write) { | ||
let table = RuleTable::new(); | ||
for section in table.sections { | ||
writeln!(writer, "{}", section.render_markdown_table(None)).unwrap(); | ||
} | ||
writeln!(writer, "Default: {}", table.turned_on_by_default_count).unwrap(); | ||
writeln!(writer, "Total: {}", table.total).unwrap(); | ||
} | ||
|
||
fn get_diagnostic_reporter(&self) -> Box<dyn DiagnosticReporter> { | ||
Box::new(GraphicalReporter::default()) | ||
} | ||
} | ||
|
||
/// Pretty-prints diagnostics. Primarily meant for human-readable output in a terminal. | ||
/// | ||
/// See [`GraphicalReportHandler`] for how to configure colors, context lines, etc. | ||
struct GraphicalReporter { | ||
handler: GraphicalReportHandler, | ||
} | ||
|
||
impl Default for GraphicalReporter { | ||
fn default() -> Self { | ||
Self { handler: GraphicalReportHandler::new() } | ||
} | ||
} | ||
|
||
impl DiagnosticReporter for GraphicalReporter { | ||
fn finish(&mut self) -> Option<String> { | ||
None | ||
} | ||
|
||
fn render_error(&mut self, error: Error) -> Option<String> { | ||
let mut output = String::new(); | ||
self.handler.render_report(&mut output, error.as_ref()).unwrap(); | ||
Some(output) | ||
} | ||
} | ||
impl GraphicalReporter { | ||
#[cfg(test)] | ||
pub fn with_handler(mut self, handler: GraphicalReportHandler) -> Self { | ||
self.handler = handler; | ||
self | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use crate::output_formatter::default::DefaultOutputFormatter; | ||
use crate::output_formatter::{ | ||
default::{DefaultOutputFormatter, GraphicalReporter}, | ||
InternalFormatter, | ||
}; | ||
use miette::{GraphicalReportHandler, GraphicalTheme, NamedSource}; | ||
use oxc_diagnostics::{reporter::DiagnosticReporter, OxcDiagnostic}; | ||
use oxc_span::Span; | ||
|
||
#[test] | ||
fn all_rules() { | ||
let mut writer = Vec::new(); | ||
let mut formatter = DefaultOutputFormatter; | ||
|
||
DefaultOutputFormatter::all_rules(&mut writer); | ||
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().with_handler( | ||
GraphicalReportHandler::new_themed(GraphicalTheme::none()).with_links(false), | ||
); | ||
|
||
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 ! error message\n ,-[file://test.ts:1:1]\n 1 | debugger;\n : ^^^^^^^^\n `----\n" | ||
); | ||
} | ||
} |
Oops, something went wrong.