From dc3e72510fc80a7c82a24a72155818c833df7c03 Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Fri, 21 Mar 2025 07:46:00 +0000 Subject: [PATCH] refactor(ast/estree): expose `INCLUDE_TS_FIELDS` constant on `Serializer` (#9943) Remove the `serialize_ts_element` method added in #9913, and instead expose an `INCLUDE_TS_FIELDS` constant on `Serializer` trait. Before: ```rs if let Some(this_param) = &self.0.this_param { seq.serialize_ts_element(this_param); } ``` After: ```rs if S::INCLUDE_TS_FIELDS { if let Some(this_param) = &self.0.this_param { seq.serialize_element(this_param); } } ``` This has one slight advantage. Because `INCLUDE_TS_FIELDS` is a constant, compiler will easily be able to prove that the code inside `if S::INCLUDE_TS_FIELDS { ... }` is dead code in the JS-only AST serializer, and remove it. In this specific case, the TS-only logic is trivial, so compiler would probably see it could be optimized out anyway. But we may encounter other cases where more complex TS-only code is required, and compiler might not be able to prove that it's dead code in those cases. --- crates/oxc_ast/src/serialize.rs | 6 ++++-- crates/oxc_estree/src/serialize/mod.rs | 6 ++++++ crates/oxc_estree/src/serialize/sequences.rs | 7 ------- crates/oxc_estree/src/serialize/structs.rs | 3 +++ 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/crates/oxc_ast/src/serialize.rs b/crates/oxc_ast/src/serialize.rs index 5857301aa3aac..123f904b4febb 100644 --- a/crates/oxc_ast/src/serialize.rs +++ b/crates/oxc_ast/src/serialize.rs @@ -684,8 +684,10 @@ impl ESTree for FunctionFormalParameters<'_, '_> { fn serialize(&self, serializer: S) { let mut seq = serializer.serialize_sequence(); - if let Some(this_param) = &self.0.this_param { - seq.serialize_ts_element(this_param); + if S::INCLUDE_TS_FIELDS { + if let Some(this_param) = &self.0.this_param { + seq.serialize_element(this_param); + } } for item in &self.0.params.items { diff --git a/crates/oxc_estree/src/serialize/mod.rs b/crates/oxc_estree/src/serialize/mod.rs index 5be882d28a2c5..9727f1855763c 100644 --- a/crates/oxc_estree/src/serialize/mod.rs +++ b/crates/oxc_estree/src/serialize/mod.rs @@ -30,6 +30,9 @@ pub trait ESTree { // Internal methods we don't want to expose outside this crate are in [`SerializerPrivate`] trait. #[expect(private_bounds)] pub trait Serializer: SerializerPrivate { + /// `true` if output should contain TS fields + const INCLUDE_TS_FIELDS: bool; + /// Type of struct serializer this serializer uses. type StructSerializer: StructSerializer; /// Type of sequence serializer this serializer uses. @@ -99,6 +102,9 @@ impl Default for ESTreeSerializer { } impl<'s, C: Config, F: Formatter> Serializer for &'s mut ESTreeSerializer { + /// `true` if output should contain TS fields + const INCLUDE_TS_FIELDS: bool = C::INCLUDE_TS_FIELDS; + type StructSerializer = ESTreeStructSerializer<'s, C, F>; type SequenceSerializer = ESTreeSequenceSerializer<'s, C, F>; diff --git a/crates/oxc_estree/src/serialize/sequences.rs b/crates/oxc_estree/src/serialize/sequences.rs index 8557725f12d23..142aeb6916f5f 100644 --- a/crates/oxc_estree/src/serialize/sequences.rs +++ b/crates/oxc_estree/src/serialize/sequences.rs @@ -4,7 +4,6 @@ use super::{Config, ESTree, ESTreeSerializer, Formatter, Serializer, SerializerP pub trait SequenceSerializer { /// Serialize sequence entry. fn serialize_element(&mut self, value: &T); - fn serialize_ts_element(&mut self, value: &T); /// Finish serializing sequence. fn end(self); @@ -44,12 +43,6 @@ impl SequenceSerializer for ESTreeSequenceSerializer<'_ value.serialize(&mut *self.serializer); } - fn serialize_ts_element(&mut self, value: &T) { - if C::INCLUDE_TS_FIELDS { - self.serialize_element(value); - } - } - /// Finish serializing sequence. fn end(mut self) { let (buffer, formatter) = self.serializer.buffer_and_formatter_mut(); diff --git a/crates/oxc_estree/src/serialize/structs.rs b/crates/oxc_estree/src/serialize/structs.rs index 45cdd3fabba87..46f62b1f9ea9c 100644 --- a/crates/oxc_estree/src/serialize/structs.rs +++ b/crates/oxc_estree/src/serialize/structs.rs @@ -145,6 +145,9 @@ pub(super) enum StructState { pub struct FlatStructSerializer<'p, P: StructSerializer>(pub &'p mut P); impl<'p, P: StructSerializer> Serializer for FlatStructSerializer<'p, P> { + /// `true` if output should contain TS fields + const INCLUDE_TS_FIELDS: bool = P::Config::INCLUDE_TS_FIELDS; + type StructSerializer = Self; type SequenceSerializer = ESTreeSequenceSerializer<'p, P::Config, P::Formatter>;