diff --git a/.changeset/tricky-llamas-collect.md b/.changeset/tricky-llamas-collect.md new file mode 100644 index 000000000000..0b8d3debe449 --- /dev/null +++ b/.changeset/tricky-llamas-collect.md @@ -0,0 +1,6 @@ +--- +swc_core: patch +swc_ecma_transforms_testing: patch +--- + +fix(es/testing): Fix sourcemap generation diff --git a/Cargo.lock b/Cargo.lock index 3245ca079ce9..7a7cb03f4287 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5610,6 +5610,7 @@ dependencies = [ "serde_json", "sha2", "sourcemap", + "swc_allocator", "swc_common", "swc_ecma_ast", "swc_ecma_codegen", diff --git a/crates/swc_ecma_transforms_testing/Cargo.toml b/crates/swc_ecma_transforms_testing/Cargo.toml index b9129c78f3ef..dc9414ade97b 100644 --- a/crates/swc_ecma_transforms_testing/Cargo.toml +++ b/crates/swc_ecma_transforms_testing/Cargo.toml @@ -22,6 +22,7 @@ sha2 = { workspace = true } sourcemap = { workspace = true } tempfile = { workspace = true } +swc_allocator = { version = "2.0.0", path = "../swc_allocator", default-features = false } swc_common = { version = "5.0.0", path = "../swc_common", features = [ "sourcemap", ] } diff --git a/crates/swc_ecma_transforms_testing/src/lib.rs b/crates/swc_ecma_transforms_testing/src/lib.rs index 38bb0231e1a1..834096383e7a 100644 --- a/crates/swc_ecma_transforms_testing/src/lib.rs +++ b/crates/swc_ecma_transforms_testing/src/lib.rs @@ -27,7 +27,7 @@ use swc_common::{ FileName, Mark, SourceMap, DUMMY_SP, }; use swc_ecma_ast::*; -use swc_ecma_codegen::to_code_default; +use swc_ecma_codegen::{to_code_default, Emitter}; use swc_ecma_parser::{lexer::Lexer, Parser, StringInput, Syntax}; use swc_ecma_testing::{exec_node_js, JsExecOptions}; use swc_ecma_transforms_base::{ @@ -900,7 +900,7 @@ fn test_fixture_inner<'a>( }); let mut src_map = if config.sourcemap { - Some(Vec::new()) + Some(swc_allocator::maybe::vec::Vec::new()) } else { None }; @@ -942,6 +942,24 @@ fn test_fixture_inner<'a>( let module = &actual; let comments: &Rc = &tester.comments.clone(); + let mut buf = vec![]; + { + let mut emitter = Emitter { + cfg: Default::default(), + cm: tester.cm.clone(), + wr: Box::new(swc_ecma_codegen::text_writer::JsWriter::new( + tester.cm.clone(), + "\n", + &mut buf, + src_map.as_mut(), + )), + comments: Some(comments), + }; + + // println!("Emitting: {:?}", module); + emitter.emit_program(module).unwrap(); + } + if let Some(src_map) = &mut src_map { sourcemap = Some(tester.cm.build_source_map_with_config( src_map, @@ -950,7 +968,7 @@ fn test_fixture_inner<'a>( )); } - to_code_default(tester.cm.clone(), Some(comments), module) + String::from_utf8(buf).expect("codegen generated non-utf8 output") }; Ok(actual_src)