From 893af230c0a572012eae57562373b1517092d7ac Mon Sep 17 00:00:00 2001 From: Boshen Date: Sun, 12 May 2024 21:40:44 +0800 Subject: [PATCH] refactor: clean up more diagnostics usages --- crates/oxc_diagnostics/src/service.rs | 2 +- crates/oxc_language_server/src/linter.rs | 7 +++-- crates/oxc_linter/src/context.rs | 4 +-- crates/oxc_linter/src/fixer.rs | 34 ++++++++++++++---------- crates/oxc_linter/src/service.rs | 6 +---- crates/oxc_semantic/src/builder.rs | 8 +++--- crates/oxc_semantic/tests/util/mod.rs | 4 +-- crates/oxc_wasm/src/lib.rs | 6 +++-- tasks/coverage/src/suite.rs | 9 ++----- 9 files changed, 41 insertions(+), 39 deletions(-) diff --git a/crates/oxc_diagnostics/src/service.rs b/crates/oxc_diagnostics/src/service.rs index e8da236f2..c3222cb07 100644 --- a/crates/oxc_diagnostics/src/service.rs +++ b/crates/oxc_diagnostics/src/service.rs @@ -99,7 +99,7 @@ impl DiagnosticService { pub fn wrap_diagnostics( path: &Path, source_text: &str, - diagnostics: Vec, + diagnostics: Vec, ) -> (PathBuf, Vec) { let source = Arc::new(NamedSource::new(path.to_string_lossy(), source_text.to_owned())); let diagnostics = diagnostics diff --git a/crates/oxc_language_server/src/linter.rs b/crates/oxc_language_server/src/linter.rs index 03ee501b8..76e839780 100644 --- a/crates/oxc_language_server/src/linter.rs +++ b/crates/oxc_language_server/src/linter.rs @@ -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::>(); let (_, errors_with_position) = diff --git a/crates/oxc_linter/src/context.rs b/crates/oxc_linter/src/context.rs index 747982263..4008e1fa3 100644 --- a/crates/oxc_linter/src/context.rs +++ b/crates/oxc_linter/src/context.rs @@ -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 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); } diff --git a/crates/oxc_linter/src/fixer.rs b/crates/oxc_linter/src/fixer.rs index d80cd060d..b94395ee7 100644 --- a/crates/oxc_linter/src/fixer.rs +++ b/crates/oxc_linter/src/fixer.rs @@ -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>, @@ -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>) -> 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>) -> 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) -> 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); diff --git a/crates/oxc_linter/src/service.rs b/crates/oxc_linter/src/service.rs index 986ce55f2..b98ae4f89 100644 --- a/crates/oxc_linter/src/service.rs +++ b/crates/oxc_linter/src/service.rs @@ -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); diff --git a/crates/oxc_semantic/src/builder.rs b/crates/oxc_semantic/src/builder.rs index 93fc76e1f..3089938da 100644 --- a/crates/oxc_semantic/src/builder.rs +++ b/crates/oxc_semantic/src/builder.rs @@ -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, /// Semantic early errors such as redeclaration errors. - errors: RefCell>, + errors: RefCell>, // 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, + pub errors: Vec, } 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>) { diff --git a/crates/oxc_semantic/tests/util/mod.rs b/crates/oxc_semantic/tests/util/mod.rs index 0152dc6fb..8154e892a 100644 --- a/crates/oxc_semantic/tests/util/mod.rs +++ b/crates/oxc_semantic/tests/util/mod.rs @@ -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) -> Vec { + fn wrap_diagnostics(&self, diagnostics: Vec) -> Vec { let name = "test".to_owned() + match (self.source_type.is_javascript(), self.source_type.is_jsx()) { (true, true) => ".jsx", diff --git a/crates/oxc_wasm/src/lib.rs b/crates/oxc_wasm/src/lib.rs index 5931022f8..9f4e86434 100644 --- a/crates/oxc_wasm/src/lib.rs +++ b/crates/oxc_wasm/src/lib.rs @@ -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::>(), + ); } 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); } diff --git a/tasks/coverage/src/suite.rs b/tasks/coverage/src/suite.rs index ce3c89af3..dd35f5935 100644 --- a/tasks/coverage/src/suite.rs +++ b/tasks/coverage/src/suite.rs @@ -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::>(); + let errors = parser_ret.errors.into_iter().chain(semantic_ret.errors).collect::>(); let result = if errors.is_empty() { Ok(String::new())