diff --git a/crates/oxc_formatter/src/formatter/source_text.rs b/crates/oxc_formatter/src/formatter/source_text.rs index 8cf8ffdb9ecf6..06eb3d42f0b2c 100644 --- a/crates/oxc_formatter/src/formatter/source_text.rs +++ b/crates/oxc_formatter/src/formatter/source_text.rs @@ -160,6 +160,10 @@ impl<'a> SourceText<'a> { && comment.span.end <= start { start = comment.span.start; + } else if start != 0 && matches!(self.byte_at(start - 1), Some(b';')) { + // Skip leading semicolon if present + // `;(function() {});` + start -= 1; } // Count the newlines in the leading trivia of the next node diff --git a/crates/oxc_formatter/tests/fixtures/js/semicolons/empty-line.js b/crates/oxc_formatter/tests/fixtures/js/semicolons/empty-line.js new file mode 100644 index 0000000000000..d3e8dd4482d6f --- /dev/null +++ b/crates/oxc_formatter/tests/fixtures/js/semicolons/empty-line.js @@ -0,0 +1,5 @@ +const a = 1 + +;(function() { + const b = 2; +})() diff --git a/crates/oxc_formatter/tests/fixtures/js/semicolons/empty-line.js.snap b/crates/oxc_formatter/tests/fixtures/js/semicolons/empty-line.js.snap new file mode 100644 index 0000000000000..c939f9895d078 --- /dev/null +++ b/crates/oxc_formatter/tests/fixtures/js/semicolons/empty-line.js.snap @@ -0,0 +1,30 @@ +--- +source: crates/oxc_formatter/tests/fixtures/mod.rs +--- +==================== Input ==================== +const a = 1 + +;(function() { + const b = 2; +})() + +==================== Output ==================== +-------------- +{ semi: true } +-------------- +const a = 1; + +(function () { + const b = 2; +})(); + +--------------- +{ semi: false } +--------------- +const a = 1 + +;(function () { + const b = 2 +})() + +===================== End ===================== diff --git a/crates/oxc_formatter/tests/fixtures/js/semicolons/options.json b/crates/oxc_formatter/tests/fixtures/js/semicolons/options.json new file mode 100644 index 0000000000000..e02982cb6b5ad --- /dev/null +++ b/crates/oxc_formatter/tests/fixtures/js/semicolons/options.json @@ -0,0 +1,8 @@ +[ + { + "semi": true + }, + { + "semi": false + } +] \ No newline at end of file