Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 5 additions & 1 deletion crates/oxc_ast/src/ast/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ pub struct Program<'a> {
#[estree(skip)]
pub comments: Vec<'a, Comment>,
pub hashbang: Option<Hashbang<'a>>,
#[estree(rename = "body")]
pub directives: Vec<'a, Directive<'a>>,
#[estree(append_to = "directives")]
pub body: Vec<'a, Statement<'a>>,
pub scope_id: Cell<Option<ScopeId>>,
}
Expand Down Expand Up @@ -1012,6 +1014,7 @@ pub enum Statement<'a> {
#[ast(visit)]
#[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ESTree)]
#[estree(rename = "ExpressionStatement")]
pub struct Directive<'a> {
pub span: Span,
/// Directive with any escapes unescaped
Expand Down Expand Up @@ -1710,8 +1713,9 @@ pub enum FormalParameterKind {
#[estree(rename = "BlockStatement")]
pub struct FunctionBody<'a> {
pub span: Span,
pub directives: Vec<'a, Directive<'a>>,
#[estree(rename = "body")]
pub directives: Vec<'a, Directive<'a>>,
#[estree(append_to = "directives")]
pub statements: Vec<'a, Statement<'a>>,
}

Expand Down
16 changes: 10 additions & 6 deletions crates/oxc_ast/src/generated/derive_estree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use serde::{__private::ser::FlatMapSerializer, ser::SerializeMap, Serialize, Serializer};

use oxc_estree::ser::AppendTo;
use oxc_estree::ser::{AppendTo, AppendToConcat};

use crate::ast::js::*;
use crate::ast::jsx::*;
Expand All @@ -20,8 +20,10 @@ impl Serialize for Program<'_> {
map.serialize_entry("end", &self.span.end)?;
self.source_type.serialize(FlatMapSerializer(&mut map))?;
map.serialize_entry("hashbang", &self.hashbang)?;
map.serialize_entry("directives", &self.directives)?;
map.serialize_entry("body", &self.body)?;
map.serialize_entry(
"body",
&AppendToConcat { array: &self.directives, after: &self.body },
)?;
map.end()
}
}
Expand Down Expand Up @@ -837,7 +839,7 @@ impl Serialize for Statement<'_> {
impl Serialize for Directive<'_> {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let mut map = serializer.serialize_map(None)?;
map.serialize_entry("type", "Directive")?;
map.serialize_entry("type", "ExpressionStatement")?;
map.serialize_entry("start", &self.span.start)?;
map.serialize_entry("end", &self.span.end)?;
map.serialize_entry("expression", &self.expression)?;
Expand Down Expand Up @@ -1408,8 +1410,10 @@ impl Serialize for FunctionBody<'_> {
map.serialize_entry("type", "BlockStatement")?;
map.serialize_entry("start", &self.span.start)?;
map.serialize_entry("end", &self.span.end)?;
map.serialize_entry("directives", &self.directives)?;
map.serialize_entry("body", &self.statements)?;
map.serialize_entry(
"body",
&AppendToConcat { array: &self.directives, after: &self.statements },
)?;
map.end()
}
}
Expand Down
18 changes: 18 additions & 0 deletions crates/oxc_estree/src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,21 @@ impl<TVec: Serialize, TAfter: Serialize> Serialize for AppendTo<'_, TVec, TAfter
}
}
}

pub struct AppendToConcat<'a, TVec, TAfter> {
pub array: &'a [TVec],
pub after: &'a [TAfter],
}

impl<TVec: Serialize, TAfter: Serialize> Serialize for AppendToConcat<'_, TVec, TAfter> {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let mut seq = serializer.serialize_seq(Some(self.array.len() + self.after.len()))?;
for element in self.array {
seq.serialize_element(element)?;
}
for element in self.after {
seq.serialize_element(element)?;
}
seq.end()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use serde::{__private::ser::FlatMapSerializer, ser::SerializeMap, Serialize, Serializer};

use oxc_estree::ser::AppendTo;
use oxc_estree::ser::{AppendTo, AppendToConcat};

