Handle sourcemap correctly

This commit is contained in:
강동윤 2019-02-27 17:44:11 +09:00
parent a5bc5c1490
commit 1e38d2e124
2 changed files with 32 additions and 32 deletions

View File

@ -1,8 +1,9 @@
#[cfg(feature = "fold")] #[cfg(feature = "fold")]
use fold::{FoldWith, VisitWith}; use fold::{FoldWith, VisitWith};
pub use syntax_pos::{ pub use syntax_pos::{
hygiene, BytePos, ExpnInfo, FileName, Globals, Mark, MultiSpan, SourceFile, Span, SpanData, hygiene, BytePos, CharPos, ExpnInfo, FileName, Globals, Loc, LocWithOpt, Mark, MultiSpan,
SyntaxContext, DUMMY_SP, GLOBALS, NO_EXPANSION, SourceFile, SourceFileAndBytePos, Span, SpanData, SpanLinesError, SyntaxContext, DUMMY_SP,
GLOBALS, NO_EXPANSION,
}; };
/// ///

View File

@ -4,7 +4,7 @@ use std::{
io::{self, Write}, io::{self, Write},
sync::Arc, sync::Arc,
}; };
use swc_common::{SourceMap, Span}; use swc_common::{FileName, SourceMap, Span};
/// ///
/// ----- /// -----
@ -58,29 +58,41 @@ impl<'a, W: Write> JsWriter<'a, W> {
fn raw_write(&mut self, data: &[u8]) -> io::Result<usize> { fn raw_write(&mut self, data: &[u8]) -> io::Result<usize> {
let written = self.wr.write(data)?; let written = self.wr.write(data)?;
self.written_bytes += written; self.written_bytes += written;
self.line_pos += written;
Ok(written) Ok(written)
} }
fn write(&mut self, span: Option<Span>, data: &str) -> io::Result<usize> { fn write(&mut self, span: Option<Span>, data: &str) -> io::Result<usize> {
let mut cnt = 0; let mut cnt = 0;
macro_rules! srcmap {
($byte_pos:expr) => {{
match self.srcmap {
Some(ref mut srcmap) => {
let loc = self.cm.lookup_char_pos($byte_pos);
let src = match loc.file.name {
FileName::Real(ref p) => Some(p.display().to_string()),
_ => None,
};
srcmap.add(
self.line_count as _,
self.line_pos as _,
loc.line as _,
loc.col.0 as _,
src.as_ref().map(|s| &**s),
None,
);
}
_ => {}
}
}};
}
if data.len() > 0 { if data.len() > 0 {
if let Some(span) = span { if let Some(span) = span {
if !span.is_dummy() { if !span.is_dummy() {
match self.srcmap { srcmap!(span.lo())
Some(ref mut srcmap) => {
let loc = self.cm.lookup_char_pos(span.lo());
srcmap.add(
self.line_count as _,
self.line_pos as _,
loc.line as _,
loc.col.0 as _,
None,
None,
);
}
_ => {}
}
} }
} }
@ -92,20 +104,7 @@ impl<'a, W: Write> JsWriter<'a, W> {
if let Some(span) = span { if let Some(span) = span {
if !span.is_dummy() { if !span.is_dummy() {
match self.srcmap { srcmap!(span.hi())
Some(ref mut srcmap) => {
let loc = self.cm.lookup_char_pos(span.hi());
srcmap.add(
self.line_count as _,
self.line_pos as _,
loc.line as _,
loc.col.0 as _,
None,
None,
);
}
None => {}
}
} }
} }
} }
@ -157,7 +156,7 @@ impl<'a, W: Write> WriteJs for JsWriter<'a, W> {
if !self.line_start { if !self.line_start {
self.raw_write(self.new_line.as_bytes())?; self.raw_write(self.new_line.as_bytes())?;
self.line_count += 1; self.line_count += 1;
self.line_pos = self.written_bytes; self.line_pos = 0;
self.line_start = true; self.line_start = true;
} }