Skip to content

Commit cf74382

Browse files
authoredSep 23, 2024··
fix(es/codegen): Fix wrong sourcemap when there are new lines in tpl (#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
1 parent 185d6f5 commit cf74382

File tree

8 files changed

+89
-6
lines changed

8 files changed

+89
-6
lines changed
 

‎.changeset/gentle-cycles-repair.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
swc_ecma_codegen: patch
3+
swc_core: patch
4+
---
5+
6+
fix(es/codegen): fix wrong sourcemap when there are new lines in tpl

‎Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"jsc": {
3+
"parser": {
4+
"syntax": "typescript",
5+
"tsx": true
6+
},
7+
"target": "es2015",
8+
"externalHelpers": true,
9+
"loose": false,
10+
"minify": {
11+
"compress": true,
12+
"mangle": {
13+
"toplevel": true
14+
}
15+
}
16+
},
17+
"module": {
18+
"type": "es6"
19+
},
20+
"minify": true,
21+
"sourceMaps": true
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function foo(span, error) {
2+
span.setStatus({
3+
message: `${error.message} ${
4+
error.code
5+
? `\nMon\tgo\nos
6+
e Error\n C
7+
od\ne: ${error.code}`
8+
: "1\n23"
9+
}`,
10+
});
11+
}
12+
foo();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function e(e,o){e.setStatus({message:`${o.message} ${o.code?`
2+
Mon go
3+
os
4+
e Error
5+
C
6+
od
7+
e: ${o.code}`:"1\n23"}`})}e();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"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",
3+
"names": [
4+
"foo",
5+
"span",
6+
"error",
7+
"setStatus",
8+
"message",
9+
"code"
10+
],
11+
"sources": [
12+
"../../input/index.js"
13+
],
14+
"sourcesContent": [
15+
"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"
16+
],
17+
"version": 3
18+
}

‎crates/swc_ecma_codegen/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ bench = false
2020
memchr = { workspace = true }
2121
num-bigint = { workspace = true, features = ["serde"] }
2222
once_cell = { workspace = true }
23+
regex = { workspace = true }
2324
serde = { workspace = true }
2425
sourcemap = { workspace = true }
2526
tracing = { workspace = true }

‎crates/swc_ecma_codegen/src/lib.rs

+22-6
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ fn replace_close_inline_script(raw: &str) -> Cow<str> {
139139
Cow::Owned(result)
140140
}
141141

142+
static NEW_LINE_TPL_REGEX: Lazy<regex::Regex> = Lazy::new(|| regex::Regex::new(r"\\n|\n").unwrap());
143+
142144
impl<'a, W, S: SourceMapper> Emitter<'a, W, S>
143145
where
144146
W: WriteJs,
@@ -1979,17 +1981,31 @@ where
19791981

19801982
#[emitter]
19811983
fn emit_quasi(&mut self, node: &TplElement) -> Result {
1982-
srcmap!(node, true);
1983-
19841984
let raw = node.raw.replace("\r\n", "\n").replace('\r', "\n");
19851985
if self.cfg.minify || (self.cfg.ascii_only && !node.raw.is_ascii()) {
19861986
let v = get_template_element_from_raw(&raw, self.cfg.ascii_only);
1987-
self.wr.write_str_lit(DUMMY_SP, &v)?;
1987+
let span = node.span();
1988+
1989+
let mut last_offset_gen = 0;
1990+
let mut last_offset_origin = 0;
1991+
for ((offset_gen, _), mat) in v
1992+
.match_indices('\n')
1993+
.zip(NEW_LINE_TPL_REGEX.find_iter(&raw))
1994+
{
1995+
self.wr
1996+
.add_srcmap(span.lo + BytePos(last_offset_origin as u32))?;
1997+
self.wr
1998+
.write_str_lit(DUMMY_SP, &v[last_offset_gen..=offset_gen])?;
1999+
last_offset_gen = offset_gen + 1;
2000+
last_offset_origin = mat.end();
2001+
}
2002+
self.wr
2003+
.add_srcmap(span.lo + BytePos(last_offset_origin as u32))?;
2004+
self.wr.write_str_lit(DUMMY_SP, &v[last_offset_gen..])?;
2005+
self.wr.add_srcmap(span.hi)?;
19882006
} else {
1989-
self.wr.write_str_lit(DUMMY_SP, &raw)?;
2007+
self.wr.write_str_lit(node.span(), &raw)?;
19902008
}
1991-
1992-
srcmap!(node, false);
19932009
}
19942010

19952011
#[emitter]

0 commit comments

Comments
 (0)
Please sign in to comment.