Skip to content

Commit 63c7e0d

Browse files
committed
Don't create CommentMap for file
1 parent 378de5e commit 63c7e0d

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

wsl.go

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ func (w *WSL) checkCaseClause(stmt *ast.CaseClause, cursor *Cursor) {
518518
w.checkCaseLeadingNewline(stmt)
519519

520520
if w.config.CaseMaxLines != 0 {
521-
w.checkCaseTrailingNewline(stmt, stmt.Body, cursor)
521+
w.checkCaseTrailingNewline(stmt.Body, cursor)
522522
}
523523

524524
w.checkBody(stmt.Body)
@@ -528,7 +528,7 @@ func (w *WSL) checkCommClause(stmt *ast.CommClause, cursor *Cursor) {
528528
w.checkCommLeadingNewline(stmt)
529529

530530
if w.config.CaseMaxLines != 0 {
531-
w.checkCaseTrailingNewline(stmt, stmt.Body, cursor)
531+
w.checkCaseTrailingNewline(stmt.Body, cursor)
532532
}
533533

534534
w.checkBody(stmt.Body)
@@ -941,7 +941,7 @@ func (w *WSL) checkTypeSwitch(stmt *ast.TypeSwitchStmt, cursor *Cursor) {
941941
w.maybeCheckBlock(stmt, stmt.Body, cursor, CheckTypeSwitch)
942942
}
943943

944-
func (w *WSL) checkCaseTrailingNewline(currentCase ast.Node, body []ast.Stmt, cursor *Cursor) {
944+
func (w *WSL) checkCaseTrailingNewline(body []ast.Stmt, cursor *Cursor) {
945945
if len(body) == 0 {
946946
return
947947
}
@@ -984,20 +984,21 @@ func (w *WSL) checkCaseTrailingNewline(currentCase ast.Node, body []ast.Stmt, cu
984984
nextContentPos := nextCase.Pos()
985985
nextContentLine := nextCaseLine
986986

987-
// Find comments between last statement and next case.
988-
// Comments attach to either currentCase (trailing) or nextCase (leading).
989-
// We only handle zero or one comment group to avoid complex edge cases.
990-
comments := ast.NewCommentMap(w.fset, w.file, w.file.Comments)
991-
992-
// Merge comments from last statement and next case.
987+
// Filter comments between last statement and next case only.
993988
var commentGroups []*ast.CommentGroup
994989

995-
for _, node := range []ast.Node{currentCase, nextCase} {
996-
for _, cg := range comments[node] {
997-
cgLine := w.lineFor(cg.Pos())
998-
if cgLine > lastStmtEndLine && cgLine < nextCaseLine {
999-
commentGroups = append(commentGroups, cg)
1000-
}
990+
for _, cg := range w.file.Comments {
991+
if cg.Pos() >= nextCase.Pos() {
992+
break
993+
}
994+
995+
if cg.Pos() <= lastStmt.End() {
996+
continue
997+
}
998+
999+
cgLine := w.lineFor(cg.Pos())
1000+
if cgLine > lastStmtEndLine && cgLine < nextCaseLine {
1001+
commentGroups = append(commentGroups, cg)
10011002
}
10021003
}
10031004

0 commit comments

Comments
 (0)