Skip to content
Closed
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
29 changes: 16 additions & 13 deletions crates/oxc_ast/src/ast/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,22 @@ 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)]
#[estree(type = "Literal", via = crate::serialize::ESTreeLiteral)]
pub struct StringLiteral<'a> {
/// Node location in source code
pub span: Span,
/// The value of the string
pub value: Atom<'a>,
/// The string as it appears in source code, including quotes
pub raw: &'a str,
}

/// BigInt literal
#[ast(visit)]
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -128,19 +144,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 string as it appears in source code
pub value: Atom<'a>,
}

bitflags! {
/// Regular expression flags.
///
Expand Down
154 changes: 78 additions & 76 deletions crates/oxc_ast/src/generated/assert_layouts.rs

Large diffs are not rendered by default.

151 changes: 97 additions & 54 deletions crates/oxc_ast/src/generated/ast_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,49 @@ 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
/// - raw: The string as it appears in source code, including quotes
#[inline]
pub fn string_literal<A, S>(self, span: Span, value: A, raw: S) -> StringLiteral<'a>
where
A: IntoIn<'a, Atom<'a>>,
S: IntoIn<'a, &'a str>,
{
StringLiteral {
span,
value: value.into_in(self.allocator),
raw: raw.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
/// - raw: The string as it appears in source code, including quotes
#[inline]
pub fn alloc_string_literal<A, S>(
self,
span: Span,
value: A,
raw: S,
) -> Box<'a, StringLiteral<'a>>
where
A: IntoIn<'a, Atom<'a>>,
S: IntoIn<'a, &'a str>,
{
Box::new_in(self.string_literal(span, value, raw), 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 +233,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 string as it appears in source code
#[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 string as it appears in source code
#[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 Expand Up @@ -467,13 +480,15 @@ impl<'a> AstBuilder<'a> {
///
/// ## Parameters
/// - span: Node location in source code
/// - value: The string as it appears in source code
/// - value: The value of the string
/// - raw: The string as it appears in source code, including quotes
#[inline]
pub fn expression_string_literal<A>(self, span: Span, value: A) -> Expression<'a>
pub fn expression_string_literal<A, S>(self, span: Span, value: A, raw: S) -> Expression<'a>
where
A: IntoIn<'a, Atom<'a>>,
S: IntoIn<'a, &'a str>,
{
Expression::StringLiteral(self.alloc(self.string_literal(span, value)))
Expression::StringLiteral(self.alloc(self.string_literal(span, value, raw)))
}

/// Build an [`Expression::TemplateLiteral`]
Expand Down Expand Up @@ -7173,17 +7188,20 @@ impl<'a> AstBuilder<'a> {
///
/// ## Parameters
/// - span: Node location in source code
/// - value: The string as it appears in source code
/// - value: The value of the string
/// - raw: The string as it appears in source code, including quotes
#[inline]
pub fn import_attribute_key_string_literal<A>(
pub fn import_attribute_key_string_literal<A, S>(
self,
span: Span,
value: A,
raw: S,
) -> ImportAttributeKey<'a>
where
A: IntoIn<'a, Atom<'a>>,
S: IntoIn<'a, &'a str>,
{
ImportAttributeKey::StringLiteral(self.string_literal(span, value))
ImportAttributeKey::StringLiteral(self.string_literal(span, value, raw))
}

/// Build an [`ExportNamedDeclaration`].
Expand Down Expand Up @@ -7560,13 +7578,20 @@ impl<'a> AstBuilder<'a> {
///
/// ## Parameters
/// - span: Node location in source code
/// - value: The string as it appears in source code
/// - value: The value of the string
/// - raw: The string as it appears in source code, including quotes
#[inline]
pub fn module_export_name_string_literal<A>(self, span: Span, value: A) -> ModuleExportName<'a>
pub fn module_export_name_string_literal<A, S>(
self,
span: Span,
value: A,
raw: S,
) -> ModuleExportName<'a>
where
A: IntoIn<'a, Atom<'a>>,
S: IntoIn<'a, &'a str>,
{
ModuleExportName::StringLiteral(self.string_literal(span, value))
ModuleExportName::StringLiteral(self.string_literal(span, value, raw))
}

/// Build a [`TSThisParameter`].
Expand Down Expand Up @@ -7774,13 +7799,20 @@ impl<'a> AstBuilder<'a> {
///
/// ## Parameters
/// - span: Node location in source code
/// - value: The string as it appears in source code
/// - value: The value of the string
/// - raw: The string as it appears in source code, including quotes
#[inline]
pub fn ts_enum_member_name_string_literal<A>(self, span: Span, value: A) -> TSEnumMemberName<'a>
pub fn ts_enum_member_name_string_literal<A, S>(
self,
span: Span,
value: A,
raw: S,
) -> TSEnumMemberName<'a>
where
A: IntoIn<'a, Atom<'a>>,
S: IntoIn<'a, &'a str>,
{
TSEnumMemberName::StaticStringLiteral(self.alloc(self.string_literal(span, value)))
TSEnumMemberName::StaticStringLiteral(self.alloc(self.string_literal(span, value, raw)))
}

/// Build a [`TSEnumMemberName::StaticTemplateLiteral`]
Expand Down Expand Up @@ -7984,13 +8016,15 @@ impl<'a> AstBuilder<'a> {
///
/// ## Parameters
/// - span: Node location in source code
/// - value: The string as it appears in source code
/// - value: The value of the string
/// - raw: The string as it appears in source code, including quotes
#[inline]
pub fn ts_literal_string_literal<A>(self, span: Span, value: A) -> TSLiteral<'a>
pub fn ts_literal_string_literal<A, S>(self, span: Span, value: A, raw: S) -> TSLiteral<'a>
where
A: IntoIn<'a, Atom<'a>>,
S: IntoIn<'a, &'a str>,
{
TSLiteral::StringLiteral(self.alloc(self.string_literal(span, value)))
TSLiteral::StringLiteral(self.alloc(self.string_literal(span, value, raw)))
}

/// Build a [`TSLiteral::TemplateLiteral`]
Expand Down Expand Up @@ -11034,17 +11068,20 @@ impl<'a> AstBuilder<'a> {
///
/// ## Parameters
/// - span: Node location in source code
/// - value: The string as it appears in source code
/// - value: The value of the string
/// - raw: The string as it appears in source code, including quotes
#[inline]
pub fn ts_module_declaration_name_string_literal<A>(
pub fn ts_module_declaration_name_string_literal<A, S>(
self,
span: Span,
value: A,
raw: S,
) -> TSModuleDeclarationName<'a>
where
A: IntoIn<'a, Atom<'a>>,
S: IntoIn<'a, &'a str>,
{
TSModuleDeclarationName::StringLiteral(self.string_literal(span, value))
TSModuleDeclarationName::StringLiteral(self.string_literal(span, value, raw))
}

/// Build a [`TSModuleDeclarationBody::TSModuleDeclaration`]
Expand Down Expand Up @@ -11432,17 +11469,20 @@ impl<'a> AstBuilder<'a> {
///
/// ## Parameters
/// - span: Node location in source code
/// - value: The string as it appears in source code
/// - value: The value of the string
/// - raw: The string as it appears in source code, including quotes
#[inline]
pub fn ts_import_attribute_name_string_literal<A>(
pub fn ts_import_attribute_name_string_literal<A, S>(
self,
span: Span,
value: A,
raw: S,
) -> TSImportAttributeName<'a>
where
A: IntoIn<'a, Atom<'a>>,
S: IntoIn<'a, &'a str>,
{
TSImportAttributeName::StringLiteral(self.string_literal(span, value))
TSImportAttributeName::StringLiteral(self.string_literal(span, value, raw))
}

/// Build a [`TSFunctionType`].
Expand Down Expand Up @@ -12826,17 +12866,20 @@ impl<'a> AstBuilder<'a> {
///
/// ## Parameters
/// - span: Node location in source code
/// - value: The string as it appears in source code
/// - value: The value of the string
/// - raw: The string as it appears in source code, including quotes
#[inline]
pub fn jsx_attribute_value_string_literal<A>(
pub fn jsx_attribute_value_string_literal<A, S>(
self,
span: Span,
value: A,
raw: S,
) -> JSXAttributeValue<'a>
where
A: IntoIn<'a, Atom<'a>>,
S: IntoIn<'a, &'a str>,
{
JSXAttributeValue::StringLiteral(self.alloc(self.string_literal(span, value)))
JSXAttributeValue::StringLiteral(self.alloc(self.string_literal(span, value, raw)))
}

/// Build a [`JSXAttributeValue::ExpressionContainer`]
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
Loading