use crate::ast::*;

Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_span/src/generated/derive_estree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use serde::{__private::ser::FlatMapSerializer, ser::SerializeMap, Serialize, Serializer};

use oxc_estree::ser::AppendTo;
use oxc_estree::ser::{AppendTo, AppendToConcat};

use crate::source_type::*;
use crate::span::*;
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_syntax/src/generated/derive_estree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use serde::{__private::ser::FlatMapSerializer, ser::SerializeMap, Serialize, Serializer};

use oxc_estree::ser::AppendTo;
use oxc_estree::ser::{AppendTo, AppendToConcat};

use crate::operator::*;

Expand Down
8 changes: 3 additions & 5 deletions npm/oxc-types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ export interface Program extends Span {
type: 'Program';
sourceType: ModuleKind;
hashbang: Hashbang | null;
directives: Array<Directive>;
body: Array<Statement>;
body: Array<Directive | Statement>;
}

export type Expression =
Expand Down Expand Up @@ -330,7 +329,7 @@ export type Statement =
| ModuleDeclaration;

export interface Directive extends Span {
type: 'Directive';
type: 'ExpressionStatement';
expression: StringLiteral;
directive: string;
}
Expand Down Expand Up @@ -583,8 +582,7 @@ export type FormalParameterKind = 'FormalParameter' | 'UniqueFormalParameters' |

export interface FunctionBody extends Span {
type: 'BlockStatement';
directives: Array<Directive>;
body: Array<Statement>;
body: Array<Directive | Statement>;
}

export interface ArrowFunctionExpression extends Span {
Expand Down
14 changes: 10 additions & 4 deletions tasks/ast_tools/src/derives/estree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl Derive for DeriveESTree {
};

///@@line_break
use oxc_estree::ser::AppendTo;
use oxc_estree::ser::{AppendTo, AppendToConcat};
}
}

Expand Down Expand Up @@ -300,10 +300,16 @@ impl<'s> StructSerializerGenerator<'s> {
let via_ty = parse_str::<Type>(via_str).unwrap();
value = quote!( #via_ty::from(&#value) );
} else if let Some(append_field_index) = field.estree.append_field_index {
let append_from_ident = struct_def.fields[append_field_index].ident();
value = quote! {
AppendTo { array: &#value, after: &#self_path.#append_from_ident }
let append_field = &struct_def.fields[append_field_index];
let append_from_ident = append_field.ident();
let wrapper = if append_field.type_def(self.schema).is_option() {
quote! { AppendTo }
} else {
quote! { AppendToConcat }
};
value = quote! {
#wrapper { array: &#value, after: &#self_path.#append_from_ident }
}
}

self.stmts.extend(quote! {
Expand Down
15 changes: 12 additions & 3 deletions tasks/ast_tools/src/generators/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,18 @@ fn generate_ts_type_def_for_struct_field<'s>(
schema: &'s Schema,
) {
let field_type_name = if let Some(append_field_index) = field.estree.append_field_index {
let appended_field = struct_def.fields[append_field_index].type_def(schema);
let appended_field = appended_field.as_option().unwrap();
let appended_type_name = ts_type_name(appended_field.inner_type(schema), schema);
let appended_field = &struct_def.fields[append_field_index];
let appended_type = appended_field.type_def(schema);
let appended_type = match appended_type {
TypeDef::Option(option_def) => option_def.inner_type(schema),
TypeDef::Vec(vec_def) => vec_def.inner_type(schema),
_ => panic!(
"Appended field must be `Option<T>` or `Vec<T>`: `{}::{}`",
struct_def.name(),
appended_field.name()
),
};
let appended_type_name = ts_type_name(appended_type, schema);

let field_type = field.type_def(schema);
let (vec_def, is_option) = match field_type {
Expand Down
1 change: 0 additions & 1 deletion tasks/ast_tools/src/schema/defs/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ impl TypeDef {
}
}

#[expect(dead_code)]
pub fn is_option(&self) -> bool {
matches!(self, Self::Option(_))
}
Expand Down
Loading
Loading