Skip to content

Commit 39631b5

Browse files
feat(oxc_codegen): support configure initial indent when using oxc_codegen (#13091)
Here is an example in rolldown https://github.com/rolldown/rolldown/pull/5724/files#diff-8a1733ecebbc28acb7d0c6ccd52499d7d2c9611f046b0b4a30ee3e1f939cbf13R57-R74, when concate multiple module, rolldown uses the output of `oxc_codegen` for AST and adding head and tail of the concatenatedModules with string replace to make sure generates the correct sourcemap with minimum effort. In this scenario, rolldown needs `oxc_codegen` to start generating string with initial ident `1`
1 parent 74fb6c9 commit 39631b5

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

crates/oxc_codegen/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ impl<'a> Codegen<'a> {
197197
pub fn build(mut self, program: &Program<'a>) -> CodegenReturn {
198198
self.quote = if self.options.single_quote { Quote::Single } else { Quote::Double };
199199
self.source_text = Some(program.source_text);
200+
self.indent = self.options.initial_indent;
200201
self.code.reserve(program.source_text.len());
201202
self.build_comments(&program.comments);
202203
if let Some(path) = &self.options.source_map_path {

crates/oxc_codegen/src/options.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ pub struct CodegenOptions {
3838
///
3939
/// Default is `1`.
4040
pub indent_width: usize,
41+
42+
/// Initial indentation level for generated code.
43+
///
44+
/// Default is `0`.
45+
pub initial_indent: u32,
4146
}
4247

4348
impl Default for CodegenOptions {
@@ -49,6 +54,7 @@ impl Default for CodegenOptions {
4954
source_map_path: None,
5055
indent_char: IndentChar::default(),
5156
indent_width: DEFAULT_INDENT_WIDTH,
57+
initial_indent: 0,
5258
}
5359
}
5460
}
@@ -63,6 +69,7 @@ impl CodegenOptions {
6369
source_map_path: None,
6470
indent_char: IndentChar::default(),
6571
indent_width: DEFAULT_INDENT_WIDTH,
72+
initial_indent: 0,
6673
}
6774
}
6875

crates/oxc_codegen/tests/integration/js.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,4 +647,11 @@ fn indentation() {
647647
..CodegenOptions::default()
648648
},
649649
);
650+
651+
// Test spaces with width 4
652+
test_options(
653+
"let foo = 1;",
654+
"\tlet foo = 1;\n",
655+
CodegenOptions { initial_indent: 1, ..CodegenOptions::default() },
656+
);
650657
}

0 commit comments

Comments
 (0)