Skip to content

Commit

Permalink
refactor(linter): move DiagnosticsReporters to oxlint - optimize serv…
Browse files Browse the repository at this point in the history
…ice, do not cache output
  • Loading branch information
Sysix committed Jan 15, 2025
1 parent 270da72 commit 749e148
Showing 1 changed file with 24 additions and 36 deletions.
60 changes: 24 additions & 36 deletions crates/oxc_diagnostics/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ impl DiagnosticService {
/// * When the writer fails to write
pub fn run(&mut self, writer: &mut dyn Write) {
while let Ok(Some((path, diagnostics))) = self.receiver.recv() {
let mut output = String::new();
for diagnostic in diagnostics {
let severity = diagnostic.severity();
let is_warning = severity == Some(Severity::Warning);
Expand All @@ -192,59 +191,48 @@ impl DiagnosticService {
continue;
}

if let Some(mut err_str) = self.reporter.render_error(diagnostic) {
if let Some(err_str) = self.reporter.render_error(diagnostic) {
// Skip large output and print only once.
// Setting to 1200 because graphical output may contain ansi escape codes and other decorations.
if err_str.lines().any(|line| line.len() >= 1200) {
let minified_diagnostic = Error::new(
OxcDiagnostic::warn("File is too long to fit on the screen")
.with_help(format!("{path:?} seems like a minified file")),
);
err_str = format!("{minified_diagnostic:?}");
output = err_str;

if let Some(err_str) = self.reporter.render_error(minified_diagnostic) {
writer
.write_all(err_str.as_bytes())
.or_else(Self::check_for_writer_error)
.unwrap();
}
break;
}
output.push_str(&err_str);

writer
.write_all(err_str.as_bytes())
.or_else(Self::check_for_writer_error)
.unwrap();
}
}

writer
.write_all(output.as_bytes())
.or_else(|e| {
// Do not panic when the process is skill (e.g. piping into `less`).
if matches!(e.kind(), ErrorKind::Interrupted | ErrorKind::BrokenPipe) {
Ok(())
} else {
Err(e)
}
})
.unwrap();
}

if let Some(finish_output) = self.reporter.finish() {
writer
.write_all(finish_output.as_bytes())
.or_else(|e| {
// Do not panic when the process is skill (e.g. piping into `less`).
if matches!(e.kind(), ErrorKind::Interrupted | ErrorKind::BrokenPipe) {
Ok(())
} else {
Err(e)
}
})
.or_else(Self::check_for_writer_error)
.unwrap();
}

writer
.flush()
.or_else(|e| {
// Do not panic when the process is skill (e.g. piping into `less`).
if matches!(e.kind(), ErrorKind::Interrupted | ErrorKind::BrokenPipe) {
Ok(())
} else {
Err(e)
}
})
.unwrap();
writer.flush().or_else(Self::check_for_writer_error).unwrap();
}

fn check_for_writer_error(error: std::io::Error) -> Result<(), std::io::Error> {
// Do not panic when the process is skill (e.g. piping into `less`).
if matches!(error.kind(), ErrorKind::Interrupted | ErrorKind::BrokenPipe) {
Ok(())
} else {
Err(error)
}
}
}

0 comments on commit 749e148

Please sign in to comment.