Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(es/testing): Fix sourcemap generation #9891

Merged
merged 5 commits into from
Jan 16, 2025
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
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
Loading