Skip to content

Commit 5b1e7fb

Browse files
committed
refactor(codegen)!: remove Codegen::enableSourceMap API
1 parent 558810d commit 5b1e7fb

File tree

7 files changed

+67
-52
lines changed

7 files changed

+67
-52
lines changed

crates/oxc/src/compiler.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,11 +283,10 @@ pub trait CompilerInterface {
283283
mangler: Option<Mangler>,
284284
options: CodegenOptions,
285285
) -> CodegenReturn {
286-
let mut codegen = CodeGenerator::new().with_options(options).with_mangler(mangler);
286+
let mut options = options;
287287
if self.enable_sourcemap() {
288-
codegen = codegen
289-
.enable_source_map(source_path.to_string_lossy().as_ref(), program.source_text);
288+
options.source_map_path = Some(source_path.to_path_buf());
290289
}
291-
codegen.build(program)
290+
CodeGenerator::new().with_options(options).with_mangler(mangler).build(program)
292291
}
293292
}

crates/oxc_codegen/examples/sourcemap.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{env, path::Path};
33

44
use base64::{prelude::BASE64_STANDARD, Engine};
55
use oxc_allocator::Allocator;
6-
use oxc_codegen::{CodeGenerator, CodegenReturn};
6+
use oxc_codegen::{CodeGenerator, CodegenOptions, CodegenReturn};
77
use oxc_parser::Parser;
88
use oxc_span::SourceType;
99

@@ -28,7 +28,10 @@ fn main() -> std::io::Result<()> {
2828
}
2929

3030
let CodegenReturn { code, map } = CodeGenerator::new()
31-
.enable_source_map(path.to_string_lossy().as_ref(), &source_text)
31+
.with_options(CodegenOptions {
32+
source_map_path: Some(path.to_path_buf()),
33+
..CodegenOptions::default()
34+
})
3235
.build(&ret.program);
3336

