Skip to content

Commit

Permalink
refactor(ast): move StringLiteral definition higher up (#7270)
Browse files Browse the repository at this point in the history
Pure refactor. `StringLiteral` definition was sandwiched in the middle of RegExp-related code. Move it higher up in `literal.rs`.

All the rest of the diff is just re-ordering generated code.
  • Loading branch information
overlookmotel committed Nov 13, 2024
1 parent 834c94d commit de472ca
Show file tree
Hide file tree
Showing 13 changed files with 121 additions and 121 deletions.
30 changes: 15 additions & 15 deletions crates/oxc_ast/src/ast/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,21 @@ pub struct NumericLiteral<'a> {
pub base: NumberBase,
}

/// String literal
///
/// <https://tc39.es/ecma262/#sec-literals-string-literals>
#[ast(visit)]
#[derive(Debug, Clone)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash, ESTree)]
pub struct StringLiteral<'a> {
/// Node location in source code
pub span: Span,
/// The value of the string.
///
/// Any escape sequences in the raw code are unescaped.
pub value: Atom<'a>,
}

/// BigInt literal
#[ast(visit)]
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -128,21 +143,6 @@ pub enum RegExpPattern<'a> {
Pattern(Box<'a, Pattern<'a>>) = 2,
}

/// String literal
///
/// <https://tc39.es/ecma262/#sec-literals-string-literals>
#[ast(visit)]
#[derive(Debug, Clone)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash, ESTree)]
pub struct StringLiteral<'a> {
/// Node location in source code
pub span: Span,
/// The value of the string.
///
/// Any escape sequences in the raw code are unescaped.
pub value: Atom<'a>,
}

