Skip to content

Commit

Permalink
refactor(codegen, data_structures): move CodeBuffer into `oxc_data_…
Browse files Browse the repository at this point in the history
…structures` crate (#9326)

Pure refactor. Move `CodeBuffer` from `oxc_codegen` crate into `oxc_data_structures`, so `ESTree` serializer can also use it for constructing JSON string.
  • Loading branch information
overlookmotel committed Feb 24, 2025
1 parent 35ee399 commit 9d98444
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 20 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion crates/oxc_codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ oxc_sourcemap = { workspace = true }
oxc_span = { workspace = true }
oxc_syntax = { workspace = true }

assert-unchecked = { workspace = true }
bitflags = { workspace = true }
cow-utils = { workspace = true }
nonmax = { workspace = true }
Expand Down
7 changes: 3 additions & 4 deletions crates/oxc_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#![warn(missing_docs)]

mod binary_expr_visitor;
mod code_buffer;
mod comment;
mod context;
mod r#gen;
Expand All @@ -19,7 +18,7 @@ use oxc_ast::ast::{
BindingIdentifier, BlockStatement, Comment, Expression, IdentifierReference, Program,
Statement, StringLiteral,
};
use oxc_data_structures::stack::Stack;
use oxc_data_structures::{code_buffer::CodeBuffer, stack::Stack};
use oxc_semantic::SymbolTable;
use oxc_span::{GetSpan, SPAN, Span};
use oxc_syntax::{
Expand All @@ -29,8 +28,8 @@ use oxc_syntax::{
};

use crate::{
binary_expr_visitor::BinaryExpressionVisitor, code_buffer::CodeBuffer, comment::CommentsMap,
operator::Operator, sourcemap_builder::SourcemapBuilder,
binary_expr_visitor::BinaryExpressionVisitor, comment::CommentsMap, operator::Operator,
sourcemap_builder::SourcemapBuilder,
};
pub use crate::{
context::Context,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! A string builder for constructing source code.
use std::iter;

use assert_unchecked::assert_unchecked;
Expand All @@ -12,7 +14,7 @@ use assert_unchecked::assert_unchecked;
///
/// # Example
/// ```
/// use oxc_codegen::CodeBuffer;
/// # use oxc_data_structures::CodeBuffer;
/// let mut code = CodeBuffer::new();
///
/// // mock settings
Expand Down Expand Up @@ -40,7 +42,7 @@ impl CodeBuffer {
///
/// # Example
/// ```
/// use oxc_codegen::CodeBuffer;
/// # use oxc_data_structures::CodeBuffer;
/// let mut code = CodeBuffer::new();
///
/// // use `code` to build new source text
Expand Down Expand Up @@ -90,7 +92,7 @@ impl CodeBuffer {
///
/// # Example
/// ```
/// use oxc_codegen::CodeBuffer;
/// # use oxc_data_structures::CodeBuffer;
/// let mut code = CodeBuffer::new();
/// assert!(code.is_empty());
///
Expand All @@ -113,7 +115,7 @@ impl CodeBuffer {
///
/// # Example
/// ```
/// use oxc_codegen::CodeBuffer;
/// # use oxc_data_structures::CodeBuffer;
/// let mut code = CodeBuffer::default();
/// code.reserve(10);
/// ```
Expand All @@ -129,7 +131,7 @@ impl CodeBuffer {
///
/// # Example
/// ```
/// use oxc_codegen::CodeBuffer;
/// # use oxc_data_structures::CodeBuffer;
/// let mut code = CodeBuffer::new();
/// code.print_str("foo");
///
Expand All @@ -151,7 +153,7 @@ impl CodeBuffer {
///
/// # Example
/// ```
/// use oxc_codegen::CodeBuffer;
/// # use oxc_data_structures::CodeBuffer;
/// let mut code = CodeBuffer::new();
/// code.print_str("foo");
///
Expand Down Expand Up @@ -185,7 +187,7 @@ impl CodeBuffer {
///
/// # Example
/// ```
/// use oxc_codegen::CodeBuffer;
/// # use oxc_data_structures::CodeBuffer;
/// let mut code = CodeBuffer::new();
/// code.print_ascii_byte(b'f');
/// code.print_ascii_byte(b'o');
Expand Down Expand Up @@ -222,7 +224,7 @@ impl CodeBuffer {
///
/// # Example
/// ```
/// use oxc_codegen::CodeBuffer;
/// # use oxc_data_structures::CodeBuffer;
/// let mut code = CodeBuffer::new();
/// // Safe: 'a' is a valid ASCII character. Its UTF-8 representation only
/// // requires a single byte.
Expand Down Expand Up @@ -283,7 +285,7 @@ impl CodeBuffer {
///
/// # Example
/// ```
/// use oxc_codegen::CodeBuffer;
/// # use oxc_data_structures::CodeBuffer;
/// let mut code = CodeBuffer::new();
///
/// code.print_char('f');
Expand All @@ -305,7 +307,7 @@ impl CodeBuffer {
///
/// # Example
/// ```
/// use oxc_codegen::CodeBuffer;
/// # use oxc_data_structures::CodeBuffer;
/// let mut code = CodeBuffer::new();
/// code.print_str("function main() { console.log('Hello, world!') }");
/// ```
Expand All @@ -321,7 +323,7 @@ impl CodeBuffer {
///
/// # Example
/// ```
/// use oxc_codegen::CodeBuffer;
/// # use oxc_data_structures::CodeBuffer;
/// let mut code = CodeBuffer::new();
///
/// code.print_ascii_bytes([b'f', b'o', b'o']);
Expand Down Expand Up @@ -353,7 +355,7 @@ impl CodeBuffer {
///
/// # Example
/// ```
/// use oxc_codegen::CodeBuffer;
/// # use oxc_data_structures::CodeBuffer;
/// let mut code = CodeBuffer::new();
///
/// // Indent to a dynamic level.
Expand Down Expand Up @@ -425,7 +427,7 @@ impl CodeBuffer {
///
/// # Example
/// ```
/// use oxc_codegen::CodeBuffer;
/// # use oxc_data_structures::CodeBuffer;
/// let mut code = CodeBuffer::new();
/// code.print_str("foo");
/// assert_eq!(code.as_bytes(), &[b'f', b'o', b'o']);
Expand All @@ -439,13 +441,14 @@ impl CodeBuffer {
///
/// # Example
/// ```
/// use oxc_codegen::CodeBuffer;
/// # use oxc_data_structures::CodeBuffer;
/// let mut code = CodeBuffer::new();
/// code.print_str("console.log('foo');");
///
/// let source = code.into_string();
/// assert_eq!(source, "console.log('foo');");
/// ```
#[expect(clippy::missing_panics_doc)]
#[must_use]
#[inline]
pub fn into_string(self) -> String {
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_data_structures/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
#![warn(missing_docs)]

pub mod code_buffer;
pub mod rope;
pub mod stack;

0 comments on commit 9d98444

Please sign in to comment.