Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #670 #708

Merged
merged 1 commit into from
Jul 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 40 additions & 48 deletions rule/empty-lines.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import (
type EmptyLinesRule struct{}

// Apply applies the rule to given file.
func (*EmptyLinesRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
func (r *EmptyLinesRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
var failures []lint.Failure

onFailure := func(failure lint.Failure) {
failures = append(failures, failure)
}

w := lintEmptyLines{file, file.CommentMap(), onFailure}
w := lintEmptyLines{file, r.commentLines(file.CommentMap(), file), onFailure}
ast.Walk(w, file.AST)
return failures
}
Expand All @@ -30,13 +30,13 @@ func (*EmptyLinesRule) Name() string {

type lintEmptyLines struct {
file *lint.File
cmap ast.CommentMap
cmap map[int]struct{}
onFailure func(lint.Failure)
}

func (w lintEmptyLines) Visit(node ast.Node) ast.Visitor {
block, ok := node.(*ast.BlockStmt)
if !ok {
if !ok || len(block.List) == 0 {
return w
}

Expand All @@ -47,67 +47,59 @@ func (w lintEmptyLines) Visit(node ast.Node) ast.Visitor {
}

func (w lintEmptyLines) checkStart(block *ast.BlockStmt) {
if len(block.List) == 0 {
return
}

start := w.position(block.Lbrace)
blockStart := w.position(block.Lbrace)
firstNode := block.List[0]
firstStmt := w.position(firstNode.Pos())

if w.commentBetween(start, firstNode) {
firstBlockLineIsStmt := firstStmt.Line-(blockStart.Line+1) == 0
_, firstBlockLineIsComment := w.cmap[blockStart.Line+1]
if firstBlockLineIsStmt || firstBlockLineIsComment {
return
}

first := w.position(firstNode.Pos())
if first.Line-start.Line > 1 {
w.onFailure(lint.Failure{
Confidence: 1,
Node: block,
Category: "style",
Failure: "extra empty line at the start of a block",
})
}
w.onFailure(lint.Failure{
Confidence: 1,
Node: block,
Category: "style",
Failure: "extra empty line at the start of a block",
})
}

func (w lintEmptyLines) checkEnd(block *ast.BlockStmt) {
if len(block.List) < 1 {
return
}

end := w.position(block.Rbrace)
blockEnd := w.position(block.Rbrace)
lastNode := block.List[len(block.List)-1]
lastStmt := w.position(lastNode.End())

if w.commentBetween(end, lastNode) {
lastBlockLineIsStmt := (blockEnd.Line-1)-lastStmt.Line == 0
_, lastBlockLineIsComment := w.cmap[blockEnd.Line-1]
if lastBlockLineIsStmt || lastBlockLineIsComment {
return
}

last := w.position(lastNode.End())
if end.Line-last.Line > 1 {
w.onFailure(lint.Failure{
Confidence: 1,
Node: lastNode,
Category: "style",
Failure: "extra empty line at the end of a block",
})
}
w.onFailure(lint.Failure{
Confidence: 1,
Node: block,
Category: "style",
Failure: "extra empty line at the end of a block",
})
}

func (w lintEmptyLines) commentBetween(position token.Position, node ast.Node) bool {
comments := w.cmap.Filter(node).Comments()
if len(comments) == 0 {
return false
}
func (w lintEmptyLines) position(pos token.Pos) token.Position {
return w.file.ToPosition(pos)
}

func (*EmptyLinesRule) commentLines(cmap ast.CommentMap, file *lint.File) map[int]struct{} {
result := map[int]struct{}{}

for _, comment := range comments {
start, end := w.position(comment.Pos()), w.position(comment.End())
if start.Line-position.Line == 1 || position.Line-end.Line == 1 {
return true
for _, comments := range cmap {
for _, comment := range comments {
start := file.ToPosition(comment.Pos())
end := file.ToPosition(comment.End())
for i := start.Line; i <= end.Line; i++ {
result[i] = struct{}{}
}
}
}

return false
}

func (w lintEmptyLines) position(pos token.Pos) token.Position {
return w.file.ToPosition(pos)
return result
}
40 changes: 24 additions & 16 deletions testdata/empty-lines.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ func f1(x *int) bool { // MATCH /extra empty line at the start of a block/
return x > 2
}

func f2(x *int) bool {
return x > 2 // MATCH /extra empty line at the end of a block/
func f2(x *int) bool { // MATCH /extra empty line at the end of a block/
return x > 2

}

func f3(x *int) bool { // MATCH /extra empty line at the start of a block/

return x > 2 // MATCH /extra empty line at the end of a block/
return x > 2 // MATCH:17 /extra empty line at the end of a block/

}

Expand All @@ -36,8 +36,8 @@ func f6(x *int) bool {
// This is fine.
}

func f7(x *int) bool {
return x > 2 // MATCH /extra empty line at the end of a block/
func f7(x *int) bool { // MATCH /extra empty line at the end of a block/
return x > 2
// This is _not_ fine.

}
Expand All @@ -52,8 +52,8 @@ func f8(*int) bool {
}

func f9(*int) bool {
if x > 2 {
return true // MATCH /extra empty line at the end of a block/
if x > 2 { // MATCH /extra empty line at the end of a block/
return true

}

Expand All @@ -75,8 +75,8 @@ func f11(x *int) bool {
}
}

func f12(x *int) bool {
if x > 2 { // MATCH /extra empty line at the end of a block/
func f12(x *int) bool { // MATCH /extra empty line at the end of a block/
if x > 2 {
return true
}

Expand All @@ -89,17 +89,17 @@ func f13(x *int) bool {
}
}

func f14(x *int) bool {
switch { // MATCH /extra empty line at the end of a block/
func f14(x *int) bool { // MATCH /extra empty line at the end of a block/
switch {
case x == 2:
return false
}

}

func f15(x *int) bool {
switch {
case x == 2: // MATCH /extra empty line at the end of a block/
switch { // MATCH /extra empty line at the end of a block/
case x == 2:
return false

}
Expand All @@ -111,16 +111,16 @@ func f16(x *int) bool {
).Execute()
}

func f17(x *int) bool {
return Query( // MATCH /extra empty line at the end of a block/
func f17(x *int) bool { // MATCH /extra empty line at the end of a block/
return Query(
qm("x = ?", x),
).Execute()

}

func f18(x *int) bool {
if true {
if true { // MATCH /extra empty line at the end of a block/
if true {
return true
}

Expand Down Expand Up @@ -160,3 +160,11 @@ func x() {
}
}
}

func ShouldNotWarn() {
// comment

println()

// comment
}