bitflags! {
/// Regular expression flags.
///
Expand Down
20 changes: 10 additions & 10 deletions crates/oxc_ast/src/generated/assert_layouts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ const _: () = {
assert!(offset_of!(NumericLiteral, raw) == 16usize);
assert!(offset_of!(NumericLiteral, base) == 32usize);

assert!(size_of::<StringLiteral>() == 24usize);
assert!(align_of::<StringLiteral>() == 8usize);
assert!(offset_of!(StringLiteral, span) == 0usize);
assert!(offset_of!(StringLiteral, value) == 8usize);

assert!(size_of::<BigIntLiteral>() == 32usize);
assert!(align_of::<BigIntLiteral>() == 8usize);
assert!(offset_of!(BigIntLiteral, span) == 0usize);
Expand All @@ -45,11 +50,6 @@ const _: () = {
assert!(size_of::<RegExpPattern>() == 24usize);
assert!(align_of::<RegExpPattern>() == 8usize);

assert!(size_of::<StringLiteral>() == 24usize);
assert!(align_of::<StringLiteral>() == 8usize);
assert!(offset_of!(StringLiteral, span) == 0usize);
assert!(offset_of!(StringLiteral, value) == 8usize);

assert!(size_of::<Program>() == 160usize);
assert!(align_of::<Program>() == 8usize);
assert!(offset_of!(Program, span) == 0usize);
Expand Down Expand Up @@ -1581,6 +1581,11 @@ const _: () = {
assert!(offset_of!(NumericLiteral, raw) == 16usize);
assert!(offset_of!(NumericLiteral, base) == 24usize);

assert!(size_of::<StringLiteral>() == 16usize);
assert!(align_of::<StringLiteral>() == 4usize);
assert!(offset_of!(StringLiteral, span) == 0usize);
assert!(offset_of!(StringLiteral, value) == 8usize);

assert!(size_of::<BigIntLiteral>() == 20usize);
assert!(align_of::<BigIntLiteral>() == 4usize);
assert!(offset_of!(BigIntLiteral, span) == 0usize);
Expand All @@ -1601,11 +1606,6 @@ const _: () = {
assert!(size_of::<RegExpPattern>() == 12usize);
assert!(align_of::<RegExpPattern>() == 4usize);

assert!(size_of::<StringLiteral>() == 16usize);
assert!(align_of::<StringLiteral>() == 4usize);
assert!(offset_of!(StringLiteral, span) == 0usize);
assert!(offset_of!(StringLiteral, value) == 8usize);

assert!(size_of::<Program>() == 88usize);
assert!(align_of::<Program>() == 4usize);
assert!(offset_of!(Program, span) == 0usize);
Expand Down
60 changes: 30 additions & 30 deletions crates/oxc_ast/src/generated/ast_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,36 @@ impl<'a> AstBuilder<'a> {
Box::new_in(self.numeric_literal(span, value, raw, base), self.allocator)
}

/// Build a [`StringLiteral`].
///
/// If you want the built node to be allocated in the memory arena, use [`AstBuilder::alloc_string_literal`] instead.
///
/// ## Parameters
/// - span: Node location in source code
/// - value: The value of the string.
#[inline]
pub fn string_literal<A>(self, span: Span, value: A) -> StringLiteral<'a>
where
A: IntoIn<'a, Atom<'a>>,
{
StringLiteral { span, value: value.into_in(self.allocator) }
}

/// Build a [`StringLiteral`], and store it in the memory arena.
///
/// Returns a [`Box`] containing the newly-allocated node. If you want a stack-allocated node, use [`AstBuilder::string_literal`] instead.
///
/// ## Parameters
/// - span: Node location in source code
/// - value: The value of the string.
#[inline]
pub fn alloc_string_literal<A>(self, span: Span, value: A) -> Box<'a, StringLiteral<'a>>
where
A: IntoIn<'a, Atom<'a>>,
{
Box::new_in(self.string_literal(span, value), self.allocator)
}

/// Build a [`BigIntLiteral`].
///
/// If you want the built node to be allocated in the memory arena, use [`AstBuilder::alloc_big_int_literal`] instead.
Expand Down Expand Up @@ -190,36 +220,6 @@ impl<'a> AstBuilder<'a> {
Box::new_in(self.reg_exp_literal(span, regex, raw), self.allocator)
}

/// Build a [`StringLiteral`].
///
/// If you want the built node to be allocated in the memory arena, use [`AstBuilder::alloc_string_literal`] instead.
///
/// ## Parameters
/// - span: Node location in source code
/// - value: The value of the string.
#[inline]
pub fn string_literal<A>(self, span: Span, value: A) -> StringLiteral<'a>
where
A: IntoIn<'a, Atom<'a>>,
{
StringLiteral { span, value: value.into_in(self.allocator) }
}

/// Build a [`StringLiteral`], and store it in the memory arena.
///
/// Returns a [`Box`] containing the newly-allocated node. If you want a stack-allocated node, use [`AstBuilder::string_literal`] instead.
///
/// ## Parameters
/// - span: Node location in source code
/// - value: The value of the string.
#[inline]
pub fn alloc_string_literal<A>(self, span: Span, value: A) -> Box<'a, StringLiteral<'a>>
where
A: IntoIn<'a, Atom<'a>>,
{
Box::new_in(self.string_literal(span, value), self.allocator)
}

/// Build a [`Program`].
///
/// If you want the built node to be allocated in the memory arena, use [`AstBuilder::alloc_program`] instead.
Expand Down
18 changes: 9 additions & 9 deletions crates/oxc_ast/src/generated/ast_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ pub enum AstType {
BooleanLiteral,
NullLiteral,
NumericLiteral,
StringLiteral,
BigIntLiteral,
RegExpLiteral,
StringLiteral,
Program,
IdentifierName,
IdentifierReference,
Expand Down Expand Up @@ -183,9 +183,9 @@ pub enum AstKind<'a> {
BooleanLiteral(&'a BooleanLiteral),
NullLiteral(&'a NullLiteral),
NumericLiteral(&'a NumericLiteral<'a>),
StringLiteral(&'a StringLiteral<'a>),
BigIntLiteral(&'a BigIntLiteral<'a>),
RegExpLiteral(&'a RegExpLiteral<'a>),
StringLiteral(&'a StringLiteral<'a>),
Program(&'a Program<'a>),
IdentifierName(&'a IdentifierName<'a>),
IdentifierReference(&'a IdentifierReference<'a>),
Expand Down Expand Up @@ -354,9 +354,9 @@ impl<'a> GetSpan for AstKind<'a> {
Self::BooleanLiteral(it) => it.span(),
Self::NullLiteral(it) => it.span(),
Self::NumericLiteral(it) => it.span(),
Self::StringLiteral(it) => it.span(),
Self::BigIntLiteral(it) => it.span(),
Self::RegExpLiteral(it) => it.span(),
Self::StringLiteral(it) => it.span(),
Self::Program(it) => it.span(),
Self::IdentifierName(it) => it.span(),
Self::IdentifierReference(it) => it.span(),
Expand Down Expand Up @@ -549,26 +549,26 @@ impl<'a> AstKind<'a> {
}

#[inline]
pub fn as_big_int_literal(self) -> Option<&'a BigIntLiteral<'a>> {
if let Self::BigIntLiteral(v) = self {
pub fn as_string_literal(self) -> Option<&'a StringLiteral<'a>> {
if let Self::StringLiteral(v) = self {
Some(v)
} else {
None
}
}

#[inline]
pub fn as_reg_exp_literal(self) -> Option<&'a RegExpLiteral<'a>> {
if let Self::RegExpLiteral(v) = self {
pub fn as_big_int_literal(self) -> Option<&'a BigIntLiteral<'a>> {
if let Self::BigIntLiteral(v) = self {
Some(v)
} else {
None
}
}

#[inline]
pub fn as_string_literal(self) -> Option<&'a StringLiteral<'a>> {
if let Self::StringLiteral(v) = self {
pub fn as_reg_exp_literal(self) -> Option<&'a RegExpLiteral<'a>> {
if let Self::RegExpLiteral(v) = self {
Some(v)
} else {
None
Expand Down
20 changes: 10 additions & 10 deletions crates/oxc_ast/src/generated/derive_clone_in.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ impl<'old_alloc, 'new_alloc> CloneIn<'new_alloc> for NumericLiteral<'old_alloc>
}
}

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 {
StringLiteral {
span: CloneIn::clone_in(&self.span, allocator),
value: CloneIn::clone_in(&self.value, allocator),
}
}
}

impl<'old_alloc, 'new_alloc> CloneIn<'new_alloc> for BigIntLiteral<'old_alloc> {
type Cloned = BigIntLiteral<'new_alloc>;
fn clone_in(&self, allocator: &'new_alloc Allocator) -> Self::Cloned {
Expand Down Expand Up @@ -87,16 +97,6 @@ impl<'old_alloc, 'new_alloc> CloneIn<'new_alloc> for RegExpPattern<'old_alloc> {
}
}

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 {
StringLiteral {
span: CloneIn::clone_in(&self.span, allocator),
value: CloneIn::clone_in(&self.value, allocator),
}
}
}

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

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

impl<'a> ContentEq for BigIntLiteral<'a> {
fn content_eq(&self, other: &Self) -> bool {
ContentEq::content_eq(&self.raw, &other.raw)
Expand Down Expand Up @@ -75,12 +81,6 @@ impl<'a> ContentEq for RegExpPattern<'a> {
}
}

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

impl<'a> ContentEq for Program<'a> {
fn content_eq(&self, other: &Self) -> bool {
ContentEq::content_eq(&self.source_type, &other.source_type)
Expand Down
12 changes: 6 additions & 6 deletions crates/oxc_ast/src/generated/derive_content_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ impl ContentHash for BooleanLiteral {
}
}

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

impl<'a> ContentHash for BigIntLiteral<'a> {
fn content_hash<H: Hasher>(&self, state: &mut H) {
ContentHash::content_hash(&self.raw, state);
Expand Down Expand Up @@ -55,12 +61,6 @@ impl<'a> ContentHash for RegExpPattern<'a> {
}
}

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

impl<'a> ContentHash for Program<'a> {
fn content_hash<H: Hasher>(&self, state: &mut H) {
ContentHash::content_hash(&self.source_type, state);
Expand Down
20 changes: 10 additions & 10 deletions crates/oxc_ast/src/generated/derive_estree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ impl<'a> Serialize for NumericLiteral<'a> {
}
}

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)?;
map.serialize_entry("type", "StringLiteral")?;
self.span.serialize(serde::__private::ser::FlatMapSerializer(&mut map))?;
map.serialize_entry("value", &self.value)?;
map.end()
}
}

impl<'a> Serialize for BigIntLiteral<'a> {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
crate::serialize::ESTreeLiteral::from(self).serialize(serializer)
Expand Down Expand Up @@ -62,16 +72,6 @@ impl<'a> Serialize for RegExpPattern<'a> {
}
}

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)?;
map.serialize_entry("type", "StringLiteral")?;
self.span.serialize(serde::__private::ser::FlatMapSerializer(&mut map))?;
map.serialize_entry("value", &self.value)?;
map.end()
}
}

impl<'a> Serialize for Program<'a> {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let mut map = serializer.serialize_map(None)?;
Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_ast/src/generated/derive_get_span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,21 @@ impl<'a> GetSpan for NumericLiteral<'a> {
}
}

impl<'a> GetSpan for BigIntLiteral<'a> {
impl<'a> GetSpan for StringLiteral<'a> {
#[inline]
fn span(&self) -> Span {
self.span
}
}

impl<'a> GetSpan for RegExpLiteral<'a> {
impl<'a> GetSpan for BigIntLiteral<'a> {
#[inline]
fn span(&self) -> Span {
self.span
}
}

impl<'a> GetSpan for StringLiteral<'a> {
impl<'a> GetSpan for RegExpLiteral<'a> {
#[inline]
fn span(&self) -> Span {
self.span
Expand Down
Loading

0 comments on commit de472ca

Please sign in to comment.