From a3bd6aea54f8ad6eacbcb1cb67cb19c6c1cd2be8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Fri, 10 Dec 2021 19:57:39 +0900 Subject: [PATCH] fix(es/codegen): Fix sourcemap (#3003) swc_ecma_codegen: - Handle source map of multi-line template literals. (Closes #2185, Closes #2362, Closes #2900) --- Cargo.lock | 4 ++-- .../{ssue-1600 => issue-1600}/input/.swcrc | 0 .../{ssue-1600 => issue-1600}/input/index.js | 0 .../{ssue-1600 => issue-1600}/output/index.js | 0 .../fixture/sourcemap/004/output/index.map | 2 +- .../fixture/sourcemap/issue-2185/input/.swcrc | 6 ++++++ .../sourcemap/issue-2185/input/index.js | 17 +++++++++++++++++ .../sourcemap/issue-2185/output/index.js | 14 ++++++++++++++ .../sourcemap/issue-2185/output/index.map | 17 +++++++++++++++++ crates/swc_ecma_codegen/Cargo.toml | 2 +- .../src/text_writer/basic_impl.rs | 19 ++++++++++++++++++- crates/wasm/Cargo.toml | 2 +- package.json | 2 +- 13 files changed, 78 insertions(+), 7 deletions(-) rename crates/swc/tests/fixture/{ssue-1600 => issue-1600}/input/.swcrc (100%) rename crates/swc/tests/fixture/{ssue-1600 => issue-1600}/input/index.js (100%) rename crates/swc/tests/fixture/{ssue-1600 => issue-1600}/output/index.js (100%) create mode 100644 crates/swc/tests/fixture/sourcemap/issue-2185/input/.swcrc create mode 100644 crates/swc/tests/fixture/sourcemap/issue-2185/input/index.js create mode 100644 crates/swc/tests/fixture/sourcemap/issue-2185/output/index.js create mode 100644 crates/swc/tests/fixture/sourcemap/issue-2185/output/index.map diff --git a/Cargo.lock b/Cargo.lock index 5c96e17d262..ecf16f14a35 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2695,7 +2695,7 @@ dependencies = [ [[package]] name = "swc_ecma_codegen" -version = "0.83.0" +version = "0.83.1" dependencies = [ "bitflags", "memchr", @@ -3826,7 +3826,7 @@ checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" [[package]] name = "wasm" -version = "1.2.118" +version = "1.2.119" dependencies = [ "anyhow", "console_error_panic_hook", diff --git a/crates/swc/tests/fixture/ssue-1600/input/.swcrc b/crates/swc/tests/fixture/issue-1600/input/.swcrc similarity index 100% rename from crates/swc/tests/fixture/ssue-1600/input/.swcrc rename to crates/swc/tests/fixture/issue-1600/input/.swcrc diff --git a/crates/swc/tests/fixture/ssue-1600/input/index.js b/crates/swc/tests/fixture/issue-1600/input/index.js similarity index 100% rename from crates/swc/tests/fixture/ssue-1600/input/index.js rename to crates/swc/tests/fixture/issue-1600/input/index.js diff --git a/crates/swc/tests/fixture/ssue-1600/output/index.js b/crates/swc/tests/fixture/issue-1600/output/index.js similarity index 100% rename from crates/swc/tests/fixture/ssue-1600/output/index.js rename to crates/swc/tests/fixture/issue-1600/output/index.js diff --git a/crates/swc/tests/fixture/sourcemap/004/output/index.map b/crates/swc/tests/fixture/sourcemap/004/output/index.map index 2c3701f34f1..d3a889b799c 100644 --- a/crates/swc/tests/fixture/sourcemap/004/output/index.map +++ b/crates/swc/tests/fixture/sourcemap/004/output/index.map @@ -1,5 +1,5 @@ { - "mappings": "CEACA,IAAI,CAAC,gBAAkB,GAAIA,IAAI,CAAC,gBAAkB,IAAK,CAAC,CAAC,EAAEC,IAAI,CAAC,CAAC;IAAA,CAAC;QAAA,GAAG;IAAA,CAAC;IAAE,CAAC;QAEhE,IAAI,EACH,QAAQ,CAAEC,CAAuB,EAAEC,CAAmB,EAAEC,CAAmB,EAAE,CAAC;YAE7E,CAAY;gBFJtBC,CAAO,GAAP,QAAA,CAAOC,CAAA,EAAA,CAAA;gBAAM,GAAK,CAAAC,CAAA,GAAAD,CAAA,CAAAC,IAAA;gBAAlB,MAAA,KAAAC,CAAA,CAAAC,GAAA,EAAA,CAAA,MAAA,CAAA;oBACDC,QAAA,EAAAH,CAAA,CAAAI,GAAA;gBAAA,CAAA;YAAA,CAAA;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swc/tests/fixture/sourcemap/issue-2185/input/.swcrc new file mode 100644 index 00000000000..adcba14ac28 --- /dev/null +++ b/crates/swc/tests/fixture/sourcemap/issue-2185/input/.swcrc @@ -0,0 +1,6 @@ +{ + "sourceMaps": true, + "jsc": { + "target": "es2015" + } +} \ No newline at end of file diff --git a/crates/swc/tests/fixture/sourcemap/issue-2185/input/index.js b/crates/swc/tests/fixture/sourcemap/issue-2185/input/index.js new file mode 100644 index 00000000000..4c8381b546a --- /dev/null +++ b/crates/swc/tests/fixture/sourcemap/issue-2185/input/index.js @@ -0,0 +1,17 @@ +describe("foo", () => { + it("test1", () => { + expect(false).toBe(true); + }); + + it("test2", () => { + const str = ` + bar + `; + + expect(false).toBe(true); + }); + + it("test3", () => { + expect(false).toBe(true); + }); +}); \ No newline at end of file diff --git a/crates/swc/tests/fixture/sourcemap/issue-2185/output/index.js b/crates/swc/tests/fixture/sourcemap/issue-2185/output/index.js new file mode 100644 index 00000000000..b16f28510bf --- /dev/null +++ b/crates/swc/tests/fixture/sourcemap/issue-2185/output/index.js @@ -0,0 +1,14 @@ +describe("foo", ()=>{ + it("test1", ()=>{ + expect(false).toBe(true); + }); + it("test2", ()=>{ + const str = ` + bar + `; + expect(false).toBe(true); + }); + it("test3", ()=>{ + expect(false).toBe(true); + }); +}); diff --git a/crates/swc/tests/fixture/sourcemap/issue-2185/output/index.map b/crates/swc/tests/fixture/sourcemap/issue-2185/output/index.map new file mode 100644 index 00000000000..355af30693a --- /dev/null +++ b/crates/swc/tests/fixture/sourcemap/issue-2185/output/index.map @@ -0,0 +1,17 @@ +{ + "mappings": "AAAAA,QAAQ,CAAC,CAAK,UAAQ,CAAC;IACnBC,EAAE,CAAC,CAAO,YAAQ,CAAC;QACfC,MAAM,CAAC,KAAK,EAAEC,IAAI,CAAC,IAAI;IAC3B,CAAC;IAEDF,EAAE,CAAC,CAAO,YAAQ,CAAC;QACf,KAAK,CAACG,GAAG,IAAI;;MAEf;QAEEF,MAAM,CAAC,KAAK,EAAEC,IAAI,CAAC,IAAI;IAC3B,CAAC;IAEDF,EAAE,CAAC,CAAO,YAAQ,CAAC;QACfC,MAAM,CAAC,KAAK,EAAEC,IAAI,CAAC,IAAI;IAC3B,CAAC;AACL,CAAC", + "names": [ + "describe", + "it", + "expect", + "toBe", + "str" + ], + "sources": [ + "../../input/index.js" + ], + "sourcesContent": [ + "describe(\"foo\", () => {\n it(\"test1\", () => {\n expect(false).toBe(true);\n });\n\n it(\"test2\", () => {\n const str = `\n bar\n `;\n\n expect(false).toBe(true);\n });\n\n it(\"test3\", () => {\n expect(false).toBe(true);\n });\n});" + ], + "version": 3 +} diff --git a/crates/swc_ecma_codegen/Cargo.toml b/crates/swc_ecma_codegen/Cargo.toml index 0fca22648ab..be8c5e3bcc7 100644 --- a/crates/swc_ecma_codegen/Cargo.toml +++ b/crates/swc_ecma_codegen/Cargo.toml @@ -7,7 +7,7 @@ include = ["Cargo.toml", "src/**/*.rs"] license = "Apache-2.0" name = "swc_ecma_codegen" repository = "https://github.com/swc-project/swc.git" -version = "0.83.0" +version = "0.83.1" [dependencies] bitflags = "1" diff --git a/crates/swc_ecma_codegen/src/text_writer/basic_impl.rs b/crates/swc_ecma_codegen/src/text_writer/basic_impl.rs index 36175d38aea..3adfe71ab47 100644 --- a/crates/swc_ecma_codegen/src/text_writer/basic_impl.rs +++ b/crates/swc_ecma_codegen/src/text_writer/basic_impl.rs @@ -188,7 +188,24 @@ impl<'a, W: Write> WriteJs for JsWriter<'a, W> { } fn write_str_lit(&mut self, span: Span, s: &str) -> Result { - self.write(Some(span), s)?; + if !s.is_empty() { + if !span.is_dummy() { + self.srcmap(span.lo()) + } + + self.write(None, s)?; + + let line_start_of_s = compute_line_starts(s); + if line_start_of_s.len() > 1 { + self.line_count = self.line_count + line_start_of_s.len() - 1; + self.line_pos = s.len() - line_start_of_s.last().cloned().unwrap_or(0); + } + + if !span.is_dummy() { + self.srcmap(span.hi()) + } + } + Ok(()) } diff --git a/crates/wasm/Cargo.toml b/crates/wasm/Cargo.toml index 8b91a22e1f8..e2042310918 100644 --- a/crates/wasm/Cargo.toml +++ b/crates/wasm/Cargo.toml @@ -6,7 +6,7 @@ license = "Apache-2.0" name = "wasm" publish = false repository = "https://github.com/swc-project/swc.git" -version = "1.2.118" +version = "1.2.119" [lib] crate-type = ["cdylib"] diff --git a/package.json b/package.json index 3683474275c..14ccd6224d2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@swc/core", - "version": "1.2.118", + "version": "1.2.119", "description": "Super-fast alternative for babel", "homepage": "https://swc.rs", "main": "./index.js",