fix(es/codegen): Fix sourcemaps of multi line block comments (#1511)

swc_ecma_codegen:
 - Fix sourcemaps of multiline block comments.

Co-authored-by: 강동윤 <kdy1997.dev@gmail.com>
This commit is contained in:
Devon Govett 2021-03-29 10:41:05 -04:00 committed by GitHub
parent 99f4f0f280
commit 393808a8f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 7 deletions

View File

@ -7,7 +7,7 @@ include = ["Cargo.toml", "src/**/*.rs"]
license = "Apache-2.0/MIT"
name = "swc_ecma_codegen"
repository = "https://github.com/swc-project/swc.git"
version = "0.49.0"
version = "0.49.1"
[dependencies]
bitflags = "1"

View File

@ -23,7 +23,7 @@ macro_rules! write_comments {
$e.wr.write_comment(cmt.span, " ")?;
}
$e.wr.write_comment(cmt.span, "/*")?;
$e.wr.write_comment(cmt.span, &cmt.text)?;
$e.wr.write_lit(cmt.span, &cmt.text)?;
$e.wr.write_comment(cmt.span, "*/")?;
$e.wr.write_space()?;
}

View File

@ -163,13 +163,20 @@ impl<'a, W: Write> WriteJs for JsWriter<'a, W> {
fn write_lit(&mut self, span: Span, s: &str) -> Result {
if !s.is_empty() {
self.write(Some(span), s)?;
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 =
self.written_bytes - s.len() + line_start_of_s.last().cloned().unwrap_or(0);
self.line_pos = s.len() - line_start_of_s.last().cloned().unwrap_or(0);
}
if !span.is_dummy() {
self.srcmap(span.hi())
}
}
@ -223,8 +230,7 @@ fn compute_line_starts(s: &str) -> Vec<usize> {
'\n' => {
res.push(line_start);
line_start = pos;
break;
line_start = pos + 1;
}
_ => {}