mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 12:19:15 +00:00
refactor: clean up more diagnostics usages
This commit is contained in:
parent
8244d2be0a
commit
893af230c0
9 changed files with 41 additions and 39 deletions
|
|
@ -99,7 +99,7 @@ impl DiagnosticService {
|
|||
pub fn wrap_diagnostics(
|
||||
path: &Path,
|
||||
source_text: &str,
|
||||
diagnostics: Vec<Error>,
|
||||
diagnostics: Vec<OxcDiagnostic>,
|
||||
) -> (PathBuf, Vec<Error>) {
|
||||
let source = Arc::new(NamedSource::new(path.to_string_lossy(), source_text.to_owned()));
|
||||
let diagnostics = diagnostics
|
||||
|
|
|
|||
|
|
@ -296,7 +296,10 @@ impl IsolatedLintHandler {
|
|||
let reports = semantic_ret
|
||||
.errors
|
||||
.into_iter()
|
||||
.map(|diagnostic| ErrorReport { error: diagnostic, fixed_content: None })
|
||||
.map(|diagnostic| ErrorReport {
|
||||
error: Error::from(diagnostic),
|
||||
fixed_content: None,
|
||||
})
|
||||
.collect();
|
||||
return Some(Self::wrap_diagnostics(path, &original_source_text, reports, start));
|
||||
};
|
||||
|
|
@ -327,7 +330,7 @@ impl IsolatedLintHandler {
|
|||
},
|
||||
});
|
||||
|
||||
ErrorReport { error: msg.error, fixed_content }
|
||||
ErrorReport { error: Error::from(msg.error), fixed_content }
|
||||
})
|
||||
.collect::<Vec<ErrorReport>>();
|
||||
let (_, errors_with_position) =
|
||||
|
|
|
|||
|
|
@ -117,12 +117,12 @@ impl<'a> LintContext<'a> {
|
|||
}
|
||||
|
||||
pub fn diagnostic(&self, diagnostic: OxcDiagnostic) {
|
||||
self.add_diagnostic(Message::new(diagnostic.into(), None));
|
||||
self.add_diagnostic(Message::new(diagnostic, None));
|
||||
}
|
||||
|
||||
pub fn diagnostic_with_fix<F: FnOnce() -> Fix<'a>>(&self, diagnostic: OxcDiagnostic, fix: F) {
|
||||
if self.fix {
|
||||
self.add_diagnostic(Message::new(diagnostic.into(), Some(fix())));
|
||||
self.add_diagnostic(Message::new(diagnostic, Some(fix())));
|
||||
} else {
|
||||
self.diagnostic(diagnostic);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
use std::borrow::Cow;
|
||||
|
||||
use oxc_diagnostics::Error;
|
||||
use oxc_diagnostics::OxcDiagnostic;
|
||||
use oxc_span::Span;
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
|
|
@ -26,7 +26,7 @@ pub struct FixResult<'a> {
|
|||
}
|
||||
|
||||
pub struct Message<'a> {
|
||||
pub error: Error,
|
||||
pub error: OxcDiagnostic,
|
||||
start: u32,
|
||||
end: u32,
|
||||
pub fix: Option<Fix<'a>>,
|
||||
|
|
@ -35,14 +35,20 @@ pub struct Message<'a> {
|
|||
|
||||
impl<'a> Message<'a> {
|
||||
#[allow(clippy::cast_possible_truncation)] // for `as u32`
|
||||
pub fn new(error: Error, fix: Option<Fix<'a>>) -> Self {
|
||||
let labels = error.labels().map_or(vec![], Iterator::collect);
|
||||
let start =
|
||||
labels.iter().min_by_key(|span| span.offset()).map_or(0, |span| span.offset() as u32);
|
||||
let end = labels
|
||||
.iter()
|
||||
.max_by_key(|span| span.offset() + span.len())
|
||||
.map_or(0, |span| (span.offset() + span.len()) as u32);
|
||||
pub fn new(error: OxcDiagnostic, fix: Option<Fix<'a>>) -> Self {
|
||||
let (start, end) = if let Some(labels) = &error.labels {
|
||||
let start = labels
|
||||
.iter()
|
||||
.min_by_key(|span| span.offset())
|
||||
.map_or(0, |span| span.offset() as u32);
|
||||
let end = labels
|
||||
.iter()
|
||||
.max_by_key(|span| span.offset() + span.len())
|
||||
.map_or(0, |span| (span.offset() + span.len()) as u32);
|
||||
(start, end)
|
||||
} else {
|
||||
(0, 0)
|
||||
};
|
||||
Self { error, start, end, fix, fixed: false }
|
||||
}
|
||||
|
||||
|
|
@ -114,7 +120,7 @@ impl<'a> Fixer<'a> {
|
|||
mod test {
|
||||
use std::borrow::Cow;
|
||||
|
||||
use oxc_diagnostics::{Error, OxcDiagnostic};
|
||||
use oxc_diagnostics::OxcDiagnostic;
|
||||
use oxc_span::Span;
|
||||
|
||||
use super::{Fix, FixResult, Fixer, Message};
|
||||
|
|
@ -188,7 +194,7 @@ mod test {
|
|||
}
|
||||
|
||||
fn create_message(error: OxcDiagnostic, fix: Option<Fix>) -> Message {
|
||||
Message::new(Error::from(error), fix)
|
||||
Message::new(error, fix)
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -382,8 +388,8 @@ mod test {
|
|||
fn sort_no_fix_messages_correctly() {
|
||||
let result = get_fix_result(vec![
|
||||
create_message(replace_id(), Some(REPLACE_ID)),
|
||||
Message::new(no_fix_2(Span::new(1, 7)).into(), None),
|
||||
Message::new(no_fix_1(Span::new(1, 3)).into(), None),
|
||||
Message::new(no_fix_2(Span::new(1, 7)), None),
|
||||
Message::new(no_fix_1(Span::new(1, 3)), None),
|
||||
]);
|
||||
assert_eq!(result.fixed_code, TEST_CODE.replace("answer", "foo"));
|
||||
assert_eq!(result.messages.len(), 2);
|
||||
|
|
|
|||
|
|
@ -259,11 +259,7 @@ impl Runtime {
|
|||
.parse();
|
||||
|
||||
if !ret.errors.is_empty() {
|
||||
return ret
|
||||
.errors
|
||||
.into_iter()
|
||||
.map(|err| Message::new(Error::from(err), None))
|
||||
.collect();
|
||||
return ret.errors.into_iter().map(|err| Message::new(err, None)).collect();
|
||||
};
|
||||
|
||||
let program = allocator.alloc(ret.program);
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ use std::{cell::RefCell, path::PathBuf, rc::Rc, sync::Arc};
|
|||
|
||||
#[allow(clippy::wildcard_imports)]
|
||||
use oxc_ast::{ast::*, AstKind, Trivias, Visit};
|
||||
use oxc_diagnostics::{Error, OxcDiagnostic};
|
||||
use oxc_diagnostics::OxcDiagnostic;
|
||||
use oxc_span::{CompactStr, SourceType, Span};
|
||||
use oxc_syntax::{
|
||||
identifier::is_identifier_name,
|
||||
|
|
@ -38,7 +38,7 @@ pub struct SemanticBuilder<'a> {
|
|||
trivias: Rc<Trivias>,
|
||||
|
||||
/// Semantic early errors such as redeclaration errors.
|
||||
errors: RefCell<Vec<Error>>,
|
||||
errors: RefCell<Vec<OxcDiagnostic>>,
|
||||
|
||||
// states
|
||||
pub current_node_id: AstNodeId,
|
||||
|
|
@ -76,7 +76,7 @@ pub struct SemanticBuilder<'a> {
|
|||
|
||||
pub struct SemanticBuilderReturn<'a> {
|
||||
pub semantic: Semantic<'a>,
|
||||
pub errors: Vec<Error>,
|
||||
pub errors: Vec<OxcDiagnostic>,
|
||||
}
|
||||
|
||||
impl<'a> SemanticBuilder<'a> {
|
||||
|
|
@ -187,7 +187,7 @@ impl<'a> SemanticBuilder<'a> {
|
|||
|
||||
/// Push a Syntax Error
|
||||
pub fn error(&self, error: OxcDiagnostic) {
|
||||
self.errors.borrow_mut().push(error.into());
|
||||
self.errors.borrow_mut().push(error);
|
||||
}
|
||||
|
||||
fn create_ast_node(&mut self, kind: AstKind<'a>) {
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ use std::{path::PathBuf, sync::Arc};
|
|||
|
||||
use itertools::Itertools;
|
||||
use oxc_allocator::Allocator;
|
||||
use oxc_diagnostics::{Error, NamedSource};
|
||||
use oxc_diagnostics::{Error, NamedSource, OxcDiagnostic};
|
||||
use oxc_semantic::{print_basic_block, Semantic, SemanticBuilder};
|
||||
use oxc_span::SourceType;
|
||||
|
||||
|
|
@ -168,7 +168,7 @@ impl<'a> SemanticTester<'a> {
|
|||
SymbolTester::new_unique(self, self.build(), name)
|
||||
}
|
||||
|
||||
fn wrap_diagnostics(&self, diagnostics: Vec<Error>) -> Vec<Error> {
|
||||
fn wrap_diagnostics(&self, diagnostics: Vec<OxcDiagnostic>) -> Vec<Error> {
|
||||
let name = "test".to_owned()
|
||||
+ match (self.source_type.is_javascript(), self.source_type.is_jsx()) {
|
||||
(true, true) => ".jsx",
|
||||
|
|
|
|||
|
|
@ -184,7 +184,9 @@ impl Oxc {
|
|||
.build(program);
|
||||
|
||||
if run_options.syntax() {
|
||||
self.save_diagnostics(semantic_ret.errors);
|
||||
self.save_diagnostics(
|
||||
semantic_ret.errors.into_iter().map(Error::from).collect::<Vec<_>>(),
|
||||
);
|
||||
}
|
||||
|
||||
let semantic = Rc::new(semantic_ret.semantic);
|
||||
|
|
@ -192,7 +194,7 @@ impl Oxc {
|
|||
if run_options.lint() && self.diagnostics.borrow().is_empty() {
|
||||
let lint_ctx = LintContext::new(path.clone().into_boxed_path(), &semantic);
|
||||
let linter_ret = Linter::default().run(lint_ctx);
|
||||
let diagnostics = linter_ret.into_iter().map(|e| e.error).collect();
|
||||
let diagnostics = linter_ret.into_iter().map(|e| Error::from(e.error)).collect();
|
||||
self.save_diagnostics(diagnostics);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ use walkdir::WalkDir;
|
|||
|
||||
use oxc_allocator::Allocator;
|
||||
use oxc_ast::Trivias;
|
||||
use oxc_diagnostics::{Error, GraphicalReportHandler, GraphicalTheme, NamedSource};
|
||||
use oxc_diagnostics::{GraphicalReportHandler, GraphicalTheme, NamedSource};
|
||||
use oxc_parser::Parser;
|
||||
use oxc_semantic::SemanticBuilder;
|
||||
use oxc_span::{SourceType, Span};
|
||||
|
|
@ -334,12 +334,7 @@ pub trait Case: Sized + Sync + Send + UnwindSafe {
|
|||
if let Some(res) = self.check_semantic(&semantic_ret.semantic) {
|
||||
return res;
|
||||
}
|
||||
let errors = parser_ret
|
||||
.errors
|
||||
.into_iter()
|
||||
.map(Error::from)
|
||||
.chain(semantic_ret.errors)
|
||||
.collect::<Vec<_>>();
|
||||
let errors = parser_ret.errors.into_iter().chain(semantic_ret.errors).collect::<Vec<_>>();
|
||||
|
||||
let result = if errors.is_empty() {
|
||||
Ok(String::new())
|
||||
|
|
|
|||
Loading…
Reference in a new issue