use oxc_diagnostics::OxcDiagnostic;
use react_compiler::entrypoint::compile_result::{
CompileResult, CompilerErrorDetailInfo, LoggerEvent,
};
pub fn compile_result_to_diagnostics(result: &CompileResult) -> Vec<OxcDiagnostic> {
let mut diagnostics = Vec::new();
match result {
CompileResult::Success { events, .. } => {
for event in events {
if let Some(diag) = event_to_diagnostic(event) {
diagnostics.push(diag);
}
}
}
CompileResult::Error { error, events, .. } => {
diagnostics.push(error_info_to_diagnostic(error));
for event in events {
if let Some(diag) = event_to_diagnostic(event) {
diagnostics.push(diag);
}
}
}
}
diagnostics
}
fn error_info_to_diagnostic(
error: &react_compiler::entrypoint::compile_result::CompilerErrorInfo,
) -> OxcDiagnostic {
let message = format!("[ReactCompiler] {}", error.reason);
let mut diag = OxcDiagnostic::error(message);
if let Some(description) = &error.description {
diag = diag.with_help(description.clone());
}
diag
}
fn error_detail_to_diagnostic(detail: &CompilerErrorDetailInfo) -> Option<OxcDiagnostic> {
let message = if let Some(description) = &detail.description {
format!("[ReactCompiler] {}: {}. {}", detail.category, detail.reason, description)
} else {
format!("[ReactCompiler] {}: {}", detail.category, detail.reason)
};
let diagnostic = match detail.severity.as_str() {
"Off" => return None,
"Error" => OxcDiagnostic::error(message),
_ => OxcDiagnostic::warn(message),
};
Some(diagnostic)
}
fn event_to_diagnostic(event: &LoggerEvent) -> Option<OxcDiagnostic> {
match event {
LoggerEvent::CompileSuccess { .. } | LoggerEvent::CompileSkip { .. } => None,
LoggerEvent::CompileError { detail, .. }
| LoggerEvent::CompileErrorWithLoc { detail, .. } => error_detail_to_diagnostic(detail),
LoggerEvent::CompileUnexpectedThrow { data, .. } => {
Some(OxcDiagnostic::error(format!("[ReactCompiler] Unexpected error: {}", data)))
}
LoggerEvent::PipelineError { data, .. } => {
Some(OxcDiagnostic::error(format!("[ReactCompiler] Pipeline error: {}", data)))
}
}
}