mirror of
https://github.com/oxc-project/oxc.git
synced 2026-11-04 10:29:58 +00:00
feat(estree): add IS_COMPACT const to Formatter trait (#19787)
Add `IS_COMPACT` const to `Formatter` and `Serializer` traits. In conjunction with #19773, callers can manually construct JSON themselves in the correct format for the serializer (compact or pretty-printed). This is used in #19744 to net a +9% perf gain in ESTree token serialization.
This commit is contained in:
1 parent
e2a1b795ca
commit
b25228a789
2 files changed
+11
-1
No files matched your search
@@ -2,6 +2,9 @@ use oxc_data_structures::code_buffer::CodeBuffer;
|
||||
|
||||
/// Formatter trait.
|
||||
pub trait Formatter {
|
||||
/// `true` if formatter produces compact JSON (not pretty-printed JSON).
|
||||
const IS_COMPACT: bool;
|
||||
|
||||
/// Create new [`Formatter`].
|
||||
fn new() -> Self;
|
||||
|
||||
@@ -30,6 +33,8 @@ pub trait Formatter {
|
||||
pub struct CompactFormatter;
|
||||
|
||||
impl Formatter for CompactFormatter {
|
||||
const IS_COMPACT: bool = true;
|
||||
|
||||
#[inline(always)]
|
||||
fn new() -> Self {
|
||||
Self
|
||||
@@ -71,6 +76,8 @@ pub struct PrettyFormatter {
|
||||
}
|
||||
|
||||
impl Formatter for PrettyFormatter {
|
||||
const IS_COMPACT: bool = false;
|
||||
|
||||
#[inline(always)]
|
||||
fn new() -> Self {
|
||||
Self { indent: 0 }
|
||||
|
||||
@@ -35,9 +35,12 @@ pub trait ESTree {
|
||||
|
||||
/// Trait for serializers.
|
||||
pub trait Serializer {
|
||||
/// `true` if output should contain TS fields
|
||||
/// `true` if output should contain TS fields.
|
||||
const INCLUDE_TS_FIELDS: bool;
|
||||
|
||||
/// `true` if serializer's formatter produces compact JSON (not pretty-printed JSON).
|
||||
const IS_COMPACT: bool = Self::Formatter::IS_COMPACT;
|
||||
|
||||
/// Type of `Formatter` this serializer uses.
|
||||
type Formatter: Formatter;
|
||||
|
||||
|
||||
Reference in new issue
Block a user