From cbba26c23731274acf0b345e3edf3047701c0e00 Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Thu, 5 Dec 2024 04:00:56 +0000 Subject: [PATCH] fix(estree): `raw: null` in ESTree AST for generated `NullLiteral`s (#7662) In JS-side AST, leave `raw` field as `null` for `NullLiteral`s, if they are generated and have no raw representation in source text. --- crates/oxc_ast/src/ast/literal.rs | 2 +- crates/oxc_ast/src/serialize.rs | 3 ++- npm/oxc-types/types.d.ts | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/crates/oxc_ast/src/ast/literal.rs b/crates/oxc_ast/src/ast/literal.rs index d01d2eb645560..e64c6a05098c8 100644 --- a/crates/oxc_ast/src/ast/literal.rs +++ b/crates/oxc_ast/src/ast/literal.rs @@ -34,7 +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\"")] +#[estree(type = "Literal", via = crate::serialize::ESTreeLiteral, add_ts = "value: null, raw: \"null\" | null")] pub struct NullLiteral { /// Node location in source code pub span: Span, diff --git a/crates/oxc_ast/src/serialize.rs b/crates/oxc_ast/src/serialize.rs index 90d7706916fa7..df79e26a61940 100644 --- a/crates/oxc_ast/src/serialize.rs +++ b/crates/oxc_ast/src/serialize.rs @@ -44,7 +44,8 @@ impl From<&BooleanLiteral> for ESTreeLiteral<'_, bool> { impl From<&NullLiteral> for ESTreeLiteral<'_, ()> { fn from(lit: &NullLiteral) -> Self { - Self { span: lit.span, value: (), raw: Some("null"), bigint: None, regex: None } + let raw = if lit.span.is_unspanned() { None } else { Some("null") }; + Self { span: lit.span, value: (), raw, bigint: None, regex: None } } } diff --git a/npm/oxc-types/types.d.ts b/npm/oxc-types/types.d.ts index 48072043c4080..46c3292db4e8b 100644 --- a/npm/oxc-types/types.d.ts +++ b/npm/oxc-types/types.d.ts @@ -10,7 +10,7 @@ export interface BooleanLiteral extends Span { export interface NullLiteral extends Span { type: 'Literal'; value: null; - raw: 'null'; + raw: 'null' | null; } export interface NumericLiteral extends Span {