@@ -217,23 +217,26 @@ func (x *FileSyntax) Cleanup() {
217
217
case * LineBlock :
218
218
ww := 0
219
219
for _ , line := range stmt .Line {
220
- if line .Token != nil {
220
+ line .Comments = removeEmptyComments (line .Comments )
221
+ if line .Token != nil || ! isEmptyComment (line .Comments ) {
221
222
stmt .Line [ww ] = line
222
223
ww ++
223
224
}
224
225
}
225
226
if ww == 0 {
226
227
continue
227
228
}
229
+ // **Check and remove empty RParen comments**
230
+ stmt .RParen .Comments = removeEmptyComments (stmt .RParen .Comments )
228
231
if ww == 1 && len (stmt .RParen .Comments .Before ) == 0 {
229
232
// Collapse block into single line but keep the Line reference used by the
230
233
// parsed File structure.
231
234
* stmt .Line [0 ] = Line {
232
- Comments : Comments {
235
+ Comments : removeEmptyComments ( Comments {
233
236
Before : commentsAdd (stmt .Before , stmt .Line [0 ].Before ),
234
237
Suffix : commentsAdd (stmt .Line [0 ].Suffix , stmt .Suffix ),
235
238
After : commentsAdd (stmt .Line [0 ].After , stmt .After ),
236
- },
239
+ }) ,
237
240
Token : stringsAdd (stmt .Token , stmt .Line [0 ].Token ),
238
241
}
239
242
x .Stmt [w ] = stmt .Line [0 ]
@@ -248,6 +251,27 @@ func (x *FileSyntax) Cleanup() {
248
251
x .Stmt = x .Stmt [:w ]
249
252
}
250
253
254
+ // removeEmptyComments removes empty comment sections.
255
+ func removeEmptyComments (c Comments ) Comments {
256
+ // Remove comments with empty tokens and zero position markers
257
+ var newBefore []Comment
258
+ for _ , cm := range c .Before {
259
+ if cm .Token != "" || cm .Start .Line != 0 || cm .Start .Byte != 0 {
260
+ newBefore = append (newBefore , cm )
261
+ }
262
+ }
263
+
264
+ if len (newBefore ) == 0 && len (c .Suffix ) == 0 && len (c .After ) == 0 {
265
+ return Comments {}
266
+ }
267
+ return Comments {Before : newBefore , Suffix : c .Suffix , After : c .After }
268
+ }
269
+
270
+ // isEmptyComment checks if a comment structure is completely empty.
271
+ func isEmptyComment (c Comments ) bool {
272
+ return len (c .Before ) == 0 && len (c .Suffix ) == 0 && len (c .After ) == 0
273
+ }
274
+
251
275
func commentsAdd (x , y []Comment ) []Comment {
252
276
return append (x [:len (x ):len (x )], y ... )
253
277
}
@@ -865,11 +889,6 @@ func (in *input) parseLineBlock(start Position, token []string, lparen token) *L
865
889
// Suffix comment, will be attached later by assignComments.
866
890
in .lex ()
867
891
case '\n' :
868
- // Skip blank lines in require block or else add an empty comment to preserve it.
869
- if strings .Join (x .Token , " " ) == "require" {
870
- in .lex ()
871
- continue
872
- }
873
892
in .lex ()
874
893
if len (comments ) == 0 && len (x .Line ) > 0 || len (comments ) > 0 && comments [len (comments )- 1 ].Token != "" {
875
894
comments = append (comments , Comment {})
0 commit comments