fix(es): Fix sourcemap (#1548)

swc_ecma_codegen:
 - Consider indentions while calculating starting point of source map entries. (denoland/deno#10014)
This commit is contained in:
강동윤 2021-04-06 20:26:51 +09:00 committed by GitHub
parent 228429c7bb
commit 62d0cbcabb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 62 additions and 6 deletions

View File

@ -76,16 +76,17 @@ impl<'a, W: Write> JsWriter<'a, W> {
let mut cnt = 0; let mut cnt = 0;
if !data.is_empty() { if !data.is_empty() {
if self.line_start {
cnt += self.write_indent_string()?;
self.line_start = false;
}
if let Some(span) = span { if let Some(span) = span {
if !span.is_dummy() { if !span.is_dummy() {
self.srcmap(span.lo()) self.srcmap(span.lo())
} }
} }
if self.line_start {
cnt += self.write_indent_string()?;
self.line_start = false;
}
cnt += self.raw_write(data.as_bytes())?; cnt += self.raw_write(data.as_bytes())?;
if let Some(span) = span { if let Some(span) = span {

View File

@ -0,0 +1,3 @@
{
"sourceMaps": true
}

View File

@ -0,0 +1,14 @@
import "./errors.ts";
function a() {
t();
}
function t(x = false) {
if (x) {
throw new Error("Hello");
}
t(!0);
}
a();

View File

@ -0,0 +1,11 @@
{
"mappings": "UAAO,WAAa;SAEX,CAAC;IACN,CAAC;;SAGI,CAAC,CAAC,KAAS;QAAT,CAAC,GAAD,KAAS,cAAL,KAAK,GAAT,KAAS;QACZ,CAAC;kBACS,KAAK,EAAC,KAAO;;IAE3B,CAAC,EAAE,CAAC;;AAGR,CAAC",
"names": [],
"sources": [
"$DIR/tests/fixture/deno-10014/case1/input/index.ts"
],
"sourcesContent": [
"import \"./errors.ts\";\n\nfunction a() {\n t();\n}\n\nfunction t(x = false) {\n if (x) {\n throw new Error(\"Hello\");\n }\n t(!0);\n}\n\na();"
],
"version": 3
}

View File

@ -0,0 +1,12 @@
import "./errors.ts";
function a() {
t();
}
function t(param) {
var x = param === void 0 ? false : param;
if (x) {
throw new Error("Hello");
}
t(!0);
}
a();

View File

@ -1 +1,11 @@
{"version":3,"sources":["$DIR/tests/fixture/issue-1309/case1/input/index.ts"],"sourcesContent":["/n/nexport const foo = 1;"],"names":[],"mappings":";;;;;MAEa,GAAG,GAAG,CAAC;QAAP,GAAG,GAAH,GAAG"} {
"mappings": ";;;;;MAEa,GAAG,GAAG,CAAC;QAAP,GAAG,GAAH,GAAG",
"names": [],
"sources": [
"$DIR/tests/fixture/issue-1309/case1/input/index.ts"
],
"sourcesContent": [
"\n\nexport const foo = 1;"
],
"version": 3
}

View File

@ -611,7 +611,12 @@ fn tests(dir: PathBuf) {
.compare_to_file(output.join(entry.file_name())) .compare_to_file(output.join(entry.file_name()))
.unwrap(); .unwrap();
NormalizedOutput::from(v.map.unwrap_or_default()) let map = v.map.map(|json| {
let json: serde_json::Value = serde_json::from_str(&json).unwrap();
serde_json::to_string_pretty(&json).unwrap()
});
NormalizedOutput::from(map.unwrap_or_default())
.compare_to_file( .compare_to_file(
output output
.join(entry.path().with_extension("map").file_name().unwrap()), .join(entry.path().with_extension("map").file_name().unwrap()),