From bb18132da5bb6d104d7d952948cc345a7e58efac Mon Sep 17 00:00:00 2001 From: Boshen Date: Mon, 10 Apr 2023 22:21:05 +0800 Subject: [PATCH] refactor(oxc_diagnostics): remove error declarations Make this crate not dependent on `oxc_ast` --- Cargo.lock | 1 - crates/oxc_cli/src/lint/runner.rs | 11 ++++++++++- crates/oxc_diagnostics/Cargo.toml | 2 -- crates/oxc_diagnostics/src/lib.rs | 19 ------------------- crates/oxc_parser/src/js/list.rs | 15 ++++++++++++++- crates/oxc_semantic/src/builder.rs | 3 ++- crates/oxc_semantic/src/checker/javascript.rs | 4 +--- crates/oxc_semantic/src/diagnostics.rs | 14 ++++++++++++++ crates/oxc_semantic/src/lib.rs | 1 + 9 files changed, 42 insertions(+), 28 deletions(-) create mode 100644 crates/oxc_semantic/src/diagnostics.rs diff --git a/Cargo.lock b/Cargo.lock index 8ef6750da..9c474426f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -974,7 +974,6 @@ dependencies = [ "is-terminal", "miette", "owo-colors", - "oxc_ast", "textwrap 0.16.0", "thiserror", "unicode-width", diff --git a/crates/oxc_cli/src/lint/runner.rs b/crates/oxc_cli/src/lint/runner.rs index df5591fef..7d5a0f90a 100644 --- a/crates/oxc_cli/src/lint/runner.rs +++ b/crates/oxc_cli/src/lint/runner.rs @@ -12,7 +12,11 @@ use std::{ use miette::NamedSource; use oxc_allocator::Allocator; use oxc_ast::SourceType; -use oxc_diagnostics::{Error, GraphicalReportHandler, MinifiedFileError, Severity}; +use oxc_diagnostics::{ + miette::{self, Diagnostic}, + thiserror::Error, + Error, GraphicalReportHandler, Severity, +}; use oxc_linter::{Fixer, Linter, RuleCategory, RuleEnum, RULES}; use oxc_parser::Parser; use oxc_semantic::SemanticBuilder; @@ -27,6 +31,11 @@ pub struct LintRunner { linter: Arc, } +#[derive(Debug, Error, Diagnostic)] +#[error("File is too long to fit on the screen")] +#[diagnostic(help("{0:?} seems like a minified file"))] +pub struct MinifiedFileError(pub PathBuf); + impl LintRunner { #[must_use] pub fn new(options: LintOptions) -> Self { diff --git a/crates/oxc_diagnostics/Cargo.toml b/crates/oxc_diagnostics/Cargo.toml index 1484ff2b7..7b8b624d7 100644 --- a/crates/oxc_diagnostics/Cargo.toml +++ b/crates/oxc_diagnostics/Cargo.toml @@ -10,8 +10,6 @@ license.workspace = true repository.workspace = true [dependencies] -oxc_ast = { workspace = true } - thiserror = { workspace = true } miette = { workspace = true } diff --git a/crates/oxc_diagnostics/src/lib.rs b/crates/oxc_diagnostics/src/lib.rs index 7c5776f1f..f753b04fe 100644 --- a/crates/oxc_diagnostics/src/lib.rs +++ b/crates/oxc_diagnostics/src/lib.rs @@ -4,31 +4,12 @@ mod graphic_reporter; mod graphical_theme; -use std::path::PathBuf; - pub use graphic_reporter::GraphicalReportHandler; pub use miette; -use miette::Diagnostic; -use oxc_ast::{Atom, Span}; pub use thiserror; -use thiserror::Error; pub type Error = miette::Error; pub type Severity = miette::Severity; pub type Report = miette::Report; pub type Result = std::result::Result; - -#[derive(Debug, Error, Diagnostic)] -#[error("Identifier `{0}` has already been declared")] -#[diagnostic()] -pub struct Redeclaration( - pub Atom, - #[label("`{0}` has already been declared here")] pub Span, - #[label("It can not be redeclared here")] pub Span, -); - -#[derive(Debug, Error, Diagnostic)] -#[error("File is too long to fit on the screen")] -#[diagnostic(help("{0:?} seems like a minified file"))] -pub struct MinifiedFileError(pub PathBuf); diff --git a/crates/oxc_parser/src/js/list.rs b/crates/oxc_parser/src/js/list.rs index 2bbcf8b60..c6572e4e4 100644 --- a/crates/oxc_parser/src/js/list.rs +++ b/crates/oxc_parser/src/js/list.rs @@ -1,6 +1,10 @@ use oxc_allocator::Vec; use oxc_ast::{ast::*, syntax_directed_operations::PrivateBoundIdentifiers, Atom, GetSpan, Span}; -use oxc_diagnostics::{Redeclaration, Result}; +use oxc_diagnostics::{ + miette::{self, Diagnostic}, + thiserror::{self, Error}, + Result, +}; use rustc_hash::FxHashMap; use crate::diagnostics; @@ -8,6 +12,15 @@ use crate::lexer::Kind; use crate::list::{NormalList, SeparatedList}; use crate::Parser; +#[derive(Debug, Error, Diagnostic)] +#[error("Identifier `{0}` has already been declared")] +#[diagnostic()] +struct Redeclaration( + pub Atom, + #[label("`{0}` has already been declared here")] pub Span, + #[label("It can not be redeclared here")] pub Span, +); + /// ObjectExpression.properties pub struct ObjectExpressionProperties<'a> { pub elements: Vec<'a, ObjectProperty<'a>>, diff --git a/crates/oxc_semantic/src/builder.rs b/crates/oxc_semantic/src/builder.rs index 5c6d7af55..22b14bf50 100644 --- a/crates/oxc_semantic/src/builder.rs +++ b/crates/oxc_semantic/src/builder.rs @@ -9,11 +9,12 @@ use oxc_ast::{ ast::*, module_record::ModuleRecord, visit::Visit, AstKind, Atom, GetSpan, SourceType, Span, Trivias, }; -use oxc_diagnostics::{Error, Redeclaration}; +use oxc_diagnostics::Error; use crate::{ binder::Binder, checker::EarlyErrorJavaScript, + diagnostics::Redeclaration, module_record::ModuleRecordBuilder, node::{AstNodeId, AstNodes, NodeFlags, SemanticNode}, scope::{ScopeBuilder, ScopeId}, diff --git a/crates/oxc_semantic/src/checker/javascript.rs b/crates/oxc_semantic/src/checker/javascript.rs index 370b6cc00..5e54c4aae 100644 --- a/crates/oxc_semantic/src/checker/javascript.rs +++ b/crates/oxc_semantic/src/checker/javascript.rs @@ -8,13 +8,11 @@ use oxc_ast::{ use oxc_diagnostics::{ miette::{self, Diagnostic}, thiserror::{self, Error}, - Redeclaration, }; use phf::{phf_set, Set}; use rustc_hash::FxHashMap; -use crate::scope::ScopeFlags; -use crate::{builder::SemanticBuilder, AstNode}; +use crate::{builder::SemanticBuilder, diagnostics::Redeclaration, scope::ScopeFlags, AstNode}; pub struct EarlyErrorJavaScript; diff --git a/crates/oxc_semantic/src/diagnostics.rs b/crates/oxc_semantic/src/diagnostics.rs new file mode 100644 index 000000000..fd22fb916 --- /dev/null +++ b/crates/oxc_semantic/src/diagnostics.rs @@ -0,0 +1,14 @@ +use oxc_ast::{Atom, Span}; +use oxc_diagnostics::{ + miette::{self, Diagnostic}, + thiserror::{self, Error}, +}; + +#[derive(Debug, Error, Diagnostic)] +#[error("Identifier `{0}` has already been declared")] +#[diagnostic()] +pub struct Redeclaration( + pub Atom, + #[label("`{0}` has already been declared here")] pub Span, + #[label("It can not be redeclared here")] pub Span, +); diff --git a/crates/oxc_semantic/src/lib.rs b/crates/oxc_semantic/src/lib.rs index 5d9288a10..cd385b104 100644 --- a/crates/oxc_semantic/src/lib.rs +++ b/crates/oxc_semantic/src/lib.rs @@ -4,6 +4,7 @@ mod binder; mod builder; mod checker; +mod diagnostics; mod module_record; mod node; mod scope;