Skip to content

Commit

Permalink
refactor(sourcemap): change sourcemap name to take a reference (#2779)
Browse files Browse the repository at this point in the history
  • Loading branch information
underfin committed Mar 23, 2024
1 parent 712b3d2 commit d9b77d8
Show file tree
Hide file tree
Showing 18 changed files with 60 additions and 50 deletions.
4 changes: 2 additions & 2 deletions crates/oxc_codegen/examples/codegen.rs
Expand Up @@ -30,12 +30,12 @@ fn main() -> std::io::Result<()> {

let options = CodegenOptions::default();
let printed =
Codegen::<false>::new(&source_text, options.clone()).build(&ret.program).source_text;
Codegen::<false>::new("", &source_text, options.clone()).build(&ret.program).source_text;
println!("Printed:");
println!("{printed}");

let ret = Parser::new(&allocator, &printed, source_type).parse();
let minified = Codegen::<true>::new(&source_text, options).build(&ret.program).source_text;
let minified = Codegen::<true>::new("", &source_text, options).build(&ret.program).source_text;
println!("Minified:");
println!("{minified}");

Expand Down
8 changes: 3 additions & 5 deletions crates/oxc_codegen/examples/sourcemap.rs
Expand Up @@ -26,13 +26,11 @@ fn main() -> std::io::Result<()> {
return Ok(());
}

let codegen_options = CodegenOptions {
enable_source_map: Some(path.to_string_lossy().to_string()),
enable_typescript: true,
};
let codegen_options = CodegenOptions { enable_source_map: true, enable_typescript: true };

let CodegenReturn { source_text, source_map } =
Codegen::<false>::new(&source_text, codegen_options).build(&ret.program);
Codegen::<false>::new(path.to_string_lossy().as_ref(), &source_text, codegen_options)
.build(&ret.program);

if let Some(source_map) = source_map {
let mut buff = vec![];
Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_codegen/src/lib.rs
Expand Up @@ -38,7 +38,7 @@ pub use crate::{
#[derive(Debug, Default, Clone)]
pub struct CodegenOptions {
/// Pass in the filename to enable source map support.
pub enable_source_map: Option<String>,
pub enable_source_map: bool,

/// Enable TypeScript code generation.
pub enable_typescript: bool,
Expand Down Expand Up @@ -85,14 +85,14 @@ pub enum Separator {
}

impl<const MINIFY: bool> Codegen<MINIFY> {
pub fn new(source_text: &str, options: CodegenOptions) -> Self {
pub fn new(source_name: &str, source_text: &str, options: CodegenOptions) -> Self {
// Initialize the output code buffer to reduce memory reallocation.
// Minification will reduce by at least half of the original size.
let source_len = source_text.len();
let capacity = if MINIFY { source_len / 2 } else { source_len };

let mut sourcemap_builder = SourcemapBuilder::default();
if let Some(source_name) = &options.enable_source_map {
if options.enable_source_map {
sourcemap_builder.with_name_and_source(source_name, source_text);
}
Self {
Expand Down
7 changes: 4 additions & 3 deletions crates/oxc_codegen/tests/mod.rs
Expand Up @@ -8,8 +8,9 @@ fn test(source_text: &str, expected: &str) {
let source_type = SourceType::default().with_module(true);
let program = Parser::new(&allocator, source_text, source_type).parse().program;
let program = allocator.alloc(program);
let result =
Codegen::<false>::new(source_text, CodegenOptions::default()).build(program).source_text;
let result = Codegen::<false>::new("", source_text, CodegenOptions::default())
.build(program)
.source_text;
assert_eq!(expected, result, "for source {source_text}, expect {expected}, got {result}");
}

Expand All @@ -22,7 +23,7 @@ fn test_ts(source_text: &str, expected: &str, is_typescript_definition: bool) {
let program = Parser::new(&allocator, source_text, source_type).parse().program;
let program = allocator.alloc(program);
let codegen_options = CodegenOptions { enable_typescript: true, ..CodegenOptions::default() };
let result = Codegen::<false>::new(source_text, codegen_options).build(program).source_text;
let result = Codegen::<false>::new("", source_text, codegen_options).build(program).source_text;
assert_eq!(expected, result, "for source {source_text}, expect {expected}, got {result}");
}

Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/context.rs
Expand Up @@ -151,7 +151,7 @@ impl<'a> LintContext<'a> {

#[allow(clippy::unused_self)]
pub fn codegen(&self) -> Codegen<false> {
Codegen::<false>::new("", CodegenOptions::default())
Codegen::<false>::new("", "", CodegenOptions::default())
}

/* JSDoc */
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_minifier/examples/minifier.rs
Expand Up @@ -43,9 +43,9 @@ fn minify(source_text: &str, source_type: SourceType, mangle: bool, whitespace:
let options = MinifierOptions { mangle, ..MinifierOptions::default() };
Minifier::new(options).build(&allocator, program);
if whitespace {
Codegen::<true>::new(source_text, CodegenOptions::default()).build(program)
Codegen::<true>::new("", source_text, CodegenOptions::default()).build(program)
} else {
Codegen::<false>::new(source_text, CodegenOptions::default()).build(program)
Codegen::<false>::new("", source_text, CodegenOptions::default()).build(program)
}
.source_text
}
2 changes: 1 addition & 1 deletion crates/oxc_minifier/tests/mod.rs
Expand Up @@ -19,7 +19,7 @@ pub(crate) fn minify(
let program = Parser::new(&allocator, source_text, source_type).parse().program;
let program = allocator.alloc(program);
Minifier::new(options).build(&allocator, program);
Codegen::<true>::new(source_text, CodegenOptions::default()).build(program).source_text
Codegen::<true>::new("", source_text, CodegenOptions::default()).build(program).source_text
}

pub(crate) fn test(source_text: &str, expected: &str) {
Expand Down
5 changes: 3 additions & 2 deletions crates/oxc_transformer/examples/transformer.rs
Expand Up @@ -55,8 +55,9 @@ fn main() {
};
Transformer::new(&allocator, source_type, semantic, transform_options).build(program).unwrap();

let printed =
Codegen::<false>::new(&source_text, CodegenOptions::default()).build(program).source_text;
let printed = Codegen::<false>::new("", &source_text, CodegenOptions::default())
.build(program)
.source_text;
println!("Transformed:\n");
println!("{printed}");
}
8 changes: 6 additions & 2 deletions crates/oxc_transformer/src/tester.rs
Expand Up @@ -43,11 +43,15 @@ impl Tester {
Transformer::new(&self.allocator, self.source_type, semantic, self.options.clone())
.build(program)?;

Ok(Codegen::<false>::new(source_text, CodegenOptions::default()).build(program).source_text)
Ok(Codegen::<false>::new("", source_text, CodegenOptions::default())
.build(program)
.source_text)
}

fn codegen(&self, source_text: &str) -> String {
let program = Parser::new(&self.allocator, source_text, self.source_type).parse().program;
Codegen::<false>::new(source_text, CodegenOptions::default()).build(&program).source_text
Codegen::<false>::new("", source_text, CodegenOptions::default())
.build(&program)
.source_text
}
}
4 changes: 2 additions & 2 deletions crates/oxc_wasm/src/lib.rs
Expand Up @@ -272,9 +272,9 @@ impl Oxc {
..CodegenOptions::default()
};
self.codegen_text = if minifier_options.whitespace() {
Codegen::<true>::new(source_text, codegen_options).build(program).source_text
Codegen::<true>::new("", source_text, codegen_options).build(program).source_text
} else {
Codegen::<false>::new(source_text, codegen_options).build(program).source_text
Codegen::<false>::new("", source_text, codegen_options).build(program).source_text
};

Ok(())
Expand Down
8 changes: 3 additions & 5 deletions tasks/benchmark/benches/codegen_sourcemap.rs
Expand Up @@ -14,12 +14,10 @@ fn bench_codegen_sourcemap(criterion: &mut Criterion) {
group.bench_with_input(id, &file.source_text, |b, source_text| {
let allocator = Allocator::default();
let program = Parser::new(&allocator, source_text, source_type).parse().program;
let codegen_options = CodegenOptions {
enable_source_map: Some(file.file_name.clone()),
..CodegenOptions::default()
};
let codegen_options =
CodegenOptions { enable_source_map: true, ..CodegenOptions::default() };
b.iter_with_large_drop(|| {
Codegen::<false>::new(source_text, codegen_options.clone())
Codegen::<false>::new(file.file_name.as_str(), source_text, codegen_options.clone())
.build(&program)
.source_map
});
Expand Down
14 changes: 7 additions & 7 deletions tasks/coverage/src/codegen.rs
Expand Up @@ -72,12 +72,12 @@ fn get_normal_result(
let options = CodegenOptions::default();
let allocator = Allocator::default();
let parse_result1 = Parser::new(&allocator, source_text, source_type).parse();
let source_text1 = Codegen::<false>::new(source_text, options.clone())
let source_text1 = Codegen::<false>::new("", source_text, options.clone())
.build(&parse_result1.program)
.source_text;
let parse_result2 = Parser::new(&allocator, &source_text1, source_type).parse();
let source_text2 =
Codegen::<false>::new(&source_text1, options).build(&parse_result2.program).source_text;
Codegen::<false>::new("", &source_text1, options).build(&parse_result2.program).source_text;
let result = source_text1 == source_text2;

if !result {
Expand Down Expand Up @@ -114,12 +114,12 @@ fn get_minify_result(
let options = CodegenOptions::default();
let allocator = Allocator::default();
let parse_result1 = Parser::new(&allocator, source_text, source_type).parse();
let source_text1 = Codegen::<true>::new(source_text, options.clone())
let source_text1 = Codegen::<true>::new("", source_text, options.clone())
.build(&parse_result1.program)
.source_text;
let parse_result2 = Parser::new(&allocator, source_text1.as_str(), source_type).parse();
let source_text2 =
Codegen::<true>::new(&source_text1, options).build(&parse_result2.program).source_text;
Codegen::<true>::new("", &source_text1, options).build(&parse_result2.program).source_text;
let result = source_text1 == source_text2;

if !result {
Expand Down Expand Up @@ -153,15 +153,15 @@ fn get_typescript_result(
source_text: &str,
source_type: SourceType,
) -> bool {
let options = CodegenOptions { enable_source_map: None, enable_typescript: true };
let options = CodegenOptions { enable_source_map: false, enable_typescript: true };
let allocator = Allocator::default();
let parse_result1 = Parser::new(&allocator, source_text, source_type).parse();
let source_text1 = Codegen::<false>::new(source_text, options.clone())
let source_text1 = Codegen::<false>::new("", source_text, options.clone())
.build(&parse_result1.program)
.source_text;
let parse_result2 = Parser::new(&allocator, &source_text1, source_type).parse();
let source_text2 =
Codegen::<false>::new(&source_text1, options).build(&parse_result2.program).source_text;
Codegen::<false>::new("", &source_text1, options).build(&parse_result2.program).source_text;
let result = source_text1 == source_text2;

if !result {
Expand Down
2 changes: 1 addition & 1 deletion tasks/coverage/src/minifier.rs
Expand Up @@ -100,5 +100,5 @@ fn minify(source_text: &str, source_type: SourceType, options: MinifierOptions)
let program = Parser::new(&allocator, source_text, source_type).parse().program;
let program = allocator.alloc(program);
Minifier::new(options).build(&allocator, program);
Codegen::<true>::new(source_text, CodegenOptions::default()).build(program).source_text
Codegen::<true>::new("", source_text, CodegenOptions::default()).build(program).source_text
}
2 changes: 1 addition & 1 deletion tasks/coverage/src/runtime/mod.rs
Expand Up @@ -138,7 +138,7 @@ impl Case for CodegenRuntimeTest262Case {
let source_type = SourceType::default().with_module(is_module);
let allocator = Allocator::default();
let program = Parser::new(&allocator, source_text, source_type).parse().program;
let mut text = Codegen::<false>::new(source_text, CodegenOptions::default())
let mut text = Codegen::<false>::new("", source_text, CodegenOptions::default())
.build(&program)
.source_text;
if is_only_strict {
Expand Down
13 changes: 8 additions & 5 deletions tasks/coverage/src/sourcemap.rs
Expand Up @@ -126,11 +126,14 @@ impl Case for SourcemapCase {
}
}

let codegen_options = CodegenOptions {
enable_source_map: Some(self.path.to_string_lossy().to_string()),
..CodegenOptions::default()
};
let codegen_ret = Codegen::<false>::new(source_text, codegen_options).build(&ret.program);
let codegen_options =
CodegenOptions { enable_source_map: true, ..CodegenOptions::default() };
let codegen_ret = Codegen::<false>::new(
self.path.to_string_lossy().as_ref(),
source_text,
codegen_options,
)
.build(&ret.program);

TestResult::Snapshot(
SourcemapVisualizer::new(&codegen_ret.source_text, &codegen_ret.source_map.unwrap())
Expand Down
2 changes: 1 addition & 1 deletion tasks/minsize/src/lib.rs
Expand Up @@ -73,7 +73,7 @@ fn minify(source_text: &str, source_type: SourceType, options: MinifierOptions)
let program = Parser::new(&allocator, source_text, source_type).parse().program;
let program = allocator.alloc(program);
Minifier::new(options).build(&allocator, program);
Codegen::<true>::new(source_text, CodegenOptions::default()).build(program).source_text
Codegen::<true>::new("", source_text, CodegenOptions::default()).build(program).source_text
}

fn gzip_size(s: &str) -> usize {
Expand Down
17 changes: 11 additions & 6 deletions tasks/transform_conformance/src/test_case.rs
Expand Up @@ -179,7 +179,7 @@ pub trait TestCase {
.build(transformed_program);

result.map(|()| {
Codegen::<false>::new(&source_text, CodegenOptions::default())
Codegen::<false>::new("", &source_text, CodegenOptions::default())
.build(transformed_program)
.source_text
})
Expand Down Expand Up @@ -253,8 +253,9 @@ impl TestCase for ConformanceTestCase {
let mut actual_errors = String::new();
let result = transformer.build(program);
if result.is_ok() {
transformed_code =
Codegen::<false>::new(&input, codegen_options.clone()).build(program).source_text;
transformed_code = Codegen::<false>::new("", &input, codegen_options.clone())
.build(program)
.source_text;
} else {
actual_errors = result.err().unwrap().iter().map(ToString::to_string).collect();
}
Expand All @@ -269,12 +270,16 @@ impl TestCase for ConformanceTestCase {
}
// The transformation should be equal to input.js If output.js does not exist.
let program = Parser::new(&allocator, &input, source_type).parse().program;
Codegen::<false>::new(&input, codegen_options.clone()).build(&program).source_text
Codegen::<false>::new("", &input, codegen_options.clone())
.build(&program)
.source_text
},
|output| {
// Get expected code by parsing the source text, so we can get the same code generated result.
let program = Parser::new(&allocator, &output, source_type).parse().program;
Codegen::<false>::new(&output, codegen_options.clone()).build(&program).source_text
Codegen::<false>::new("", &output, codegen_options.clone())
.build(&program)
.source_text
},
);

Expand Down Expand Up @@ -334,7 +339,7 @@ impl ExecTestCase {
let source_type = SourceType::from_path(&target_path).unwrap();
let transformed_program =
Parser::new(&allocator, &source_text, source_type).parse().program;
let result = Codegen::<false>::new(&source_text, CodegenOptions::default())
let result = Codegen::<false>::new("", &source_text, CodegenOptions::default())
.build(&transformed_program)
.source_text;

Expand Down
2 changes: 1 addition & 1 deletion tasks/transform_conformance/src/ts_fixtures.rs
Expand Up @@ -127,7 +127,7 @@ impl TypeScriptFixtures {

result
.map(|()| {
Codegen::<false>::new(&source_text, CodegenOptions::default())
Codegen::<false>::new("", &source_text, CodegenOptions::default())
.build(transformed_program)
.source_text
})
Expand Down

0 comments on commit d9b77d8

Please sign in to comment.