Skip to content

Commit 001b24e

Browse files
committed
fix(formatter): skip the leading semicolon when calculating leading lines
1 parent 9e36186 commit 001b24e

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

crates/oxc_formatter/src/formatter/source_text.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ impl<'a> SourceText<'a> {
160160
&& comment.span.end <= start
161161
{
162162
start = comment.span.start;
163+
} else if start != 0 && matches!(self.byte_at(start - 1), Some(b';')) {
164+
// Skip leading semicolon if present
165+
// `;(function() {});`
166+
start -= 1;
163167
}
164168

165169
// Count the newlines in the leading trivia of the next node
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const a = 1
2+
3+
;(function() {
4+
const b = 2;
5+
})()
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
source: crates/oxc_formatter/tests/fixtures/mod.rs
3+
---
4+
==================== Input ====================
5+
const a = 1
6+
7+
;(function() {
8+
const b = 2;
9+
})()
10+
11+
==================== Output ====================
12+
--------------
13+
{ semi: true }
14+
--------------
15+
const a = 1;
16+
17+
(function () {
18+
const b = 2;
19+
})();
20+
21+
---------------
22+
{ semi: false }
23+
---------------
24+
const a = 1
25+
26+
;(function () {
27+
const b = 2
28+
})()
29+
30+
===================== End =====================
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[
2+
{
3+
"semi": true
4+
},
5+
{
6+
"semi": false
7+
}
8+
]

0 commit comments

Comments
 (0)