feat(span): implement CloneIn for the AST-related items. (#4729)

Follow-on after #4276, related to #4284.
This commit is contained in:
rzvxa 2024-08-07 17:28:54 +00:00
parent 23b0040c16
commit 2e63618462
3 changed files with 27 additions and 1 deletions

View file

@ -9,7 +9,7 @@ use compact_str::CompactString;
use serde::{Serialize, Serializer};
use crate::Span;
use oxc_allocator::{Allocator, FromIn};
use oxc_allocator::{Allocator, CloneIn, FromIn};
#[cfg(feature = "serialize")]
#[wasm_bindgen::prelude::wasm_bindgen(typescript_custom_section)]
@ -59,6 +59,14 @@ impl<'a> Atom<'a> {
}
}
impl<'old_alloc, 'new_alloc> CloneIn<'new_alloc> for Atom<'old_alloc> {
type Cloned = Atom<'new_alloc>;
fn clone_in(&self, alloc: &'new_alloc Allocator) -> Self::Cloned {
Atom::from_in(self.as_str(), alloc)
}
}
impl<'a, 'b> FromIn<'a, &'b Atom<'a>> for Atom<'a> {
fn from_in(s: &'b Atom<'a>, _: &'a Allocator) -> Self {
Self::from(s.0)

View file

@ -1,6 +1,7 @@
use std::path::Path;
mod types;
use oxc_allocator::{Allocator, CloneIn};
pub use types::*;
#[derive(Debug)]
@ -17,6 +18,14 @@ impl Default for SourceType {
}
}
impl<'a> CloneIn<'a> for SourceType {
type Cloned = Self;
#[inline]
fn clone_in(&self, _: &'a Allocator) -> Self {
*self
}
}
/// Valid file extensions
pub const VALID_EXTENSIONS: [&str; 8] = ["js", "mjs", "cjs", "jsx", "ts", "mts", "cts", "tsx"];

View file

@ -6,6 +6,7 @@ use std::{
use miette::{LabeledSpan, SourceOffset, SourceSpan};
mod types;
use oxc_allocator::{Allocator, CloneIn};
pub use types::Span;
/// An Empty span useful for creating AST nodes.
@ -338,6 +339,14 @@ impl GetSpanMut for Span {
}
}
impl<'a> CloneIn<'a> for Span {
type Cloned = Self;
#[inline]
fn clone_in(&self, _: &'a Allocator) -> Self {
*self
}
}
#[cfg(test)]
mod test {
use super::Span;