Skip to content

Commit

Permalink
fix(es/codegen): Fix wrong sourcemap when there are new lines in tpl (#…
Browse files Browse the repository at this point in the history
…9578)

**Description:**

1. split quasi by '\n' 
2. add the mapping between the original string and the generated string one by one.

<img width="873" alt="image"
src="https://github.com/user-attachments/assets/8a03da7f-2d22-4dd5-9bac-220418b10911">

**Related issue:**

 - Closes #9567
  • Loading branch information
CPunisher authored Sep 23, 2024
1 parent 185d6f5 commit cf74382
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .changeset/gentle-cycles-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
swc_ecma_codegen: patch
swc_core: patch
---

fix(es/codegen): fix wrong sourcemap when there are new lines in tpl
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.

22 changes: 22 additions & 0 deletions crates/swc/tests/fixture/sourcemap/issue-9567/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"jsc": {
"parser": {
"syntax": "typescript",
"tsx": true
},
"target": "es2015",
"externalHelpers": true,
"loose": false,
"minify": {
"compress": true,
"mangle": {
"toplevel": true
}
}
},
"module": {
"type": "es6"
},
"minify": true,
"sourceMaps": true
}
12 changes: 12 additions & 0 deletions crates/swc/tests/fixture/sourcemap/issue-9567/input/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function foo(span, error) {
span.setStatus({
message: `${error.message} ${
error.code
? `\nMon\tgo\nos
e Error\n C
od\ne: ${error.code}`
: "1\n23"
}`,
});
}
foo();
7 changes: 7 additions & 0 deletions crates/swc/tests/fixture/sourcemap/issue-9567/output/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function e(e,o){e.setStatus({message:`${o.message} ${o.code?`
Mon go
os
e Error
C
od
e: ${o.code}`:"1\n23"}`})}e();
18 changes: 18 additions & 0 deletions crates/swc/tests/fixture/sourcemap/issue-9567/output/index.map
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"mappings": "AAAA,SAASA,EAAIC,CAAI,CAAEC,CAAK,EACtBD,EAAKE,SAAS,CAAC,CACXC,QAAS,CAAC,EAAEF,EAAME,OAAO,CAAC,CAAC,EACvBF,EAAMG,IAAI,CACJ,CAAC;AAAE;AAAS;AAC5B;AAAS;AACT;AAAI,GAAG,EAAEH,EAAMG,IAAI,CAAC,CAAC,CACL,QACT,CAAC,AACN,EACF,CACAL",
"names": [
"foo",
"span",
"error",
"setStatus",
"message",
"code"
],
"sources": [
"../../input/index.js"
],
"sourcesContent": [
"function foo(span, error) {\n span.setStatus({\n message: `${error.message} ${\n error.code\n ? `\\nMon\\tgo\\nos\ne Error\\n C\nod\\ne: ${error.code}`\n : \"1\\n23\"\n }`,\n });\n}\nfoo();\n"
],
"version": 3
}
1 change: 1 addition & 0 deletions crates/swc_ecma_codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ bench = false
memchr = { workspace = true }
num-bigint = { workspace = true, features = ["serde"] }
once_cell = { workspace = true }
regex = { workspace = true }
serde = { workspace = true }
sourcemap = { workspace = true }
tracing = { workspace = true }
Expand Down
28 changes: 22 additions & 6 deletions crates/swc_ecma_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ fn replace_close_inline_script(raw: &str) -> Cow<str> {
Cow::Owned(result)
}

static NEW_LINE_TPL_REGEX: Lazy<regex::Regex> = Lazy::new(|| regex::Regex::new(r"\\n|\n").unwrap());

impl<'a, W, S: SourceMapper> Emitter<'a, W, S>
where
W: WriteJs,
Expand Down Expand Up @@ -1979,17 +1981,31 @@ where

#[emitter]
fn emit_quasi(&mut self, node: &TplElement) -> Result {
srcmap!(node, true);

let raw = node.raw.replace("\r\n", "\n").replace('\r', "\n");
if self.cfg.minify || (self.cfg.ascii_only && !node.raw.is_ascii()) {
let v = get_template_element_from_raw(&raw, self.cfg.ascii_only);
self.wr.write_str_lit(DUMMY_SP, &v)?;
let span = node.span();

let mut last_offset_gen = 0;
let mut last_offset_origin = 0;
for ((offset_gen, _), mat) in v
.match_indices('\n')
.zip(NEW_LINE_TPL_REGEX.find_iter(&raw))
{
self.wr
.add_srcmap(span.lo + BytePos(last_offset_origin as u32))?;
self.wr
.write_str_lit(DUMMY_SP, &v[last_offset_gen..=offset_gen])?;
last_offset_gen = offset_gen + 1;
last_offset_origin = mat.end();
}
self.wr
.add_srcmap(span.lo + BytePos(last_offset_origin as u32))?;
self.wr.write_str_lit(DUMMY_SP, &v[last_offset_gen..])?;
self.wr.add_srcmap(span.hi)?;
} else {
self.wr.write_str_lit(DUMMY_SP, &raw)?;
self.wr.write_str_lit(node.span(), &raw)?;
}

srcmap!(node, false);
}

#[emitter]
Expand Down

0 comments on commit cf74382

Please sign in to comment.