@@ -217,23 +217,26 @@ func (x *FileSyntax) Cleanup() {
217217 case * LineBlock :
218218 ww := 0
219219 for _ , line := range stmt .Line {
220- if line .Token != nil {
220+ line .Comments = removeEmptyComments (line .Comments )
221+ if line .Token != nil || ! isEmptyComment (line .Comments ) {
221222 stmt .Line [ww ] = line
222223 ww ++
223224 }
224225 }
225226 if ww == 0 {
226227 continue
227228 }
229+ // **Check and remove empty RParen comments**
230+ stmt .RParen .Comments = removeEmptyComments (stmt .RParen .Comments )
228231 if ww == 1 && len (stmt .RParen .Comments .Before ) == 0 {
229232 // Collapse block into single line but keep the Line reference used by the
230233 // parsed File structure.
231234 * stmt .Line [0 ] = Line {
232- Comments : Comments {
235+ Comments : removeEmptyComments ( Comments {
233236 Before : commentsAdd (stmt .Before , stmt .Line [0 ].Before ),
234237 Suffix : commentsAdd (stmt .Line [0 ].Suffix , stmt .Suffix ),
235238 After : commentsAdd (stmt .Line [0 ].After , stmt .After ),
236- },
239+ }) ,
237240 Token : stringsAdd (stmt .Token , stmt .Line [0 ].Token ),
238241 }
239242 x .Stmt [w ] = stmt .Line [0 ]
@@ -248,6 +251,27 @@ func (x *FileSyntax) Cleanup() {
248251 x .Stmt = x .Stmt [:w ]
249252}
250253
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+
251275func commentsAdd (x , y []Comment ) []Comment {
252276 return append (x [:len (x ):len (x )], y ... )
253277}
@@ -865,11 +889,6 @@ func (in *input) parseLineBlock(start Position, token []string, lparen token) *L
865889 // Suffix comment, will be attached later by assignComments.
866890 in .lex ()
867891 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- }
873892 in .lex ()
874893 if len (comments ) == 0 && len (x .Line ) > 0 || len (comments ) > 0 && comments [len (comments )- 1 ].Token != "" {
875894 comments = append (comments , Comment {})
0 commit comments