Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ast)!: add StringLiteral::raw field #7393

Merged
merged 1 commit into from
Nov 26, 2024
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
7 changes: 6 additions & 1 deletion crates/oxc_ast/src/ast/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,19 @@ 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 = "raw?: undefined")]
#[estree(type = "Literal", via = crate::serialize::ESTreeLiteral)]
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>,

/// The raw string as it appears in source code.
///
/// `None` when this ast node is not constructed from the parser.
pub raw: Option<Atom<'a>>,
}

/// BigInt literal
Expand Down
138 changes: 70 additions & 68 deletions crates/oxc_ast/src/generated/assert_layouts.rs

Large diffs are not rendered by default.

71 changes: 55 additions & 16 deletions crates/oxc_ast/src/generated/ast_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,13 @@ impl<'a> AstBuilder<'a> {
/// ## Parameters
/// - span: Node location in source code
/// - value: The value of the string.
/// - raw: The raw string as it appears in source code.
#[inline]
pub fn string_literal<A>(self, span: Span, value: A) -> StringLiteral<'a>
pub fn string_literal<A>(self, span: Span, value: A, raw: Option<Atom<'a>>) -> StringLiteral<'a>
where
A: IntoIn<'a, Atom<'a>>,
{
StringLiteral { span, value: value.into_in(self.allocator) }
StringLiteral { span, value: value.into_in(self.allocator), raw }
}

/// Build a [`StringLiteral`], and store it in the memory arena.
Expand All @@ -138,12 +139,18 @@ impl<'a> AstBuilder<'a> {
/// ## Parameters
/// - span: Node location in source code
/// - value: The value of the string.
/// - raw: The raw string as it appears in source code.
#[inline]
pub fn alloc_string_literal<A>(self, span: Span, value: A) -> Box<'a, StringLiteral<'a>>
pub fn alloc_string_literal<A>(
self,
span: Span,
value: A,
raw: Option<Atom<'a>>,
) -> Box<'a, StringLiteral<'a>>
where
A: IntoIn<'a, Atom<'a>>,
{
Box::new_in(self.string_literal(span, value), self.allocator)
Box::new_in(self.string_literal(span, value, raw), self.allocator)
}

/// Build a [`BigIntLiteral`].
Expand Down Expand Up @@ -468,12 +475,18 @@ impl<'a> AstBuilder<'a> {
/// ## Parameters
/// - span: Node location in source code
/// - value: The value of the string.
/// - raw: The raw string as it appears in source code.
#[inline]
pub fn expression_string_literal<A>(self, span: Span, value: A) -> Expression<'a>
pub fn expression_string_literal<A>(
self,
span: Span,
value: A,
raw: Option<Atom<'a>>,
) -> Expression<'a>
where
A: IntoIn<'a, Atom<'a>>,
{
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 @@ -7182,16 +7195,18 @@ impl<'a> AstBuilder<'a> {
/// ## Parameters
/// - span: Node location in source code
/// - value: The value of the string.
/// - raw: The raw string as it appears in source code.
#[inline]
pub fn import_attribute_key_string_literal<A>(
self,
span: Span,
value: A,
raw: Option<Atom<'a>>,
) -> ImportAttributeKey<'a>
where
A: IntoIn<'a, Atom<'a>>,
{
ImportAttributeKey::StringLiteral(self.string_literal(span, value))
ImportAttributeKey::StringLiteral(self.string_literal(span, value, raw))
}

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

/// Build a [`TSThisParameter`].
Expand Down Expand Up @@ -7783,12 +7804,18 @@ impl<'a> AstBuilder<'a> {
/// ## Parameters
/// - span: Node location in source code
/// - value: The value of the string.
/// - raw: The raw string as it appears in source code.
#[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>(
self,
span: Span,
value: A,
raw: Option<Atom<'a>>,
) -> TSEnumMemberName<'a>
where
A: IntoIn<'a, Atom<'a>>,
{
TSEnumMemberName::String(self.alloc(self.string_literal(span, value)))
TSEnumMemberName::String(self.alloc(self.string_literal(span, value, raw)))
}

/// Build a [`TSTypeAnnotation`].
Expand Down Expand Up @@ -7946,12 +7973,18 @@ impl<'a> AstBuilder<'a> {
/// ## Parameters
/// - span: Node location in source code
/// - value: The value of the string.
/// - raw: The raw string as it appears in source code.
#[inline]
pub fn ts_literal_string_literal<A>(self, span: Span, value: A) -> TSLiteral<'a>
pub fn ts_literal_string_literal<A>(
self,
span: Span,
value: A,
raw: Option<Atom<'a>>,
) -> TSLiteral<'a>
where
A: IntoIn<'a, Atom<'a>>,
{
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 @@ -10996,16 +11029,18 @@ impl<'a> AstBuilder<'a> {
/// ## Parameters
/// - span: Node location in source code
/// - value: The value of the string.
/// - raw: The raw string as it appears in source code.
#[inline]
pub fn ts_module_declaration_name_string_literal<A>(
self,
span: Span,
value: A,
raw: Option<Atom<'a>>,
) -> TSModuleDeclarationName<'a>
where
A: IntoIn<'a, Atom<'a>>,
{
TSModuleDeclarationName::StringLiteral(self.string_literal(span, value))
TSModuleDeclarationName::StringLiteral(self.string_literal(span, value, raw))
}

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

/// Build a [`TSFunctionType`].
Expand Down Expand Up @@ -12788,16 +12825,18 @@ impl<'a> AstBuilder<'a> {
/// ## Parameters
/// - span: Node location in source code
/// - value: The value of the string.
/// - raw: The raw string as it appears in source code.
#[inline]
pub fn jsx_attribute_value_string_literal<A>(
self,
span: Span,
value: A,
raw: Option<Atom<'a>>,
) -> JSXAttributeValue<'a>
where
A: IntoIn<'a, Atom<'a>>,
{
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
1 change: 1 addition & 0 deletions crates/oxc_ast/src/generated/derive_clone_in.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ impl<'old_alloc, 'new_alloc> CloneIn<'new_alloc> for StringLiteral<'old_alloc> {
StringLiteral {
span: CloneIn::clone_in(&self.span, allocator),
value: CloneIn::clone_in(&self.value, allocator),
raw: CloneIn::clone_in(&self.raw, allocator),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_ast/src/generated/derive_content_eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ 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)
&& ContentEq::content_eq(&self.raw, &other.raw)
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/oxc_ast/src/generated/derive_content_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ 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);
ContentHash::content_hash(&self.raw, state);
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_ecmascript/src/array_join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ mod tests {
NumberBase::Decimal,
))));
elements.push(ArrayExpressionElement::StringLiteral(
ast.alloc(ast.string_literal(SPAN, "foo")),
ast.alloc(ast.string_literal(SPAN, "foo", None)),
));
elements.push(ArrayExpressionElement::BooleanLiteral(
ast.alloc(ast.boolean_literal(SPAN, true)),
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_isolated_declarations/src/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl<'a> IsolatedDeclarations<'a> {
expr
}
}
ConstantValue::String(v) => self.ast.expression_string_literal(SPAN, v),
ConstantValue::String(v) => self.ast.expression_string_literal(SPAN, v, None),
}),
);

Expand Down
1 change: 1 addition & 0 deletions crates/oxc_isolated_declarations/src/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ impl<'a> IsolatedDeclarations<'a> {
self.ast.alloc(self.ast.string_literal(
lit.span,
if let Some(cooked) = &item.value.cooked { cooked } else { &item.value.raw },
None,
))
})
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,18 @@ impl PeepholeReplaceKnownMethods {
"toLowerCase" => Some(ctx.ast.expression_string_literal(
call_expr.span,
string_lit.value.cow_to_lowercase(),
None,
)),
"toUpperCase" => Some(ctx.ast.expression_string_literal(
call_expr.span,
string_lit.value.cow_to_uppercase(),
None,
)),
"trim" => Some(ctx.ast.expression_string_literal(
call_expr.span,
string_lit.value.trim(),
None,
)),
"trim" => Some(
ctx.ast.expression_string_literal(call_expr.span, string_lit.value.trim()),
),
_ => None,
},
"indexOf" | "lastIndexOf" => Self::try_fold_string_index_of(
Expand Down Expand Up @@ -167,6 +171,7 @@ impl PeepholeReplaceKnownMethods {
return Some(ctx.ast.expression_string_literal(
span,
string_lit.value.as_str().substring(start_idx, end_idx),
None,
));
}

Expand Down Expand Up @@ -200,7 +205,7 @@ impl PeepholeReplaceKnownMethods {
.char_at(char_at_index)
.map_or(String::new(), |v| v.to_string());

return Some(ctx.ast.expression_string_literal(span, result));
return Some(ctx.ast.expression_string_literal(span, result, None));
}

fn try_fold_string_char_code_at<'a>(
Expand Down Expand Up @@ -265,7 +270,7 @@ impl PeepholeReplaceKnownMethods {
_ => unreachable!(),
};

Some(ctx.ast.expression_string_literal(span, result))
Some(ctx.ast.expression_string_literal(span, result, None))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl<'a> Traverse<'a> for PeepholeSubstituteAlternateSyntax {
}
Expression::TemplateLiteral(_) => {
if let Some(val) = expr.to_js_string() {
*expr = ctx.ast.expression_string_literal(expr.span(), val);
*expr = ctx.ast.expression_string_literal(expr.span(), val, None);
self.changed = true;
}
}
Expand Down Expand Up @@ -271,7 +271,7 @@ impl<'a, 'b> PeepholeSubstituteAlternateSyntax {
};
let argument = Expression::Identifier(ctx.alloc(id_ref));
let left = ctx.ast.expression_unary(SPAN, UnaryOperator::Typeof, argument);
let right = ctx.ast.expression_string_literal(SPAN, "u");
let right = ctx.ast.expression_string_literal(SPAN, "u", None);
let binary_expr =
ctx.ast.binary_expression(expr.span, left, BinaryOperator::GreaterThan, right);
*expr = binary_expr;
Expand Down Expand Up @@ -521,7 +521,7 @@ impl<'a, 'b> PeepholeSubstituteAlternateSyntax {

Some(ctx.ast.expression_binary(
call_expr.span,
ctx.ast.expression_string_literal(SPAN, ""),
ctx.ast.expression_string_literal(SPAN, "", None),
BinaryOperator::Addition,
ctx.ast.move_expression(arg),
))
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_minifier/src/node_util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl<'a, 'b> Ctx<'a, 'b> {
ConstantValue::BigInt(n) => {
self.ast.expression_big_int_literal(span, n.to_string() + "n", BigintBase::Decimal)
}
ConstantValue::String(s) => self.ast.expression_string_literal(span, s),
ConstantValue::String(s) => self.ast.expression_string_literal(span, s, None),
ConstantValue::Boolean(b) => self.ast.expression_boolean_literal(span, b),
ConstantValue::Undefined => self.ast.void_0(span),
ConstantValue::Null => self.ast.expression_null_literal(span),
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_parser/src/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl<'a> ParserImpl<'a> {
pub(crate) fn cur_src(&self) -> &'a str {
let range = self.cur_token().span();
// SAFETY:
// range comes from the parser, which are ensured to meeting the criteria of `get_unchecked`.
// range comes from the lexer, which are ensured to meeting the criteria of `get_unchecked`.

unsafe { self.source_text.get_unchecked(range.start as usize..range.end as usize) }
}
Expand Down
8 changes: 7 additions & 1 deletion crates/oxc_parser/src/js/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,13 @@ impl<'a> ParserImpl<'a> {
let value = self.cur_string();
let span = self.start_span();
self.bump_any();
Ok(self.ast.string_literal(self.end_span(span), value))
let span = self.end_span(span);
// SAFETY:
// range comes from the lexer, which are ensured to meeting the criteria of `get_unchecked`.
let raw = Atom::from(unsafe {
self.source_text.get_unchecked(span.start as usize..span.end as usize)
});
Ok(self.ast.string_literal(self.end_span(span), value, Some(raw)))
}

/// Section [Array Expression](https://tc39.es/ecma262/#prod-ArrayLiteral)
Expand Down
4 changes: 4 additions & 0 deletions crates/oxc_parser/src/ts/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ impl<'a> ParserImpl<'a> {
Ok(self.ast.ts_enum_member_name_string_literal(
template.span,
template.quasi().unwrap(),
Some(Atom::from(
Span::new(template.span.start + 1, template.span.end - 1)
.source_text(self.source_text),
)),
))
}
Expression::NumericLiteral(literal) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1006,7 +1006,7 @@ impl<'a> ArrowFunctionConverter<'a> {
if ctx.scopes().root_scope_id() == target_scope_id {
let argument = Expression::Identifier(ctx.ast.alloc(reference));
let typeof_arguments = ctx.ast.expression_unary(SPAN, UnaryOperator::Typeof, argument);
let undefined_literal = ctx.ast.expression_string_literal(SPAN, "undefined");
let undefined_literal = ctx.ast.expression_string_literal(SPAN, "undefined", None);
let test = ctx.ast.expression_binary(
SPAN,
typeof_arguments,
Expand Down
Loading
Loading