Skip to content

Commit

Permalink
fix(common): Use SourceMap::adjust_mappings in correct order (#9447)
Browse files Browse the repository at this point in the history
**Related issue:**

 - Closes #9392
  • Loading branch information
kdy1 committed Aug 19, 2024
1 parent 55404ba commit 05961eb
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 50 deletions.
6 changes: 6 additions & 0 deletions .changeset/hip-buckets-share.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
swc_common: patch
swc_core: patch
---

fix(common): Use `SourceMap::adjust_mappings` in correct order
3 changes: 1 addition & 2 deletions crates/swc/tests/fixture/sourcemap/004/output/index.map
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"mappings": "AAACA,CAAAA,KAAKC,gBAAmB,GAAGD,KAAKC,gBAAmB,IAAI,EAAE,AAAF,EAAIC,IAAI,CAAC;IAC7D;QACI;KACH;IACD;QACU,MAAY,SAAUC,CAAuB,EAAEC,CAAmB,EAAEC,CAAmB;YACzF;YACA,IAAIC,IAAa,SAAoBC,CAAI;gBACrC,IAAIC,IAAOD,EAAKC,IAAI;gBACpB,OAAqB,AAAC,CAAA,GAAGC,EAA+CC,GAAG,AAAHA,EAAK,OAAO;oBAChFC,UAAUH,EAAKI,GAAG;gBACtB;YACJ;YACAP,EAAoBQ,CAAC,CAACT,IACDC,EAAoBS,CAAC,CAACV,GAAqB;gBACvCW,SAAS;oBAC1B,OAAqBA;gBACzB;gBACqBC,SAAS;oBAC1B,OAAqBV;gBACzB;YACJ;YACqB,IAAIG,IAAiDJ,EAAoB,OAC1FU,IAAU,CAAA;QAE1B;QACc,MAAY,SAAUZ,CAAuB,EAAEc,CAAwB,EAAEZ,CAAmB;YAC7Fa,CAAAA,OAAOC,QAAQ,GAAGD,OAAOC,QAAQ,IAAI,EAAE,AAAF,EAAIjB,IAAI,CAAC;gBAC3C;gBACA;oBACI,OAAOG,EAAoB;gBAC/B;aACH;QAGb;IACI;IACS,SAAUA,CAAmB;QAKzBA,EAAoBe,CAAC,CAAC,GAAG;YAC9B;YACA;YACA;SACH,EAAE;YACC,OAPOf,EAAoBA,EAAoBgB,CAAC,GAOxB;QAC5B,IAESC,OAD0BjB,EAAoBe,CAAC;IAGhE;CACC",
"mappings": "AAACA,MAAK,gBAAmB,GAAGA,KAAK,gBAAmB,IAAI,EAAA,AAAC,EAAGC,IAAI,CAAC;IAC7D;QACI;KACH;IACD;QACU,MAAY,SAAUC,CAAuB,EAAEC,CAAmB,EAAEC,CAAmB,EAAE;YAC3F;YACA,IAAIC,IAAa,SAAoBC,CAAI,EAAE;gBACvC,IAAIC,IAAOD,EAAKC,IAAI;gBACpB,OAAsB,CAAA,GAAGC,EAA+CC,GAAAA,AAAE,EAAG,OAAO;oBAChFC,UAAUH,EAAKI,GAAG;gBACtB;YACJ;YACAP,EAAoBQ,CAAC,CAACT,IACDC,EAAoBS,CAAC,CAACV,GAAqB;gBACvCW,SAAS,WAAoB;oBAC9C,OAAqBA;gBACzB;gBACqBC,SAAS,WAAoB;oBAC9C,OAAqBV;gBACzB;YACJ;YACqB,IAAIG,IAAiDJ,EAAoB,OAC1FU,IAAU,CAAA;QAE1B;QACc,MAAY,SAAUZ,CAAuB,EAAEc,CAAwB,EAAEZ,CAAmB,EAAE;aAC/Fa,OAAOC,QAAQ,GAAGD,OAAOC,QAAQ,IAAI,EAAA,AAAC,EAAGjB,IAAI,CAAC;gBAC3C;gBACA,WAAY;oBACR,OAAOG,EAAoB;gBAC/B;aACH;QAGb;IACI;IACS,SAAUA,CAAmB,EAAE;QAK3BA,EAAoBe,CAAC,CAAC,GAAG;YAC9B;YACA;YACA;SACH,EAAE,WAAY;YACX,OAPOf,EAAoBA,EAAoBgB,CAAC,GAOxB;QAC5B,IAESC,OAD0BjB,EAAoBe,CAAC;IAGhE;CACC",
"names": [
"self",
"webpackChunk_N_E",
"push",
"__unused_webpack_module",
"__webpack_exports__",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"mappings": "AAAA,IAAMA,IAAM,cACZC,QAAQC,KAAR,CAAe,KAAY,OAARC,UAEnB,IAAMC,IAAM",
"mappings": "AAAA,IAAMA,GAAG,CAAG,aAAZ,CACAC,OAAO,CAACC,KAAR,CAAe,KAAY,CAA3B,MAAmBC,OAAQ,GAE3B,IAAMC,GAAG,CAAG,EAAZ",
"names": [
"xxx",
"console",
Expand Down
5 changes: 2 additions & 3 deletions crates/swc/tests/minify/issue-7475/issue-8372/1/output.map
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
{
"mappings": "AAEAA,IAAAA,EAAQC,CAFEC,IAAA,MAAA,EAASF,QAAAC,GAAA,CAAAE",
"mappings": "AAEAA,IAAAA,EAAQC,CAAG,AAFD,IAAA,MAAA,EAAS,QAAA,GAAA,CAAA",
"names": [
"console",
"log",
"aób",
"a"
"aób"
],
"sources": [
"input-preprocess.js"
Expand Down
59 changes: 16 additions & 43 deletions crates/swc_common/src/source_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1252,23 +1252,13 @@ impl SourceMap {

let mut src_id = 0u32;

if let Some(orig) = orig {
for src in orig.sources() {
let id = builder.add_source(src);
src_id = id;

builder.set_source_contents(id, orig.get_source_contents(id));
}
}

// // This method is optimized based on the fact that mapping is sorted.
// mappings.sort_by_key(|v| v.0);

let mut cur_file: Option<Lrc<SourceFile>> = None;

let mut prev_dst_line = u32::MAX;

let mut inline_sources_content = false;
let mut ch_state = ByteToCharPosState::default();
let mut line_state = ByteToCharPosState::default();

Expand Down Expand Up @@ -1300,13 +1290,11 @@ impl SourceMap {
if config.skip(&f.name) {
continue;
}
if orig.is_none() {
src_id = builder.add_source(&config.file_name_to_source(&f.name));
src_id = builder.add_source(&config.file_name_to_source(&f.name));

inline_sources_content = config.inline_sources_content(&f.name);
if inline_sources_content && orig.is_none() {
builder.set_source_contents(src_id, Some(&f.src));
}
let inline_sources_content = config.inline_sources_content(&f.name);
if inline_sources_content {
builder.set_source_contents(src_id, Some(&f.src));
}

ch_state = ByteToCharPosState::default();
Expand All @@ -1326,7 +1314,7 @@ impl SourceMap {
continue;
}

let mut line = match f.lookup_line(pos) {
let line = match f.lookup_line(pos) {
Some(line) => line as u32,
None => continue,
};
Expand All @@ -1352,31 +1340,8 @@ impl SourceMap {
linechpos,
);

let mut col = chpos - linechpos;
let mut name = None;
if let Some(orig) = &orig {
if let Some(token) = orig
.lookup_token(line, col)
.filter(|t| t.get_dst_line() == line)
{
line = token.get_src_line();
col = token.get_src_col();
if token.has_name() {
name = token.get_name();
}
if token.get_source().is_some() {
// When we have the original source map, we use the source ids from it
src_id = token.get_raw_token().src_id;
if inline_sources_content && !builder.has_source_contents(src_id) {
if let Some(contents) = token.get_source_view() {
builder.set_source_contents(src_id, Some(contents.source()));
}
}
}
} else {
continue;
}
}
let col = chpos - linechpos;
let name = None;

let name_idx = name
.or_else(|| config.name_for_bytepos(pos))
Expand All @@ -1385,7 +1350,15 @@ impl SourceMap {
builder.add_raw(lc.line, lc.col, line, col, Some(src_id), name_idx, false);
prev_dst_line = lc.line;
}
builder.into_sourcemap()

let map = builder.into_sourcemap();

if let Some(mut orig) = orig.cloned() {
orig.adjust_mappings(&map);
return orig;
}

map
}
}

Expand Down
2 changes: 1 addition & 1 deletion renovate.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@
"automerge": true
}
],
"ignorePaths": ["packages/core/e2e/fixtures", "bindings"]
"ignorePaths": ["packages/core/e2e/fixtures"]
}

0 comments on commit 05961eb

Please sign in to comment.