diff --git a/.github/generated/ast_changes_watch_list.yml b/.github/generated/ast_changes_watch_list.yml index 6f7e577b0b832..4d1aa278c1e82 100644 --- a/.github/generated/ast_changes_watch_list.yml +++ b/.github/generated/ast_changes_watch_list.yml @@ -40,7 +40,7 @@ src: - 'crates/oxc_span/src/generated/assert_layouts.rs' - 'crates/oxc_span/src/generated/derive_dummy.rs' - 'crates/oxc_span/src/generated/derive_estree.rs' - - 'crates/oxc_span/src/source_type/mod.rs' + - 'crates/oxc_span/src/source_type.rs' - 'crates/oxc_span/src/span.rs' - 'crates/oxc_syntax/src/generated/assert_layouts.rs' - 'crates/oxc_syntax/src/generated/derive_clone_in.rs' diff --git a/crates/oxc_span/src/source_type/mod.rs b/crates/oxc_span/src/source_type.rs similarity index 96% rename from crates/oxc_span/src/source_type/mod.rs rename to crates/oxc_span/src/source_type.rs index a9bca75b6e937..9963f0b02639c 100644 --- a/crates/oxc_span/src/source_type/mod.rs +++ b/crates/oxc_span/src/source_type.rs @@ -1,4 +1,10 @@ -use std::{hash::Hash, path::Path}; +use std::{ + borrow::Cow, + error::Error, + fmt::{self, Display}, + ops::Deref, + path::Path, +}; use oxc_allocator::{Allocator, CloneIn, Dummy}; use oxc_ast_macros::ast; @@ -6,9 +12,6 @@ use oxc_estree::ESTree; use crate::ContentEq; -mod error; -pub use error::UnknownExtension; - /// Source Type for JavaScript vs TypeScript / Script vs Module / JSX #[ast] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] @@ -551,6 +554,33 @@ impl SourceType { } } +/// Error returned by [`SourceType::from_path`] and [`SourceType::from_extension`] when +/// the file extension is not found or recognized. +#[derive(Debug)] +pub struct UnknownExtension(Cow<'static, str>); + +impl UnknownExtension { + fn new>>(ext: S) -> Self { + Self(ext.into()) + } +} + +impl Deref for UnknownExtension { + type Target = str; + + fn deref(&self) -> &str { + &self.0 + } +} + +impl Display for UnknownExtension { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "Unknown file extension: {}", self.0) + } +} + +impl Error for UnknownExtension {} + #[cfg(test)] mod tests { use super::SourceType; diff --git a/crates/oxc_span/src/source_type/error.rs b/crates/oxc_span/src/source_type/error.rs deleted file mode 100644 index 69aa693ce72d4..0000000000000 --- a/crates/oxc_span/src/source_type/error.rs +++ /dev/null @@ -1,30 +0,0 @@ -use std::{borrow::Cow, error::Error, fmt, ops::Deref}; - -/// Error returned by [`SourceType::from_path`] when the file extension is not -/// found or recognized. -/// -/// [`SourceType::from_path`]: `crate::SourceType::from_path` -#[derive(Debug)] -pub struct UnknownExtension(/* msg */ pub(crate) Cow<'static, str>); - -impl Deref for UnknownExtension { - type Target = str; - - fn deref(&self) -> &str { - &self.0 - } -} -impl UnknownExtension { - #[inline] - pub(crate) fn new>>(ext: S) -> Self { - Self(ext.into()) - } -} - -impl fmt::Display for UnknownExtension { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "Unknown file extension: {}", self.0) - } -} - -impl Error for UnknownExtension {} diff --git a/tasks/ast_tools/src/main.rs b/tasks/ast_tools/src/main.rs index b7049bdb42143..fdc7aac611995 100644 --- a/tasks/ast_tools/src/main.rs +++ b/tasks/ast_tools/src/main.rs @@ -227,7 +227,7 @@ static SOURCE_PATHS: &[&str] = &[ "crates/oxc_syntax/src/symbol.rs", "crates/oxc_syntax/src/reference.rs", "crates/oxc_span/src/span.rs", - "crates/oxc_span/src/source_type/mod.rs", + "crates/oxc_span/src/source_type.rs", "crates/oxc_regular_expression/src/ast.rs", "napi/parser/src/raw_transfer_types.rs", ];