feat(ast): serialize StringLiterals to ESTree without raw (#7263)

#7211 alternative. Makes the `raw` field on estree literals optional.
This commit is contained in:
ottomated 2024-11-13 15:37:30 +00:00
parent e84ea2c3c6
commit 897d3b1567
4 changed files with 22 additions and 12 deletions

View file

@ -65,6 +65,7 @@ pub struct NumericLiteral<'a> {
#[ast(visit)] #[ast(visit)]
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash, ESTree)] #[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash, ESTree)]
#[estree(type = "Literal", via = crate::serialize::ESTreeLiteral, add_ts = "raw?: undefined")]
pub struct StringLiteral<'a> { pub struct StringLiteral<'a> {
/// Node location in source code /// Node location in source code
pub span: Span, pub span: Span,

View file

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

View file

@ -22,7 +22,7 @@ pub struct ESTreeLiteral<'a, T> {
#[serde(flatten)] #[serde(flatten)]
span: Span, span: Span,
value: T, value: T,
raw: &'a str, raw: Option<&'a str>,
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
bigint: Option<String>, bigint: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
@ -34,7 +34,7 @@ impl<'a> From<&BooleanLiteral> for ESTreeLiteral<'a, bool> {
Self { Self {
span: value.span, span: value.span,
value: value.value, value: value.value,
raw: if value.value { "true" } else { "false" }, raw: Some(if value.value { "true" } else { "false" }),
bigint: None, bigint: None,
regex: None, regex: None,
} }
@ -43,13 +43,25 @@ impl<'a> From<&BooleanLiteral> for ESTreeLiteral<'a, bool> {
impl<'a> From<&NullLiteral> for ESTreeLiteral<'a, ()> { impl<'a> From<&NullLiteral> for ESTreeLiteral<'a, ()> {
fn from(value: &NullLiteral) -> Self { fn from(value: &NullLiteral) -> Self {
Self { span: value.span, value: (), raw: "null", bigint: None, regex: None } Self { span: value.span, value: (), raw: Some("null"), bigint: None, regex: None }
} }
} }
impl<'a> From<&'a NumericLiteral<'a>> for ESTreeLiteral<'a, f64> { impl<'a> From<&'a NumericLiteral<'a>> for ESTreeLiteral<'a, f64> {
fn from(value: &'a NumericLiteral) -> Self { fn from(value: &'a NumericLiteral) -> Self {
Self { span: value.span, value: value.value, raw: value.raw, bigint: None, regex: None } Self {
span: value.span,
value: value.value,
raw: Some(value.raw),
bigint: None,
regex: None,
}
}
}
impl<'a> From<&'a StringLiteral<'a>> for ESTreeLiteral<'a, &'a str> {
fn from(value: &'a StringLiteral) -> Self {
Self { span: value.span, value: &value.value, raw: None, bigint: None, regex: None }
} }
} }
@ -73,7 +85,7 @@ impl<'a> From<&'a BigIntLiteral<'a>> for ESTreeLiteral<'a, ()> {
span: value.span, span: value.span,
// BigInts can't be serialized to JSON // BigInts can't be serialized to JSON
value: (), value: (),
raw: value.raw.as_str(), raw: Some(value.raw.as_str()),
bigint: Some(bigint.to_string()), bigint: Some(bigint.to_string()),
regex: None, regex: None,
} }
@ -94,7 +106,7 @@ impl<'a> From<&'a RegExpLiteral<'a>> for ESTreeLiteral<'a, Option<EmptyObject>>
fn from(value: &'a RegExpLiteral) -> Self { fn from(value: &'a RegExpLiteral) -> Self {
Self { Self {
span: value.span, span: value.span,
raw: value.raw, raw: Some(value.raw),
value: match &value.regex.pattern { value: match &value.regex.pattern {
RegExpPattern::Pattern(_) => Some(EmptyObject {}), RegExpPattern::Pattern(_) => Some(EmptyObject {}),
_ => None, _ => None,

View file

@ -20,8 +20,9 @@ export interface NumericLiteral extends Span {
} }
export interface StringLiteral extends Span { export interface StringLiteral extends Span {
type: 'StringLiteral'; type: 'Literal';
value: string; value: string;
raw?: undefined;
} }
export interface BigIntLiteral extends Span { export interface BigIntLiteral extends Span {