Skip to content

Commit

Permalink
feat(common): Use SourceMap::adjust_mappings (#9052)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 committed Jun 14, 2024
1 parent 8b66428 commit eda2e45
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 55 deletions.
6 changes: 3 additions & 3 deletions crates/swc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@ impl Compiler {
None
};

self.apply_transforms(handler, orig.as_ref(), config)
self.apply_transforms(handler, orig, config)
})
}

Expand Down Expand Up @@ -904,7 +904,7 @@ impl Compiler {
inline_sources_content: opts.inline_sources_content,
source_map,
source_map_names: &source_map_names,
orig: orig.as_ref(),
orig,
comments: Some(&comments),
emit_source_map_columns: opts.emit_source_map_columns,
preamble: &opts.format.preamble,
Expand Down Expand Up @@ -949,7 +949,7 @@ impl Compiler {
fn apply_transforms(
&self,
handler: &Handler,
orig: Option<&sourcemap::SourceMap>,
orig: Option<sourcemap::SourceMap>,
config: BuiltInput<impl swc_ecma_visit::Fold>,
) -> Result<TransformOutput, Error> {
self.run(|| {
Expand Down
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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B;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,YAAIC,OAAQ,dAAA,CAA3B,gBAEA,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"
],
"rangeMappings": "",
"sources": [
Expand Down
65 changes: 20 additions & 45 deletions crates/swc_common/src/source_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1201,7 +1201,7 @@ impl SourceMap {
pub fn build_source_map_from(
&self,
mappings: &[(BytePos, LineCol)],
orig: Option<&sourcemap::SourceMap>,
orig: Option<sourcemap::SourceMap>,
) -> sourcemap::SourceMap {
self.build_source_map_with_config(mappings, orig, DefaultSourceMapGenConfig)
}
Expand All @@ -1212,30 +1212,20 @@ impl SourceMap {
pub fn build_source_map_with_config(
&self,
mappings: &[(BytePos, LineCol)],
orig: Option<&sourcemap::SourceMap>,
mut orig: Option<sourcemap::SourceMap>,
config: impl SourceMapGenConfig,
) -> sourcemap::SourceMap {
let mut builder = SourceMapBuilder::new(None);

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 @@ -1267,13 +1257,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 @@ -1293,7 +1281,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 @@ -1319,31 +1307,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 @@ -1353,7 +1318,17 @@ impl SourceMap {
prev_dst_line = lc.line;
}

builder.into_sourcemap()
let mut map = builder.into_sourcemap();

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

if let Some(orig) = orig {
map = orig;
}

map
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/swc_compiler_base/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ pub struct PrintArgs<'a> {
pub inline_sources_content: bool,
pub source_map: SourceMapsConfig,
pub source_map_names: &'a AHashMap<BytePos, JsWord>,
pub orig: Option<&'a sourcemap::SourceMap>,
pub orig: Option<sourcemap::SourceMap>,
pub comments: Option<&'a dyn Comments>,
pub emit_source_map_columns: bool,
pub preamble: &'a str,
Expand Down

0 comments on commit eda2e45

Please sign in to comment.