mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 12:19:15 +00:00
refactor(sourcemap): change sourcemap name to take a reference (#2779)
This commit is contained in:
parent
712b3d2a11
commit
d9b77d853b
18 changed files with 60 additions and 50 deletions
|
|
@ -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}");
|
||||
|
||||
|
|
|
|||
|
|
@ -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![];
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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}");
|
||||
}
|
||||
|
||||
|
|
@ -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}");
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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 */
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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}");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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(())
|
||||
|
|
|
|||
|
|
@ -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
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
@ -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 {
|
||||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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())
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
})
|
||||
|
|
@ -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();
|
||||
}
|
||||
|
|
@ -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
|
||||
},
|
||||
);
|
||||
|
||||
|
|
@ -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;
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
})
|
||||
|
|
|
|||
Loading…
Reference in a new issue