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
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/oxc_ast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ oxc_span = { workspace = true }
oxc_syntax = { workspace = true }

bitflags = { workspace = true }
cow-utils = { workspace = true }
num-bigint = { workspace = true }
num-traits = { workspace = true }

serde = { workspace = true, features = ["derive"], optional = true }
serde_json = { workspace = true, optional = true }
Expand Down
25 changes: 12 additions & 13 deletions crates/oxc_ast/src/ast/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use oxc_syntax::number::{BigintBase, NumberBase};
#[ast(visit)]
#[derive(Debug, Clone)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash, ESTree)]
#[estree(type = "Literal", via = crate::serialize::ESTreeLiteral, add_ts = "raw: string")]
pub struct BooleanLiteral {
/// Node location in source code
pub span: Span,
Expand All @@ -33,6 +34,7 @@ pub struct BooleanLiteral {
#[ast(visit)]
#[derive(Debug, Clone)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ESTree)]
#[estree(type = "Literal", via = crate::serialize::ESTreeLiteral, add_ts = "value: null, raw: \"null\"")]
pub struct NullLiteral {
/// Node location in source code
pub span: Span,
Expand All @@ -44,6 +46,7 @@ pub struct NullLiteral {
#[ast(visit)]
#[derive(Debug, Clone)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ESTree)]
#[estree(type = "Literal", via = crate::serialize::ESTreeLiteral)]
pub struct NumericLiteral<'a> {
/// Node location in source code
pub span: Span,
Expand All @@ -60,6 +63,7 @@ pub struct NumericLiteral<'a> {
#[ast(visit)]
#[derive(Debug, Clone)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash, ESTree)]
#[estree(type = "Literal", via = crate::serialize::ESTreeLiteral, add_ts = "value: null, bigint: string")]
pub struct BigIntLiteral<'a> {
/// Node location in source code
pub span: Span,
Expand All @@ -76,18 +80,20 @@ pub struct BigIntLiteral<'a> {
#[ast(visit)]
#[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash, ESTree)]
#[estree(
type = "Literal",
via = crate::serialize::ESTreeLiteral,
add_ts = "value: {} | null, regex: { pattern: string, flags: string }"
)]
pub struct RegExpLiteral<'a> {
/// Node location in source code
pub span: Span,
/// Placeholder for printing.
///
/// Valid regular expressions are printed as `{}`, while invalid ones are
/// printed as `null`. Note that invalid regular expressions are not yet
/// printed properly.
pub value: EmptyObject,
/// The parsed regular expression. See [`oxc_regular_expression`] for more
/// details.
#[estree(skip)]
pub regex: RegExp<'a>,
/// The regular expression as it appears in source code
pub raw: &'a str,
}

/// A regular expression
Expand Down Expand Up @@ -122,13 +128,6 @@ pub enum RegExpPattern<'a> {
Pattern(Box<'a, Pattern<'a>>) = 2,
}

/// An empty object literal (`{}`)
#[ast]
#[derive(Debug, Clone)]
#[generate_derive(CloneIn, ContentEq, ContentHash, ESTree)]
#[estree(no_type)]
pub struct EmptyObject;
Comment on lines -125 to -130
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very glad to see the back of this hack!


