Skip to content

Commit

Permalink
fix(es/testing): Fix sourcemap generation (#9891)
Browse files Browse the repository at this point in the history
**Description:**

This is a regression from #9150
  • Loading branch information
kdy1 authored Jan 16, 2025
1 parent 96c6c07 commit 008f2de
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .changeset/tricky-llamas-collect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
swc_core: patch
swc_ecma_transforms_testing: patch
---

fix(es/testing): Fix sourcemap generation
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/swc_ecma_transforms_testing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
] }
Expand Down
24 changes: 21 additions & 3 deletions crates/swc_ecma_transforms_testing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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
};
Expand Down Expand Up @@ -942,6 +942,24 @@ fn test_fixture_inner<'a>(
let module = &actual;
let comments: &Rc<SingleThreadedComments> = &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,
Expand All @@ -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)
Expand Down

0 comments on commit 008f2de

Please sign in to comment.