Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(ast): refactor custom Serialize impls #3859

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions crates/oxc_ast/src/serialize.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use oxc_allocator::{Box, Vec};
use oxc_allocator::Box;
use oxc_span::Span;
use serde::{
ser::{SerializeSeq, Serializer},
Expand Down Expand Up @@ -80,7 +80,7 @@ struct SerArrayAssignmentTarget<'a, 'b> {
#[serde(flatten)]
span: Span,
elements:
ElementsAndRest<'a, 'b, Option<AssignmentTargetMaybeDefault<'a>>, AssignmentTargetRest<'a>>,
ElementsAndRest<'b, Option<AssignmentTargetMaybeDefault<'a>>, AssignmentTargetRest<'a>>,
}

impl<'a> Serialize for ObjectAssignmentTarget<'a> {
Expand All @@ -98,7 +98,7 @@ impl<'a> Serialize for ObjectAssignmentTarget<'a> {
struct SerObjectAssignmentTarget<'a, 'b> {
#[serde(flatten)]
span: Span,
properties: ElementsAndRest<'a, 'b, AssignmentTargetProperty<'a>, AssignmentTargetRest<'a>>,
properties: ElementsAndRest<'b, AssignmentTargetProperty<'a>, AssignmentTargetRest<'a>>,
}

impl<'a> Serialize for ObjectPattern<'a> {
Expand All @@ -116,7 +116,7 @@ impl<'a> Serialize for ObjectPattern<'a> {
struct SerObjectPattern<'a, 'b> {
#[serde(flatten)]
span: Span,
properties: ElementsAndRest<'a, 'b, BindingProperty<'a>, Box<'a, BindingRestElement<'a>>>,
properties: ElementsAndRest<'b, BindingProperty<'a>, Box<'a, BindingRestElement<'a>>>,
}

impl<'a> Serialize for ArrayPattern<'a> {
Expand All @@ -134,7 +134,7 @@ impl<'a> Serialize for ArrayPattern<'a> {
struct SerArrayPattern<'a, 'b> {
#[serde(flatten)]
span: Span,
elements: ElementsAndRest<'a, 'b, Option<BindingPattern<'a>>, Box<'a, BindingRestElement<'a>>>,
elements: ElementsAndRest<'b, Option<BindingPattern<'a>>, Box<'a, BindingRestElement<'a>>>,
}

/// Serialize `FormalParameters`, to be estree compatible, with `items` and `rest` fields combined
Expand Down Expand Up @@ -162,7 +162,7 @@ struct SerFormalParameters<'a, 'b> {
#[serde(flatten)]
span: Span,
kind: FormalParameterKind,
items: ElementsAndRest<'a, 'b, FormalParameter<'a>, SerFormalParameterRest<'a, 'b>>,
items: ElementsAndRest<'b, FormalParameter<'a>, SerFormalParameterRest<'a, 'b>>,
}

#[derive(Serialize)]
Expand All @@ -175,18 +175,18 @@ struct SerFormalParameterRest<'a, 'b> {
optional: bool,
}

pub struct ElementsAndRest<'a, 'b, E, R> {
elements: &'b Vec<'a, E>,
pub struct ElementsAndRest<'b, E, R> {
elements: &'b [E],
rest: &'b Option<R>,
}

impl<'a, 'b, E, R> ElementsAndRest<'a, 'b, E, R> {
pub fn new(elements: &'b Vec<'a, E>, rest: &'b Option<R>) -> Self {
impl<'b, E, R> ElementsAndRest<'b, E, R> {
pub fn new(elements: &'b [E], rest: &'b Option<R>) -> Self {
Self { elements, rest }
}
}

impl<'a, 'b, E: Serialize, R: Serialize> Serialize for ElementsAndRest<'a, 'b, E, R> {
impl<'b, E: Serialize, R: Serialize> Serialize for ElementsAndRest<'b, E, R> {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
if let Some(rest) = self.rest {
let mut seq = serializer.serialize_seq(Some(self.elements.len() + 1))?;
Expand Down
Loading