Skip to content

Commit

Permalink
fix(common): Do not generate invalid source map (#9050)
Browse files Browse the repository at this point in the history
**Related issue:**

 - Closes #8789
  • Loading branch information
kdy1 committed Aug 5, 2024
1 parent a8e857b commit 9d65c77
Show file tree
Hide file tree
Showing 14 changed files with 28,375 additions and 62 deletions.
5 changes: 5 additions & 0 deletions .changeset/angry-beers-dress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
swc_common: minor
---

fix(common): Do not generate invalid source map
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ resolver = "2"
siphasher = "0.3.9"
smallvec = "1.8.0"
smartstring = "1"
sourcemap = "8.0.1"
sourcemap = "9.0.0"
st-map = "0.2.0"
syn = "2"
tempfile = "3.6.0"
Expand Down
6 changes: 3 additions & 3 deletions crates/swc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ impl Compiler {
None
};

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

Expand Down Expand Up @@ -906,7 +906,7 @@ impl Compiler {
inline_sources_content: opts.inline_sources_content,
source_map,
source_map_names: &source_map_names,
orig,
orig: orig.as_ref(),
comments: Some(&comments),
emit_source_map_columns: opts.emit_source_map_columns,
preamble: &opts.format.preamble,
Expand Down Expand Up @@ -952,7 +952,7 @@ impl Compiler {
&self,
handler: &Handler,
fm: Arc<SourceFile>,
orig: Option<sourcemap::SourceMap>,
orig: Option<&sourcemap::SourceMap>,
config: BuiltInput<impl swc_ecma_visit::Fold>,
) -> Result<TransformOutput, Error> {
self.run(|| {
Expand Down
4 changes: 4 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8789/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"inputSourceMap": true,
"sourceMaps": true
}
40 changes: 40 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8789/input/a.js

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8789/input/a.js.map

Large diffs are not rendered by default.

24,389 changes: 24,389 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8789/output/a.js

Large diffs are not rendered by default.

3,882 changes: 3,882 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8789/output/a.map

Large diffs are not rendered by default.

18 changes: 8 additions & 10 deletions crates/swc/tests/fixture/sourcemap/004/output/index.map
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
{
"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,MAAKC,gBAAmBD,AAAH,GAAGA,EAAKC,GAAAA,aAAgB,GAAK,CAAD,CAAIC,EAAAA,EAAAA,AAAI,CAAC,CAAA,IAAA,CAAA;IACvD;QACI;KACH;IACD;QACU,MAAYE,CAAC,EAAEC,AAANF,CAAO,AAAN,KAAM,CAAA,EAAA,CAAA,EAAA,CAAA,EAAA;YAClB;YACA,IAAIG,IAAI,AAASH,CAAC,QAAA,CAAA,EAAA;gBACd,IAAIC,IAAMG,AAAFJ,EAAEI,AAAI,CAAA,GAAA;gBACd,OAAO,AAAC,AAAU,CAAV,AAAU,GAAPC,AAAc,EAAZC,AAAY,GAAA,AAAV,AAAU,AAAT,EAAE,AAAO,OAAA;oBACrBC,UAAUN,EAAEO,AAAG,GAAA;gBACnB;YACJ;YACAN,EAAEC,AAAa,CAAZ,AAAY,CAAXF,AAAW,IAAPC,AACK,EADHO,AACG,CADF,AACE,CAAA,AADDR,GACC,AADE;gBAEAU,SAAAA,WAAAA;oBACX,OAAA;gBACAC,SAAS;gBACET,SAAAA,WAAAA;oBACX,OAAA;gBACJ;YACA,IAAIE,IAAIH,EAAE,OAAOS,IAAI,CAAC;YAC1B,IAAA,IAAA,EAAA,OACM,IAAYV,CAAC,EAAJD,AAAME,CAAL,AAAM;wBAEd;UACA,IAAA,SAAA,CAAA,EAAA,CAAA,EAAA,CAAA,EAAA;oBACI,OAAOA,CAAAA,CAAE,EAAA,OAAA,QAAA,IAAA,EAAA,EAAA,IAAA,CAAA;gBACb;gBACH,WAAA;oBACL,OAAA,EAAA;gBACJ;aACSF,CAAC;oBAGF;YACA;IACD,SAAA,CAAA,EAAA;;;;;;mBAGP,EAAA,EAAA,CAAA",
"names": [
"self",
"webpackChunk_N_E",
"push",
"__unused_webpack_module",
"__webpack_exports__",
"__webpack_require__",
"StaticPage",
"_ref",
"n",
"t",
"u",
"r",
"data",
"react_jsx_runtime__WEBPACK_IMPORTED_MODULE_0__",
"_",
"jsx",
"children",
"foo",
"r",
"d",
"__N_SSG",
"i",
"default",
"__unused_webpack_exports",
"window",
"__NEXT_P",
"O",
Expand All @@ -29,7 +27,7 @@
"../../input/index.js"
],
"sourcesContent": [
"(self[\"webpackChunk_N_E\"] = self[\"webpackChunk_N_E\"] || []).push([\n [\n 158\n ],\n {\n /***/ 2943: /***/ function (__unused_webpack_module, __webpack_exports__, __webpack_require__) {\n \"use strict\";\n var StaticPage = function StaticPage(_ref) {\n var data = _ref.data;\n return /*#__PURE__*/ (0, react_jsx_runtime__WEBPACK_IMPORTED_MODULE_0__.jsx)(\"div\", {\n children: data.foo\n });\n };\n __webpack_require__.r(__webpack_exports__);\n /* harmony export */ __webpack_require__.d(__webpack_exports__, {\n /* harmony export */ __N_SSG: function __N_SSG1() {\n return /* binding */ __N_SSG;\n },\n /* harmony export */ default: function _default() {\n return /* binding */ StaticPage;\n }\n });\n /* harmony import */ var react_jsx_runtime__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(4512);\n var __N_SSG = true;\n /***/\n},\n /***/ 7139: /***/ function (__unused_webpack_module, __unused_webpack_exports, __webpack_require__) {\n (window.__NEXT_P = window.__NEXT_P || []).push([\n \"/static\",\n function () {\n return __webpack_require__(2943);\n }\n ]);\n if (false) { }\n /***/\n}\n },\n /******/ function (__webpack_require__) {\n // webpackRuntimeModules\n /******/ var __webpack_exec__ = function __webpack_exec__(moduleId) {\n return __webpack_require__(__webpack_require__.s = moduleId);\n };\n /******/ __webpack_require__.O(0, [\n 774,\n 888,\n 179\n ], function () {\n return __webpack_exec__(7139);\n });\n /******/ var __webpack_exports__ = __webpack_require__.O();\n /******/ _N_E = __webpack_exports__;\n /******/\n}\n]);\n"
"(self.webpackChunk_N_E = self.webpackChunk_N_E || []).push([\n [\n 158\n ],\n {\n 2943: function(n, t, u) {\n \"use strict\";\n var r = function(n) {\n var t = n.data;\n return (0, _.jsx)(\"div\", {\n children: t.foo\n });\n };\n u.r(t), u.d(t, {\n __N_SSG: function() {\n return i;\n },\n default: function() {\n return r;\n }\n });\n var _ = u(4512), i = !0;\n },\n 7139: function(n, t, u) {\n (window.__NEXT_P = window.__NEXT_P || []).push([\n \"/static\",\n function() {\n return u(2943);\n }\n ]);\n }\n },\n function(n) {\n n.O(0, [\n 774,\n 888,\n 179\n ], function() {\n return n(n.s = 7139);\n }), _N_E = n.O();\n }\n]);\n"
],
"version": 3
}
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": "MCAMA,EAAM,CAAA,GAAA,UACZC,GADA,IAAMD,CACEE,KAAK,CAAC,AAAC,KAAY,OAARC,UACnB,IAAMC,IAAM",
"names": [
"xxx",
"console",
Expand All @@ -8,10 +8,12 @@
"bbb"
],
"sources": [
"unknown"
"unknown",
"../../input/index.js"
],
"sourcesContent": [
"const xxx = ', something'\nconsole.error(`❌ ${message}`);\n\nconst bbb = ''\n"
"const xxx = ', something'\nconsole.error(`❌ ${message}`);\n\nconst bbb = ''\n",
"const xxx = ', something';\nconsole.error(`❌ ${message}`);\nconst bbb = '';\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJ4eHgiLCJjb25zb2xlIiwiZXJyb3IiLCJtZXNzYWdlIiwiYmJiIl0sInNvdXJjZXMiOlsidW5rbm93biJdLCJzb3VyY2VzQ29udGVudCI6WyJjb25zdCB4eHggPSAnLCBzb21ldGhpbmcnXG5jb25zb2xlLmVycm9yKGDinYwgJHttZXNzYWdlfWApO1xuXG5jb25zdCBiYmIgPSAnJ1xuIl0sIm1hcHBpbmdzIjoiQUFBQSxNQUFNQSxHQUFHLEdBQUcsYUFBWjtBQUNBQyxPQUFPLENBQUNDLEtBQVIsQ0FBZSxLQUFJQyxPQUFRLEVBQTNCO0FBRUEsTUFBTUMsR0FBRyxHQUFHLEVBQVoifQ==\n"
],
"version": 3
}
14 changes: 8 additions & 6 deletions crates/swc/tests/minify/issue-7475/issue-8372/1/output.map
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
{
"mappings": "AAEAA,IAAAA,EAAQC,CAFEC,IAAA,MAAA,EAASF,QAAAC,GAAA,CAAAE",
"mappings": "YCAYC;IAAK,UAAA,CAAI;AAErBC,QAAQC,GAAG,CAACH",
"names": [
"console",
"log",
"a",
"aób",
"a"
"console",
"log"
],
"sources": [
"input-preprocess.js"
"input-preprocess.js",
"$DIR/tests/minify/issue-7475/issue-8372/1/input.js"
],
"sourcesContent": [
"const a = {aób: 'ó'}\n\nconsole.log(a)"
"const a = {aób: 'ó'}\n\nconsole.log(a)",
"const a = { aób: 'ó' }\n\nconsole.log(a)"
],
"version": 3
}
55 changes: 19 additions & 36 deletions crates/swc_common/src/source_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1234,7 +1234,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 @@ -1245,7 +1245,7 @@ impl SourceMap {
pub fn build_source_map_with_config(
&self,
mappings: &[(BytePos, LineCol)],
orig: Option<sourcemap::SourceMap>,
orig: Option<&sourcemap::SourceMap>,
config: impl SourceMapGenConfig,
) -> sourcemap::SourceMap {
let mut builder = SourceMapBuilder::new(None);
Expand All @@ -1268,7 +1268,6 @@ impl SourceMap {

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 +1299,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 +1323,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 +1349,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 @@ -1386,7 +1360,16 @@ impl SourceMap {
prev_dst_line = lc.line;
}

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

if let Some(orig) = orig {
map.adjust_mappings(orig)
}
for token in map.tokens() {
dbg!(&token);
}

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 @@ -110,7 +110,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<sourcemap::SourceMap>,
pub orig: Option<&'a sourcemap::SourceMap>,
pub comments: Option<&'a dyn Comments>,
pub emit_source_map_columns: bool,
pub preamble: &'a str,
Expand Down

0 comments on commit 9d65c77

Please sign in to comment.