/// String literal
///
/// <https://tc39.es/ecma262/#sec-literals-string-literals>
Expand Down
14 changes: 4 additions & 10 deletions crates/oxc_ast/src/generated/assert_layouts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ const _: () = {
assert!(offset_of!(BigIntLiteral, raw) == 8usize);
assert!(offset_of!(BigIntLiteral, base) == 24usize);

assert!(size_of::<RegExpLiteral>() == 40usize);
assert!(size_of::<RegExpLiteral>() == 56usize);
assert!(align_of::<RegExpLiteral>() == 8usize);
assert!(offset_of!(RegExpLiteral, span) == 0usize);
assert!(offset_of!(RegExpLiteral, value) == 8usize);
assert!(offset_of!(RegExpLiteral, regex) == 8usize);
assert!(offset_of!(RegExpLiteral, raw) == 40usize);

assert!(size_of::<RegExp>() == 32usize);
assert!(align_of::<RegExp>() == 8usize);
Expand All @@ -45,9 +45,6 @@ const _: () = {
assert!(size_of::<RegExpPattern>() == 24usize);
assert!(align_of::<RegExpPattern>() == 8usize);

assert!(size_of::<EmptyObject>() == 0usize);
assert!(align_of::<EmptyObject>() == 1usize);

assert!(size_of::<StringLiteral>() == 24usize);
assert!(align_of::<StringLiteral>() == 8usize);
assert!(offset_of!(StringLiteral, span) == 0usize);
Expand Down Expand Up @@ -1591,11 +1588,11 @@ const _: () = {
assert!(offset_of!(BigIntLiteral, raw) == 8usize);
assert!(offset_of!(BigIntLiteral, base) == 16usize);

assert!(size_of::<RegExpLiteral>() == 24usize);
assert!(size_of::<RegExpLiteral>() == 32usize);
assert!(align_of::<RegExpLiteral>() == 4usize);
assert!(offset_of!(RegExpLiteral, span) == 0usize);
assert!(offset_of!(RegExpLiteral, value) == 8usize);
assert!(offset_of!(RegExpLiteral, regex) == 8usize);
assert!(offset_of!(RegExpLiteral, raw) == 24usize);

assert!(size_of::<RegExp>() == 16usize);
assert!(align_of::<RegExp>() == 4usize);
Expand All @@ -1605,9 +1602,6 @@ const _: () = {
assert!(size_of::<RegExpPattern>() == 12usize);
assert!(align_of::<RegExpPattern>() == 4usize);

assert!(size_of::<EmptyObject>() == 0usize);
assert!(align_of::<EmptyObject>() == 1usize);

assert!(size_of::<StringLiteral>() == 16usize);
assert!(align_of::<StringLiteral>() == 4usize);
assert!(offset_of!(StringLiteral, span) == 0usize);
Expand Down
53 changes: 30 additions & 23 deletions crates/oxc_ast/src/generated/ast_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,16 +159,14 @@ impl<'a> AstBuilder<'a> {
///
/// ## Parameters
/// - span: Node location in source code
/// - value: Placeholder for printing.
/// - regex: The parsed regular expression. See [`oxc_regular_expression`] for more
/// - raw: The regular expression as it appears in source code
#[inline]
pub fn reg_exp_literal(
self,
span: Span,
value: EmptyObject,
regex: RegExp<'a>,
) -> RegExpLiteral<'a> {
RegExpLiteral { span, value, regex }
pub fn reg_exp_literal<S>(self, span: Span, regex: RegExp<'a>, raw: S) -> RegExpLiteral<'a>
where
S: IntoIn<'a, &'a str>,
{
RegExpLiteral { span, regex, raw: raw.into_in(self.allocator) }
}

/// Build a [`RegExpLiteral`], and store it in the memory arena.
Expand All @@ -177,16 +175,19 @@ impl<'a> AstBuilder<'a> {
///
/// ## Parameters
/// - span: Node location in source code
/// - value: Placeholder for printing.
/// - regex: The parsed regular expression. See [`oxc_regular_expression`] for more
/// - raw: The regular expression as it appears in source code
#[inline]
pub fn alloc_reg_exp_literal(
pub fn alloc_reg_exp_literal<S>(
self,
span: Span,
value: EmptyObject,
regex: RegExp<'a>,
) -> Box<'a, RegExpLiteral<'a>> {
Box::new_in(self.reg_exp_literal(span, value, regex), self.allocator)
raw: S,
) -> Box<'a, RegExpLiteral<'a>>
where
S: IntoIn<'a, &'a str>,
{
Box::new_in(self.reg_exp_literal(span, regex, raw), self.allocator)
}

/// Build a [`StringLiteral`].
Expand Down Expand Up @@ -445,16 +446,19 @@ impl<'a> AstBuilder<'a> {
///
/// ## Parameters
/// - span: Node location in source code
/// - value: Placeholder for printing.
/// - regex: The parsed regular expression. See [`oxc_regular_expression`] for more
/// - raw: The regular expression as it appears in source code
#[inline]
pub fn expression_reg_exp_literal(
pub fn expression_reg_exp_literal<S>(
self,
span: Span,
value: EmptyObject,
regex: RegExp<'a>,
) -> Expression<'a> {
Expression::RegExpLiteral(self.alloc(self.reg_exp_literal(span, value, regex)))
raw: S,
) -> Expression<'a>
where
S: IntoIn<'a, &'a str>,
{
Expression::RegExpLiteral(self.alloc(self.reg_exp_literal(span, regex, raw)))
}

/// Build an [`Expression::StringLiteral`]
Expand Down Expand Up @@ -7959,16 +7963,19 @@ impl<'a> AstBuilder<'a> {
///
/// ## Parameters
/// - span: Node location in source code
/// - value: Placeholder for printing.
/// - regex: The parsed regular expression. See [`oxc_regular_expression`] for more
/// - raw: The regular expression as it appears in source code
#[inline]
pub fn ts_literal_reg_exp_literal(
pub fn ts_literal_reg_exp_literal<S>(
self,
span: Span,
value: EmptyObject,
regex: RegExp<'a>,
) -> TSLiteral<'a> {
TSLiteral::RegExpLiteral(self.alloc(self.reg_exp_literal(span, value, regex)))
raw: S,
) -> TSLiteral<'a>
where
S: IntoIn<'a, &'a str>,
{
TSLiteral::RegExpLiteral(self.alloc(self.reg_exp_literal(span, regex, raw)))
}

/// Build a [`TSLiteral::StringLiteral`]
Expand Down
9 changes: 1 addition & 8 deletions crates/oxc_ast/src/generated/derive_clone_in.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ impl<'old_alloc, 'new_alloc> CloneIn<'new_alloc> for RegExpLiteral<'old_alloc> {
fn clone_in(&self, allocator: &'new_alloc Allocator) -> Self::Cloned {
RegExpLiteral {
span: CloneIn::clone_in(&self.span, allocator),
value: CloneIn::clone_in(&self.value, allocator),
regex: CloneIn::clone_in(&self.regex, allocator),
raw: CloneIn::clone_in(&self.raw, allocator),
}
}
}
Expand All @@ -87,13 +87,6 @@ impl<'old_alloc, 'new_alloc> CloneIn<'new_alloc> for RegExpPattern<'old_alloc> {
}
}

impl<'alloc> CloneIn<'alloc> for EmptyObject {
type Cloned = EmptyObject;
fn clone_in(&self, _: &'alloc Allocator) -> Self::Cloned {
EmptyObject
}
}

impl<'old_alloc, 'new_alloc> CloneIn<'new_alloc> for StringLiteral<'old_alloc> {
type Cloned = StringLiteral<'new_alloc>;
fn clone_in(&self, allocator: &'new_alloc Allocator) -> Self::Cloned {
Expand Down
10 changes: 2 additions & 8 deletions crates/oxc_ast/src/generated/derive_content_eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ impl<'a> ContentEq for BigIntLiteral<'a> {

impl<'a> ContentEq for RegExpLiteral<'a> {
fn content_eq(&self, other: &Self) -> bool {
ContentEq::content_eq(&self.value, &other.value)
&& ContentEq::content_eq(&self.regex, &other.regex)
ContentEq::content_eq(&self.regex, &other.regex)
&& ContentEq::content_eq(&self.raw, &other.raw)
}
}

Expand Down Expand Up @@ -75,12 +75,6 @@ impl<'a> ContentEq for RegExpPattern<'a> {
}
}

impl ContentEq for EmptyObject {
fn content_eq(&self, _: &Self) -> bool {
true
}
}

impl<'a> ContentEq for StringLiteral<'a> {
fn content_eq(&self, other: &Self) -> bool {
ContentEq::content_eq(&self.value, &other.value)
Expand Down
6 changes: 1 addition & 5 deletions crates/oxc_ast/src/generated/derive_content_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ impl<'a> ContentHash for BigIntLiteral<'a> {

impl<'a> ContentHash for RegExpLiteral<'a> {
fn content_hash<H: Hasher>(&self, state: &mut H) {
ContentHash::content_hash(&self.value, state);
ContentHash::content_hash(&self.regex, state);
ContentHash::content_hash(&self.raw, state);
}
}

Expand All @@ -55,10 +55,6 @@ impl<'a> ContentHash for RegExpPattern<'a> {
}
}

impl ContentHash for EmptyObject {
fn content_hash<H: Hasher>(&self, _: &mut H) {}
}

impl<'a> ContentHash for StringLiteral<'a> {
fn content_hash<H: Hasher>(&self, state: &mut H) {
ContentHash::content_hash(&self.value, state);
Expand Down
38 changes: 5 additions & 33 deletions crates/oxc_ast/src/generated/derive_estree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,52 +15,31 @@ use crate::ast::ts::*;

impl Serialize for BooleanLiteral {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let mut map = serializer.serialize_map(None)?;
map.serialize_entry("type", "BooleanLiteral")?;
self.span.serialize(serde::__private::ser::FlatMapSerializer(&mut map))?;
map.serialize_entry("value", &self.value)?;
map.end()
crate::serialize::ESTreeLiteral::from(self).serialize(serializer)
}
}

impl Serialize for NullLiteral {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let mut map = serializer.serialize_map(None)?;
map.serialize_entry("type", "NullLiteral")?;
self.span.serialize(serde::__private::ser::FlatMapSerializer(&mut map))?;
map.end()
crate::serialize::ESTreeLiteral::from(self).serialize(serializer)
}
}

impl<'a> Serialize for NumericLiteral<'a> {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let mut map = serializer.serialize_map(None)?;
map.serialize_entry("type", "NumericLiteral")?;
self.span.serialize(serde::__private::ser::FlatMapSerializer(&mut map))?;
map.serialize_entry("value", &self.value)?;
map.serialize_entry("raw", &self.raw)?;
map.end()
crate::serialize::ESTreeLiteral::from(self).serialize(serializer)
}
}

impl<'a> Serialize for BigIntLiteral<'a> {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let mut map = serializer.serialize_map(None)?;
map.serialize_entry("type", "BigIntLiteral")?;
self.span.serialize(serde::__private::ser::FlatMapSerializer(&mut map))?;
map.serialize_entry("raw", &self.raw)?;
map.end()
crate::serialize::ESTreeLiteral::from(self).serialize(serializer)
}
}

impl<'a> Serialize for RegExpLiteral<'a> {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let mut map = serializer.serialize_map(None)?;
map.serialize_entry("type", "RegExpLiteral")?;
self.span.serialize(serde::__private::ser::FlatMapSerializer(&mut map))?;
map.serialize_entry("value", &self.value)?;
map.serialize_entry("regex", &self.regex)?;
map.end()
crate::serialize::ESTreeLiteral::from(self).serialize(serializer)
}
}

Expand All @@ -83,13 +62,6 @@ impl<'a> Serialize for RegExpPattern<'a> {
}
}

impl Serialize for EmptyObject {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let mut map = serializer.serialize_map(None)?;
map.end()
}
}

impl<'a> Serialize for StringLiteral<'a> {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let mut map = serializer.serialize_map(None)?;
Expand Down
Loading