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
2 changes: 1 addition & 1 deletion crates/oxc/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl CompilerInterface for Compiler {
}

fn after_codegen(&mut self, ret: CodegenReturn) {
self.printed = ret.source_text;
self.printed = ret.code;
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_codegen/examples/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,5 @@ fn codegen(source_text: &str, ret: &ParserReturn<'_>, minify: bool) -> String {
)
.with_options(CodegenOptions { minify, ..CodegenOptions::default() })
.build(&ret.program)
.source_text
.code
}
13 changes: 4 additions & 9 deletions crates/oxc_codegen/examples/sourcemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,14 @@ fn main() -> std::io::Result<()> {
return Ok(());
}

let CodegenReturn { source_text, source_map } = CodeGenerator::new()
let CodegenReturn { code, map } = CodeGenerator::new()
.enable_source_map(path.to_string_lossy().as_ref(), &source_text)
.build(&ret.program);

if let Some(source_map) = source_map {
if let Some(source_map) = map {
let result = source_map.to_json_string();
let hash = BASE64_STANDARD.encode(format!(
"{}\0{}{}\0{}",
source_text.len(),
source_text,
result.len(),
result
));
let hash =
BASE64_STANDARD.encode(format!("{}\0{}{}\0{}", code.len(), code, result.len(), result));
println!("https://evanw.github.io/source-map-visualization/#{hash}");
}

Expand Down
10 changes: 5 additions & 5 deletions crates/oxc_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ pub struct CommentOptions {
/// Output from [`Codegen::build`]
pub struct CodegenReturn {
/// The generated source code.
pub source_text: String,
pub code: String,
/// The source map from the input source code to the generated source code.
///
/// You must use [`Codegen::enable_source_map`] for this to be [`Some`].
pub source_map: Option<oxc_sourcemap::SourceMap>,
pub map: Option<oxc_sourcemap::SourceMap>,
}

pub struct Codegen<'a> {
Expand Down Expand Up @@ -228,9 +228,9 @@ impl<'a> Codegen<'a> {
#[must_use]
pub fn build(mut self, program: &Program<'_>) -> CodegenReturn {
program.print(&mut self, Context::default());
let source_text = self.into_source_text();
let source_map = self.sourcemap_builder.map(SourcemapBuilder::into_sourcemap);
CodegenReturn { source_text, source_map }
let code = self.into_source_text();
let map = self.sourcemap_builder.map(SourcemapBuilder::into_sourcemap);
CodegenReturn { code, map }
}

#[must_use]
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_codegen/tests/integration/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn codegen(source_text: &str) -> String {
CommentOptions { preserve_annotate_comments: true },
)
.build(&ret.program)
.source_text
.code
}

pub fn snapshot(name: &str, cases: &[&str]) {
Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_codegen/tests/integration/tester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ pub fn test(source_text: &str, expected: &str) {
CommentOptions { preserve_annotate_comments: true },
)
.build(&ret.program)
.source_text;
.code;
assert_eq!(result, expected, "\nfor source: {source_text:?}");
}

pub fn test_without_source(source_text: &str, expected: &str) {
let source_type = SourceType::jsx();
let allocator = Allocator::default();
let ret = Parser::new(&allocator, source_text, source_type).parse();
let result = CodeGenerator::new().build(&ret.program).source_text;
let result = CodeGenerator::new().build(&ret.program).code;
assert_eq!(result, expected, "\nfor source: {source_text:?}");
}

Expand All @@ -33,6 +33,6 @@ pub fn test_minify(source_text: &str, expected: &str) {
let result = CodeGenerator::new()
.with_options(CodegenOptions { minify: true, ..CodegenOptions::default() })
.build(&ret.program)
.source_text;
.code;
assert_eq!(result, expected, "\nfor minify source: {source_text}");
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fn main() {
CommentOptions { preserve_annotate_comments: false },
)
.build(&id_ret.program)
.source_text;
.code;

println!("Dts Emit:\n");
println!("{printed}\n");
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_isolated_declarations/tests/deno/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ mod tests {
IsolatedDeclarationsOptions { strip_internal: true },
)
.build(&ret.program);
let actual = CodeGenerator::new().build(&ret.program).source_text;
let actual = CodeGenerator::new().build(&ret.program).code;
let expected_program = Parser::new(&allocator, expected, source_type).parse().program;
let expected = CodeGenerator::new().build(&expected_program).source_text;
let expected = CodeGenerator::new().build(&expected_program).code;
assert_eq!(actual.trim(), expected.trim());
}

Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_isolated_declarations/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn transform(path: &Path, source_text: &str) -> String {
CommentOptions { preserve_annotate_comments: false },
)
.build(&id_ret.program)
.source_text;
.code;

let mut snapshot =
format!("```\n==================== .D.TS ====================\n\n{code}\n\n");
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_minifier/examples/mangler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ fn mangler(source_text: &str, source_type: SourceType, debug: bool) -> String {
let ret = Parser::new(&allocator, source_text, source_type).parse();
let program = allocator.alloc(ret.program);
let mangler = Mangler::new().with_options(MangleOptions { debug }).build(program);
CodeGenerator::new().with_mangler(Some(mangler)).build(program).source_text
CodeGenerator::new().with_mangler(Some(mangler)).build(program).code
}
2 changes: 1 addition & 1 deletion crates/oxc_minifier/examples/minifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ fn minify(
let mut program = ret.program;
let options = MinifierOptions { mangle, compress: CompressOptions::default() };
let ret = Minifier::new(options).build(allocator, &mut program);
CodeGenerator::new().with_mangler(ret.mangler).build(&program).source_text
CodeGenerator::new().with_mangler(ret.mangler).build(&program).code
}
2 changes: 1 addition & 1 deletion crates/oxc_minifier/src/tester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ fn run<'a, P: CompressorPass<'a>>(
CodeGenerator::new()
.with_options(CodegenOptions { single_quote: true, ..CodegenOptions::default() })
.build(&program)
.source_text
.code
}
2 changes: 1 addition & 1 deletion crates/oxc_minifier/tests/mangler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn mangle(source_text: &str) -> String {
let ret = Parser::new(&allocator, source_text, source_type).parse();
let program = ret.program;
let mangler = Mangler::new().build(&program);
CodeGenerator::new().with_mangler(Some(mangler)).build(&program).source_text
CodeGenerator::new().with_mangler(Some(mangler)).build(&program).code
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_minifier/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ fn run(source_text: &str, source_type: SourceType, options: Option<CompressOptio
CodeGenerator::new()
.with_options(CodegenOptions { single_quote: true, ..CodegenOptions::default() })
.build(program)
.source_text
.code
}
2 changes: 1 addition & 1 deletion crates/oxc_transformer/examples/transformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ fn main() {
}
}

let printed = CodeGenerator::new().build(&program).source_text;
let printed = CodeGenerator::new().build(&program).code;
println!("Transformed:\n");
println!("{printed}");
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub(crate) fn test(source_text: &str, expected: &str, config: InjectGlobalVariab
let result = CodeGenerator::new()
.with_options(CodegenOptions { single_quote: true, ..CodegenOptions::default() })
.build(program)
.source_text;
.code;
let expected = run(expected, source_type);
assert_eq!(result, expected, "for source {source_text}");
}
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_transformer/tests/plugins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ fn run(source_text: &str, source_type: SourceType) -> String {
CodeGenerator::new()
.with_options(CodegenOptions { single_quote: true, ..CodegenOptions::default() })
.build(program)
.source_text
.code
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub(crate) fn test(source_text: &str, expected: &str, config: ReplaceGlobalDefin
let result = CodeGenerator::new()
.with_options(CodegenOptions { single_quote: true, ..CodegenOptions::default() })
.build(program)
.source_text;
.code;
let expected = run(expected, source_type);
assert_eq!(result, expected, "for source {source_text}");
}
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ impl Oxc {
..CodegenOptions::default()
})
.build(&program)
.source_text;
.code;

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion napi/minify/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ pub fn minify(filename: String, source_text: String) -> String {
.with_mangler(mangler)
.with_capacity(source_text.len())
.build(&program)
.source_text
.code
}
4 changes: 2 additions & 2 deletions napi/transform/src/isolated_declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ pub fn isolated_declaration(
let errors = wrap_diagnostics(source_path, source_type, &source_text, errors);

IsolatedDeclarationsResult {
code: codegen_ret.source_text,
map: codegen_ret.source_map.map(SourceMap::from),
code: codegen_ret.code,
map: codegen_ret.map.map(SourceMap::from),
errors,
}
}
8 changes: 4 additions & 4 deletions napi/transform/src/transformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,13 @@ impl CompilerInterface for Compiler {
}

fn after_codegen(&mut self, ret: CodegenReturn) {
self.printed = ret.source_text;
self.printed_sourcemap = ret.source_map.map(SourceMap::from);
self.printed = ret.code;
self.printed_sourcemap = ret.map.map(SourceMap::from);
}

fn after_isolated_declarations(&mut self, ret: CodegenReturn) {
self.declaration.replace(ret.source_text);
self.declaration_map = ret.source_map.map(SourceMap::from);
self.declaration.replace(ret.code);
self.declaration_map = ret.map.map(SourceMap::from);
}
}

Expand Down
2 changes: 1 addition & 1 deletion tasks/benchmark/benches/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn bench_codegen(criterion: &mut Criterion) {

let mut group = criterion.benchmark_group("codegen");
group.bench_with_input(id.clone(), &ret.program, |b, program| {
b.iter_with_large_drop(|| CodeGenerator::new().build(program).source_map);
b.iter_with_large_drop(|| CodeGenerator::new().build(program).map);
});
group.finish();

Expand Down
6 changes: 3 additions & 3 deletions tasks/benchmark/benches/sourcemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ fn bench_sourcemap(criterion: &mut Criterion) {
let allocator = Allocator::default();
let ret = Parser::new(&allocator, source_text, source_type).parse();

let CodegenReturn { source_text: output_txt, .. } = CodeGenerator::new()
let CodegenReturn { code: output_txt, .. } = CodeGenerator::new()
.enable_source_map(file.file_name.as_str(), source_text)
.build(&ret.program);
let lines = output_txt.matches('\n').count() as u32;

b.iter(|| {
let CodegenReturn { source_map, .. } = CodeGenerator::new()
let CodegenReturn { map, .. } = CodeGenerator::new()
.enable_source_map(file.file_name.as_str(), source_text)
.build(&ret.program);
if let Some(sourcemap) = source_map {
if let Some(sourcemap) = map {
let concat_sourcemap_builder = ConcatSourceMapBuilder::from_sourcemaps(&[
(&sourcemap, 0),
(&sourcemap, lines),
Expand Down
2 changes: 1 addition & 1 deletion tasks/coverage/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl CompilerInterface for Driver {
}

fn after_codegen(&mut self, ret: CodegenReturn) {
self.printed = ret.source_text;
self.printed = ret.code;
}
}

Expand Down
2 changes: 1 addition & 1 deletion tasks/coverage/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ impl Case for CodegenRuntimeTest262Case {
let source_type = SourceType::default().with_module(is_module);
let allocator = Allocator::default();
let ret = Parser::new(&allocator, source_text, source_type).parse();
let mut text = CodeGenerator::new().build(&ret.program).source_text;
let mut text = CodeGenerator::new().build(&ret.program).code;
if is_only_strict {
text = format!("\"use strict\";\n{text}");
}
Expand Down
2 changes: 1 addition & 1 deletion tasks/coverage/src/typescript/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ impl Baseline {
let allocator = Allocator::default();
let source_type = SourceType::from_path(Path::new(&self.name)).unwrap();
let ret = Parser::new(&allocator, &self.original, source_type).parse();
let printed = CodeGenerator::new().build(&ret.program).source_text;
let printed = CodeGenerator::new().build(&ret.program).code;
self.oxc_printed = printed;
}

Expand Down
2 changes: 1 addition & 1 deletion tasks/coverage/src/typescript/transpile_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,6 @@ fn transpile(path: &Path, source_text: &str) -> (String, Vec<OxcDiagnostic>) {
IsolatedDeclarationsOptions { strip_internal: true },
)
.build(&ret.program);
let printed = CodeGenerator::new().build(&ret.program).source_text;
let printed = CodeGenerator::new().build(&ret.program).code;
(printed, ret.errors)
}
2 changes: 1 addition & 1 deletion tasks/minsize/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ fn minify(source_text: &str, source_type: SourceType, options: MinifierOptions)
.with_options(CodegenOptions { minify: true, ..CodegenOptions::default() })
.with_mangler(ret.mangler)
.build(program)
.source_text
.code
}

fn gzip_size(s: &str) -> usize {
Expand Down
2 changes: 1 addition & 1 deletion tasks/transform_conformance/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl CompilerInterface for Driver {
}

fn after_codegen(&mut self, ret: CodegenReturn) {
self.printed = ret.source_text;
self.printed = ret.code;
}

fn after_transform(
Expand Down
4 changes: 2 additions & 2 deletions tasks/transform_conformance/src/test_case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ impl TestCase for ConformanceTestCase {
// CommentOptions { preserve_annotate_comments: true },
// )
.build(&ret.program)
.source_text
.code
},
);

Expand Down Expand Up @@ -396,7 +396,7 @@ impl ExecTestCase {
let source_text = fs::read_to_string(&target_path).unwrap();
let source_type = SourceType::from_path(&target_path).unwrap();
let transformed_ret = Parser::new(&allocator, &source_text, source_type).parse();
let result = CodeGenerator::new().build(&transformed_ret.program).source_text;
let result = CodeGenerator::new().build(&transformed_ret.program).code;
fs::write(&target_path, result).unwrap();
target_path
}
Expand Down