Skip to content

Commit

Permalink
Commit last semi
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 committed Aug 29, 2022
1 parent 0e41b16 commit 19b756e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 27 deletions.
2 changes: 2 additions & 0 deletions crates/swc_ecma_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ where
if !self.cfg.omit_last_semi {
self.wr.commit_pending_semi()?;
}
self.wr.commit_pending_semi()?;
}

#[emitter]
Expand All @@ -133,6 +134,7 @@ where
if !self.cfg.omit_last_semi {
self.wr.commit_pending_semi()?;
}
self.wr.commit_pending_semi()?;
}

#[emitter]
Expand Down
52 changes: 26 additions & 26 deletions crates/swc_ecma_codegen/src/text_writer/basic_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,37 +60,37 @@ impl<'a, W: Write> JsWriter<'a, W> {
Ok(())
}

fn write(&mut self, span: Option<Span>, data: &str) -> Result {
fn write(&mut self, span: Option<Span>, data: &str, force_srcmap: bool) -> Result {
if !data.is_empty() {
if self.line_start {
self.write_indent_string()?;
self.line_start = false;

if let Some(pending) = self.pending_srcmap.take() {
self.srcmap(pending);
self.srcmap(pending, false);
}
}

if let Some(span) = span {
if !span.is_dummy() {
self.srcmap(span.lo())
if force_srcmap || !span.is_dummy() {
self.srcmap(span.lo(), force_srcmap)
}
}

self.raw_write(data)?;

if let Some(span) = span {
if !span.is_dummy() {
self.srcmap(span.hi())
if force_srcmap || !span.is_dummy() {
self.srcmap(span.hi(), force_srcmap)
}
}
}

Ok(())
}

fn srcmap(&mut self, byte_pos: BytePos) {
if byte_pos.is_dummy() {
fn srcmap(&mut self, byte_pos: BytePos, force: bool) {
if !force && byte_pos.is_dummy() {
return;
}

Expand Down Expand Up @@ -121,32 +121,32 @@ impl<'a, W: Write> WriteJs for JsWriter<'a, W> {
}

fn write_semi(&mut self, span: Option<Span>) -> Result {
self.write(span, ";")?;
self.write(span, ";", true)?;
Ok(())
}

fn write_space(&mut self) -> Result {
self.write(None, " ")?;
self.write(None, " ", false)?;
Ok(())
}

fn write_keyword(&mut self, span: Option<Span>, s: &'static str) -> Result {
self.write(span, s)?;
self.write(span, s, false)?;
Ok(())
}

fn write_operator(&mut self, span: Option<Span>, s: &str) -> Result {
self.write(span, s)?;
self.write(span, s, false)?;
Ok(())
}

fn write_param(&mut self, s: &str) -> Result {
self.write(None, s)?;
self.write(None, s, false)?;
Ok(())
}

fn write_property(&mut self, s: &str) -> Result {
self.write(None, s)?;
self.write(None, s, false)?;
Ok(())
}

Expand All @@ -159,7 +159,7 @@ impl<'a, W: Write> WriteJs for JsWriter<'a, W> {
self.line_start = true;

if let Some(pending) = pending {
self.srcmap(pending)
self.srcmap(pending, false)
}
}

Expand All @@ -169,10 +169,10 @@ impl<'a, W: Write> WriteJs for JsWriter<'a, W> {
fn write_lit(&mut self, span: Span, s: &str) -> Result {
if !s.is_empty() {
if !span.is_dummy() {
self.srcmap(span.lo())
self.srcmap(span.lo(), false)
}

self.write(None, s)?;
self.write(None, s, false)?;

let line_start_of_s = compute_line_starts(s);
if line_start_of_s.len() > 1 {
Expand All @@ -182,15 +182,15 @@ impl<'a, W: Write> WriteJs for JsWriter<'a, W> {
}

if !span.is_dummy() {
self.srcmap(span.hi())
self.srcmap(span.hi(), false)
}
}

Ok(())
}

fn write_comment(&mut self, s: &str) -> Result {
self.write(None, s)?;
self.write(None, s, false)?;
{
let line_start_of_s = compute_line_starts(s);
if line_start_of_s.len() > 1 {
Expand All @@ -205,10 +205,10 @@ impl<'a, W: Write> WriteJs for JsWriter<'a, W> {
fn write_str_lit(&mut self, span: Span, s: &str) -> Result {
if !s.is_empty() {
if !span.is_dummy() {
self.srcmap(span.lo())
self.srcmap(span.lo(), false)
}

self.write(None, s)?;
self.write(None, s, false)?;

let line_start_of_s = compute_line_starts(s);
if line_start_of_s.len() > 1 {
Expand All @@ -218,25 +218,25 @@ impl<'a, W: Write> WriteJs for JsWriter<'a, W> {
}

if !span.is_dummy() {
self.srcmap(span.hi())
self.srcmap(span.hi(), false)
}
}

Ok(())
}

fn write_str(&mut self, s: &str) -> Result {
self.write(None, s)?;
self.write(None, s, false)?;
Ok(())
}

fn write_symbol(&mut self, span: Span, s: &str) -> Result {
self.write(Some(span), s)?;
self.write(Some(span), s, false)?;
Ok(())
}

fn write_punct(&mut self, span: Option<Span>, s: &'static str) -> Result {
self.write(span, s)?;
self.write(span, s, false)?;
Ok(())
}

Expand All @@ -248,7 +248,7 @@ impl<'a, W: Write> WriteJs for JsWriter<'a, W> {
if self.line_start {
self.pending_srcmap = Some(pos);
} else {
self.srcmap(pos);
self.srcmap(pos, false);
}
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_codegen/tests/sourcemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ fn identity(entry: PathBuf) {

let actual_code = String::from_utf8(wr).unwrap();

if actual_code != expected_code && format!("{};", actual_code) != expected_code {
if actual_code != expected_code {
// Generated code is different
// We can't ensure that identical sourcemap will mean identical code
eprintln!("Actual code:\n{}", actual_code);
Expand Down

0 comments on commit 19b756e

Please sign in to comment.