refactor(oxc_diagnostics): remove over complicated Diagnostics struct

This commit is contained in:
Boshen 2023-04-01 14:09:20 +08:00
parent d4ff0bb40e
commit 59476e42d4
2 changed files with 12 additions and 34 deletions

View file

@ -4,7 +4,7 @@
mod graphic_reporter;
mod graphical_theme;
use std::{cell::RefCell, ops::Deref, path::PathBuf, rc::Rc};
use std::path::PathBuf;
pub use graphic_reporter::GraphicalReportHandler;
pub use miette;
@ -19,25 +19,6 @@ pub type Report = miette::Report;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, Default, Clone)]
pub struct Diagnostics(Rc<RefCell<Vec<Error>>>);
impl Deref for Diagnostics {
type Target = Rc<RefCell<Vec<Error>>>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl Diagnostics {
/// # Panics
#[must_use]
pub fn into_inner(self) -> Vec<Error> {
Rc::try_unwrap(self.0).unwrap().into_inner()
}
}
#[derive(Debug, Error, Diagnostic)]
#[error("Identifier `{0}` has already been declared")]
#[diagnostic()]

View file

@ -3,7 +3,6 @@ use std::{rc::Rc, sync::Arc};
use miette::NamedSource;
use oxc_allocator::Allocator;
use oxc_ast::SourceType;
use oxc_diagnostics::Diagnostics;
use oxc_linter::Linter;
use oxc_parser::Parser;
use oxc_semantic::SemanticBuilder;
@ -30,24 +29,21 @@ impl Driver {
.allow_return_outside_function(true)
.parse();
let mut diagnostics = ret.errors;
let program = self.allocator.alloc(ret.program);
let diagnostics = Diagnostics::default();
let semantic_ret =
SemanticBuilder::new(source_text, source_type, &ret.trivias).build(program);
let mut diagnostics = diagnostics.into_inner();
diagnostics.extend(semantic_ret.errors);
let source = Arc::new(NamedSource::new(path, source_text.to_string()));
diagnostics.extend(
Linter::from_json_str(eslintrc)
.with_fix(false)
.run(&Rc::new(semantic_ret.semantic))
.into_iter()
.map(|m| m.error.with_source_code(Arc::clone(&source)))
.chain(ret.errors)
.chain(semantic_ret.errors),
);
let messages =
Linter::from_json_str(eslintrc).with_fix(false).run(&Rc::new(semantic_ret.semantic));
diagnostics
.extend(messages.into_iter().map(|m| m.error.with_source_code(Arc::clone(&source))));
if diagnostics.is_empty() {
if let Ok(ast) = JsValue::from_serde(program) {
@ -55,11 +51,12 @@ impl Driver {
}
}
let result = diagnostics
let diagnostics = diagnostics
.into_iter()
.map(|error| format!("{error:?}"))
.collect::<Vec<String>>()
.join("\n");
JsValue::from_str(&result)
JsValue::from_str(&diagnostics)
}
}