Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions crates/oxc_codegen/src/code_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,20 @@ impl CodeBuffer {
self.buf.extend(bytes);
}

/// Get contents of `CodeBuffer` as a byte slice.
///
/// # Examples
/// ```
/// use oxc_codegen::CodeBuffer;
/// let mut code = CodeBuffer::new();
/// code.print_str("foo");
/// assert_eq!(code.as_bytes(), &[b'f', b'o', b'o']);
/// ```
#[inline]
pub fn as_bytes(&self) -> &[u8] {
&self.buf
}

/// Convert a `CodeBuffer` into a string of source code, leaving its
/// internal buffer empty and finalizing the codegen process.
///
Expand Down Expand Up @@ -344,7 +358,7 @@ impl CodeBuffer {

impl AsRef<[u8]> for CodeBuffer {
fn as_ref(&self) -> &[u8] {
&self.buf
self.as_bytes()
}
}

Expand Down Expand Up @@ -394,7 +408,7 @@ mod test {
assert!(code.is_empty());
assert_eq!(code.len(), 0);
let empty_slice: &[u8] = &[];
assert_eq!(code.as_ref(), empty_slice);
assert_eq!(code.as_bytes(), empty_slice);
assert_eq!(String::from(code), "");
}

Expand All @@ -407,7 +421,7 @@ mod test {
code.print_ascii_byte(b'o');

assert_eq!(code.len(), 3);
assert_eq!(code.as_ref(), &[b'f', b'o', b'o']);
assert_eq!(code.as_bytes(), &[b'f', b'o', b'o']);
assert_eq!(String::from(code), "foo");
}

Expand All @@ -423,7 +437,7 @@ mod test {
}

assert_eq!(code.len(), 3);
assert_eq!(code.as_ref(), &[b'f', b'o', b'o']);
assert_eq!(code.as_bytes(), &[b'f', b'o', b'o']);
assert_eq!(String::from(code), "foo");
}

Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,13 +556,13 @@ impl<'a> Codegen<'a> {

fn add_source_mapping(&mut self, position: u32) {
if let Some(sourcemap_builder) = self.sourcemap_builder.as_mut() {
sourcemap_builder.add_source_mapping(self.code.as_ref(), position, None);
sourcemap_builder.add_source_mapping(self.code.as_bytes(), position, None);
}
}

fn add_source_mapping_for_name(&mut self, span: Span, name: &str) {
if let Some(sourcemap_builder) = self.sourcemap_builder.as_mut() {
sourcemap_builder.add_source_mapping_for_name(self.code.as_ref(), span, name);
sourcemap_builder.add_source_mapping_for_name(self.code.as_bytes(), span, name);
}
}
}