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
4 changes: 4 additions & 0 deletions crates/oxc_ast/src/ast/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ pub use match_expression;
#[estree(rename = "Identifier")]
pub struct IdentifierName<'a> {
pub span: Span,
#[estree(json_safe)]
pub name: Atom<'a>,
}

Expand All @@ -224,6 +225,7 @@ pub struct IdentifierName<'a> {
pub struct IdentifierReference<'a> {
pub span: Span,
/// The name of the identifier being referenced.
#[estree(json_safe)]
pub name: Atom<'a>,
/// Reference ID
///
Expand All @@ -246,6 +248,7 @@ pub struct IdentifierReference<'a> {
pub struct BindingIdentifier<'a> {
pub span: Span,
/// The identifier name being bound.
#[estree(json_safe)]
pub name: Atom<'a>,
/// Unique identifier for this binding.
///
Expand All @@ -267,6 +270,7 @@ pub struct BindingIdentifier<'a> {
#[estree(rename = "Identifier")]
pub struct LabelIdentifier<'a> {
pub span: Span,
#[estree(json_safe)]
pub name: Atom<'a>,
}

Expand Down
1 change: 1 addition & 0 deletions crates/oxc_ast/src/ast/jsx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ pub struct JSXIdentifier<'a> {
/// Node location in source code
pub span: Span,
/// The name of the identifier.
#[estree(json_safe)]
pub name: Atom<'a>,
}

Expand Down
1 change: 1 addition & 0 deletions crates/oxc_ast/src/ast/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,7 @@ pub struct TSConstructSignatureDeclaration<'a> {
#[estree(rename = "Identifier")]
pub struct TSIndexSignatureName<'a> {
pub span: Span,
#[estree(json_safe)]
pub name: Atom<'a>,
pub type_annotation: Box<'a, TSTypeAnnotation<'a>>,
}
Expand Down
12 changes: 6 additions & 6 deletions crates/oxc_ast/src/generated/derive_estree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl ESTree for IdentifierName<'_> {
state.serialize_field("type", &JsonSafeString("Identifier"));
state.serialize_field("start", &self.span.start);
state.serialize_field("end", &self.span.end);
state.serialize_field("name", &self.name);
state.serialize_field("name", &JsonSafeString(self.name.as_str()));
state.end();
}
}
Expand All @@ -95,7 +95,7 @@ impl ESTree for IdentifierReference<'_> {
state.serialize_field("type", &JsonSafeString("Identifier"));
state.serialize_field("start", &self.span.start);
state.serialize_field("end", &self.span.end);
state.serialize_field("name", &self.name);
state.serialize_field("name", &JsonSafeString(self.name.as_str()));
state.end();
}
}
Expand All @@ -106,7 +106,7 @@ impl ESTree for BindingIdentifier<'_> {
state.serialize_field("type", &JsonSafeString("Identifier"));
state.serialize_field("start", &self.span.start);
state.serialize_field("end", &self.span.end);
state.serialize_field("name", &self.name);
state.serialize_field("name", &JsonSafeString(self.name.as_str()));
state.end();
}
}
Expand All @@ -117,7 +117,7 @@ impl ESTree for LabelIdentifier<'_> {
state.serialize_field("type", &JsonSafeString("Identifier"));
state.serialize_field("start", &self.span.start);
state.serialize_field("end", &self.span.end);
state.serialize_field("name", &self.name);
state.serialize_field("name", &JsonSafeString(self.name.as_str()));
state.end();
}
}
Expand Down Expand Up @@ -2156,7 +2156,7 @@ impl ESTree for JSXIdentifier<'_> {
state.serialize_field("type", &JsonSafeString("JSXIdentifier"));
state.serialize_field("start", &self.span.start);
state.serialize_field("end", &self.span.end);
state.serialize_field("name", &self.name);
state.serialize_field("name", &JsonSafeString(self.name.as_str()));
state.end();
}
}
Expand Down Expand Up @@ -2879,7 +2879,7 @@ impl ESTree for TSIndexSignatureName<'_> {
state.serialize_field("type", &JsonSafeString("Identifier"));
state.serialize_field("start", &self.span.start);
state.serialize_field("end", &self.span.end);
state.serialize_field("name", &self.name);
state.serialize_field("name", &JsonSafeString(self.name.as_str()));
state.serialize_field("typeAnnotation", &self.type_annotation);
state.end();
}
Expand Down
12 changes: 12 additions & 0 deletions tasks/ast_tools/src/derives/estree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ fn parse_estree_attr(location: AttrLocation, part: AttrPart) -> Result<()> {
AttrLocation::StructField(struct_def, field_index) => match part {
AttrPart::Tag("skip") => struct_def.fields[field_index].estree.skip = true,
AttrPart::Tag("flatten") => struct_def.fields[field_index].estree.flatten = true,
AttrPart::Tag("json_safe") => struct_def.fields[field_index].estree.json_safe = true,
AttrPart::String("rename", value) => {
struct_def.fields[field_index].estree.rename = Some(value);
}
Expand Down Expand Up @@ -372,6 +373,17 @@ impl<'s> StructSerializerGenerator<'s> {
quote! {
#wrapper_ident { array: &#self_path.#field_name_ident, after: &#self_path.#append_from_ident }
}
} else if field.estree.json_safe {
// Wrap value in `JsonSafeString(...)` if field is tagged `#[estree(json_safe)]`
match field.type_def(self.schema).name() {
"&str" => quote!( JsonSafeString(#self_path.#field_name_ident) ),
"Atom" => quote!( JsonSafeString(#self_path.#field_name_ident.as_str()) ),
_ => panic!(
"`#[estree(json_safe)]` is only valid on struct fields containing a `&str` or `Atom`: {}::{}",
struct_def.name(),
field.name(),
),
}
} else {
quote!( #self_path.#field_name_ident )
};
Expand Down
2 changes: 2 additions & 0 deletions tasks/ast_tools/src/schema/extensions/estree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ pub struct ESTreeStructField {
pub append_field_index: Option<usize>,
pub skip: bool,
pub flatten: bool,
// `true` for fields containing a `&str` or `Atom` which does not need escaping in JSON
pub json_safe: bool,
pub is_ts: bool,
}

Expand Down
Loading