Skip to content

Commit

Permalink
refactor(diagnostic): 1 diagnostic
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed May 10, 2024
1 parent 9525653 commit 1f580c0
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 5 deletions.
55 changes: 53 additions & 2 deletions crates/oxc_diagnostics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ mod graphical_theme;
mod reporter;
mod service;

use std::path::PathBuf;
use std::{
fmt::{self, Display},
path::PathBuf,
};

pub use miette;
pub use thiserror;
Expand All @@ -23,7 +26,7 @@ pub type Report = miette::Report;

pub type Result<T> = std::result::Result<T, Error>;

use miette::Diagnostic;
use miette::{Diagnostic, LabeledSpan, SourceCode};
use thiserror::Error;

#[derive(Debug, Error, Diagnostic)]
Expand All @@ -35,3 +38,51 @@ pub struct MinifiedFileError(pub PathBuf);
#[error("Failed to open file {0:?} with error \"{1}\"")]
#[diagnostic(help("Failed to open file {0:?} with error \"{1}\""))]
pub struct FailedToOpenFileError(pub PathBuf, pub std::io::Error);

#[derive(Debug, Default, Error)]
pub struct OxcDiagnostic {
pub message: &'static str,
pub help: Option<&'static str>,
pub severity: Option<Severity>,
pub labels: Vec<LabeledSpan>,
}

impl Display for OxcDiagnostic {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.message)
}
}

impl Diagnostic for OxcDiagnostic {
fn code<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
None
}

fn severity(&self) -> Option<Severity> {
Some(self.severity.unwrap_or(Severity::Warning))
}

fn help<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
self.help.map(|s| -> Box<dyn Display + 'a> { Box::new(s.to_string()) })
}

fn url<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
None
}

fn source_code(&self) -> Option<&dyn SourceCode> {
None
}

fn labels(&self) -> Option<Box<dyn Iterator<Item = LabeledSpan> + '_>> {
Some(Box::new(self.labels.iter().map(Clone::clone)))
}

fn related<'a>(&'a self) -> Option<Box<dyn Iterator<Item = &'a dyn Diagnostic> + 'a>> {
None
}

fn diagnostic_source(&self) -> Option<&dyn Diagnostic> {
None
}
}
10 changes: 8 additions & 2 deletions crates/oxc_parser/src/cursor.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Code related to navigating `Token`s from the lexer

use oxc_ast::ast::RegExpFlags;
use oxc_diagnostics::Result;
use oxc_diagnostics::{OxcDiagnostic, Result};
use oxc_span::Span;

use crate::{
Expand Down Expand Up @@ -158,7 +158,13 @@ impl<'a> ParserImpl<'a> {
pub(crate) fn asi(&mut self) -> Result<()> {
if !self.can_insert_semicolon() {
let span = Span::new(self.prev_token_end, self.cur_token().start);
return Err(diagnostics::AutoSemicolonInsertion(span).into());
let diagnostic = OxcDiagnostic {
message: "Expected a semicolon or an implicit semicolon after a statement, but found none",
help: None,
severity: None,
labels: vec![span.into()]
};
return Err(diagnostic.into());
}
if self.at(Kind::Semicolon) {
self.advance(Kind::Semicolon);
Expand Down
8 changes: 7 additions & 1 deletion crates/oxc_span/src/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use std::hash::{Hash, Hasher};

use miette::{SourceOffset, SourceSpan};
use miette::{LabeledSpan, SourceOffset, SourceSpan};

#[cfg(feature = "serialize")]
use serde::Serialize;
Expand Down Expand Up @@ -59,6 +59,12 @@ impl From<Span> for SourceSpan {
}
}

impl From<Span> for LabeledSpan {
fn from(val: Span) -> Self {
LabeledSpan::underline(val)
}
}

/// Get the span for an AST node
pub trait GetSpan {
fn span(&self) -> Span;
Expand Down

0 comments on commit 1f580c0

Please sign in to comment.