3437
if let Some(source_map) = map {

crates/oxc_codegen/src/lib.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ mod gen;
1010
mod operator;
1111
mod sourcemap_builder;
1212

13-
use std::borrow::Cow;
13+
use std::{borrow::Cow, path::PathBuf};
1414

1515
use oxc_ast::ast::{
1616
BindingIdentifier, BlockStatement, Expression, IdentifierReference, Program, Statement,
@@ -35,7 +35,7 @@ pub use crate::{
3535
/// Code generator without whitespace removal.
3636
pub type CodeGenerator<'a> = Codegen<'a>;
3737

38-
#[derive(Clone, Copy)]
38+
#[derive(Debug, Clone)]
3939
pub struct CodegenOptions {
4040
/// Use single quotes instead of double quotes.
4141
///
@@ -58,16 +58,24 @@ pub struct CodegenOptions {
5858
///
5959
/// Default is `false`.
6060
pub annotation_comments: bool,
61+
62+
pub source_map_path: Option<PathBuf>,
6163
}
6264

6365
impl Default for CodegenOptions {
6466
fn default() -> Self {
65-
Self { single_quote: false, minify: false, comments: true, annotation_comments: false }
67+
Self {
68+
single_quote: false,
69+
minify: false,
70+
comments: true,
71+
annotation_comments: false,
72+
source_map_path: None,
73+
}
6674
}
6775
}
6876

6977
impl CodegenOptions {
70-
fn print_annotation_comments(self) -> bool {
78+
fn print_annotation_comments(&self) -> bool {
7179
!self.minify && (self.comments || self.annotation_comments)
7280
}
7381
}
@@ -78,6 +86,8 @@ pub struct CodegenReturn {
7886
pub code: String,
7987

8088
/// The source map from the input source code to the generated source code.
89+
///
90+
/// You must set [`CodegenOptions::source_map_path`] for this to be [`Some`].
8191
pub map: Option<oxc_sourcemap::SourceMap>,
8292
}
8393

@@ -185,14 +195,6 @@ impl<'a> Codegen<'a> {
185195
self
186196
}
187197

188-
#[must_use]
189-
pub fn enable_source_map(mut self, source_name: &str, source_text: &str) -> Self {
190-
let mut sourcemap_builder = SourcemapBuilder::default();
191-
sourcemap_builder.with_name_and_source(source_name, source_text);
192-
self.sourcemap_builder = Some(sourcemap_builder);
193-
self
194-
}
195-
196198
#[must_use]
197199
pub fn with_mangler(mut self, mangler: Option<Mangler>) -> Self {
198200
self.mangler = mangler;
@@ -207,6 +209,9 @@ impl<'a> Codegen<'a> {
207209
if self.options.print_annotation_comments() {
208210
self.build_comments(&program.comments);
209211
}
212+
if let Some(path) = &self.options.source_map_path {
213+
self.sourcemap_builder = Some(SourcemapBuilder::new(path, program.source_text));
214+
}
210215

211216
program.print(&mut self, Context::default());
212217
let code = self.into_source_text();

crates/oxc_codegen/src/sourcemap_builder.rs

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::sync::Arc;
1+
use std::{path::Path, sync::Arc};
22

33
use nonmax::NonMaxU32;
44
use oxc_index::{Idx, IndexVec};
@@ -73,27 +73,23 @@ pub struct SourcemapBuilder {
7373
generated_column: u32,
7474
}
7575

76-
impl Default for SourcemapBuilder {
77-
fn default() -> Self {
76+
impl SourcemapBuilder {
77+
pub fn new(path: &Path, source_text: &str) -> Self {
78+
let mut sourcemap_builder = oxc_sourcemap::SourceMapBuilder::default();
79+
let line_offset_tables = Self::generate_line_offset_tables(source_text);
80+
let source_id =
81+
sourcemap_builder.set_source_and_content(path.to_string_lossy().as_ref(), source_text);
7882
Self {
79-
source_id: 0,
80-
original_source: "".into(),
83+
source_id,
84+
original_source: Arc::from(source_text),
8185
last_generated_update: 0,
8286
last_position: None,
83-
line_offset_tables: LineOffsetTables::default(),
84-
sourcemap_builder: oxc_sourcemap::SourceMapBuilder::default(),
87+
line_offset_tables,
88+
sourcemap_builder,
8589
generated_line: 0,
8690
generated_column: 0,
8791
}
8892
}
89-
}
90-
91-
impl SourcemapBuilder {
92-
pub fn with_name_and_source(&mut self, name: &str, source: &str) {
93-
self.line_offset_tables = Self::generate_line_offset_tables(source);
94-
self.source_id = self.sourcemap_builder.set_source_and_content(name, source);
95-
self.original_source = source.into();
96-
}
9793

9894
pub fn into_sourcemap(self) -> oxc_sourcemap::SourceMap {
9995
self.sourcemap_builder.into_sourcemap()
@@ -392,8 +388,7 @@ mod test {
392388
}
393389

394390
fn assert_mapping(source: &str, mappings: &[(u32, u32, u32)]) {
395-
let mut builder = SourcemapBuilder::default();
396-
builder.with_name_and_source("x.js", source);
391+
let mut builder = SourcemapBuilder::new(Path::new("x.js"), source);
397392
for (position, expected_line, expected_col) in mappings.iter().copied() {
398393
let (line, col) = builder.search_original_line_and_column(position);
399394
assert_eq!(
@@ -407,8 +402,7 @@ mod test {
407402
#[test]
408403
fn add_source_mapping() {
409404
fn create_mappings(source: &str, line: u32, column: u32) {
410-
let mut builder = SourcemapBuilder::default();
411-
builder.with_name_and_source("x.js", source);
405+
let mut builder = SourcemapBuilder::new(Path::new("x.js"), source);
412406
let output: Vec<u8> = source.as_bytes().into();
413407
for (i, _ch) in source.char_indices() {
414408
#[allow(clippy::cast_possible_truncation)]
@@ -444,8 +438,7 @@ mod test {
444438
#[test]
445439
fn add_source_mapping_for_name() {
446440
let output = "ac".as_bytes();
447-
let mut builder = SourcemapBuilder::default();
448-
builder.with_name_and_source("x.js", "ab");
441+
let mut builder = SourcemapBuilder::new(Path::new("x.js"), "ab");
449442
builder.add_source_mapping_for_name(output, Span::new(0, 1), "a");
450443
builder.add_source_mapping_for_name(output, Span::new(1, 2), "c");
451444
let sm = builder.into_sourcemap();
@@ -464,8 +457,7 @@ mod test {
464457
#[test]
465458
fn add_source_mapping_for_unordered_position() {
466459
let output = "".as_bytes();
467-
let mut builder = SourcemapBuilder::default();
468-
builder.with_name_and_source("x.js", "ab");
460+
let mut builder = SourcemapBuilder::new(Path::new("x.js"), "ab");
469461
builder.add_source_mapping(output, 1, None);
470462
builder.add_source_mapping(output, 0, None);
471463
let sm = builder.into_sourcemap();

napi/transform/src/isolated_declaration.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use napi_derive::napi;
44

55
use oxc::{
66
allocator::Allocator,
7-
codegen::CodeGenerator,
7+
codegen::{CodeGenerator, CodegenOptions},
88
isolated_declarations::IsolatedDeclarations,
99
napi::{
1010
isolated_declarations::{IsolatedDeclarationsOptions, IsolatedDeclarationsResult},
@@ -39,11 +39,12 @@ pub fn isolated_declaration(
3939
)
4040
.build(&ret.program);
4141

42-
let mut codegen = CodeGenerator::new();
43-
if options.sourcemap == Some(true) {
44-
codegen = codegen.enable_source_map(&filename, &source_text);
45-
}
46-
let codegen_ret = codegen.build(&transformed_ret.program);
42+
let codegen_ret = CodeGenerator::new()
43+
.with_options(CodegenOptions {
44+
source_map_path: Some(source_path.to_path_buf()),
45+
..CodegenOptions::default()
46+
})
47+
.build(&transformed_ret.program);
4748

4849
let errors = ret.errors.into_iter().chain(transformed_ret.errors).collect();
4950
let errors = wrap_diagnostics(source_path, source_type, &source_text, errors);

tasks/benchmark/benches/codegen.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
use std::path::PathBuf;
2+
13
use oxc_allocator::Allocator;
24
use oxc_benchmark::{criterion_group, criterion_main, BenchmarkId, Criterion};
3-
use oxc_codegen::CodeGenerator;
5+
use oxc_codegen::{CodeGenerator, CodegenOptions};
46
use oxc_parser::Parser;
57
use oxc_span::SourceType;
68
use oxc_tasks_common::TestFiles;
@@ -22,7 +24,12 @@ fn bench_codegen(criterion: &mut Criterion) {
2224
let mut group = criterion.benchmark_group("codegen_sourcemap");
2325
group.bench_with_input(id, &ret.program, |b, program| {
2426
b.iter_with_large_drop(|| {
25-
CodeGenerator::new().enable_source_map(&file.file_name, source_text).build(program)
27+
CodeGenerator::new()
28+
.with_options(CodegenOptions {
29+
source_map_path: Some(PathBuf::from(&file.file_name)),
30+
..CodegenOptions::default()
31+
})
32+
.build(program)
2633
});
2734
});
2835
group.finish();

tasks/benchmark/benches/sourcemap.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
use std::path::PathBuf;
2+
13
use oxc_allocator::Allocator;
24
use oxc_benchmark::{criterion_group, criterion_main, BenchmarkId, Criterion};
3-
use oxc_codegen::{CodeGenerator, CodegenReturn};
5+
use oxc_codegen::{CodeGenerator, CodegenOptions, CodegenReturn};
46
use oxc_parser::Parser;
57
use oxc_sourcemap::ConcatSourceMapBuilder;
68
use oxc_span::SourceType;
@@ -18,13 +20,19 @@ fn bench_sourcemap(criterion: &mut Criterion) {
1820
let ret = Parser::new(&allocator, source_text, source_type).parse();
1921

2022
let CodegenReturn { code: output_txt, .. } = CodeGenerator::new()
21-
.enable_source_map(file.file_name.as_str(), source_text)
23+
.with_options(CodegenOptions {
24+
source_map_path: Some(PathBuf::from(&file.file_name)),
25+
..CodegenOptions::default()
26+
})
2227
.build(&ret.program);
2328
let lines = output_txt.matches('\n').count() as u32;
2429

2530
b.iter(|| {
2631
let CodegenReturn { map, .. } = CodeGenerator::new()
27-
.enable_source_map(file.file_name.as_str(), source_text)
32+
.with_options(CodegenOptions {
33+
source_map_path: Some(PathBuf::from(&file.file_name)),
34+
..CodegenOptions::default()
35+
})
2836
.build(&ret.program);
2937
if let Some(sourcemap) = map {
3038
let concat_sourcemap_builder = ConcatSourceMapBuilder::from_sourcemaps(&[

0 commit comments

Comments
 (0)