Attach "package comment is detached" warning to the gap.
It's more directly useful (the obvious fix is to delete the blank line).
diff --git a/lint.go b/lint.go
index dc58a45..275eb0e 100644
--- a/lint.go
+++ b/lint.go
@@ -368,8 +368,15 @@
pkgPos := f.fset.Position(f.f.Package)
if endPos.Line+1 < pkgPos.Line {
// There isn't a great place to anchor this error;
- // the package statement seems as good as any.
- f.errorf(f.f, 0.9, link(ref), category("comments"), "package comment is detached; there should be no blank lines between it and the package statement")
+ // the start of the blank lines between the doc and the package statement
+ // is at least pointing at the location of the problem.
+ pos := token.Position{
+ Filename: endPos.Filename,
+ // Offset not set; it is non-trivial, and doesn't appear to be needed.
+ Line: endPos.Line + 1,
+ Column: 1,
+ }
+ f.pkg.errorfAt(pos, 0.9, link(ref), category("comments"), "package comment is detached; there should be no blank lines between it and the package statement")
return
}
}
diff --git a/lint_test.go b/lint_test.go
index cd81449..90da9b7 100644
--- a/lint_test.go
+++ b/lint_test.go
@@ -9,6 +9,7 @@
import (
"bytes"
"flag"
+ "fmt"
"go/ast"
"go/parser"
"go/printer"
@@ -16,6 +17,7 @@
"io/ioutil"
"path"
"regexp"
+ "strconv"
"strings"
"testing"
@@ -114,17 +116,22 @@
continue
}
if strings.Contains(line, "MATCH") {
- a, b := strings.Index(line, "/"), strings.LastIndex(line, "/")
- if a == -1 || a == b {
- t.Fatalf("Malformed match instruction %q at %v:%d", line, filename, ln)
- }
- pat := line[a+1 : b]
- rx, err := regexp.Compile(pat)
+ rx, err := extractPattern(line)
if err != nil {
- t.Fatalf("Bad match pattern %q at %v:%d: %v", pat, filename, ln, err)
+ t.Fatalf("At %v:%d: %v", filename, ln, err)
+ }
+ matchLine := ln
+ if i := strings.Index(line, "MATCH:"); i >= 0 {
+ // This is a match for a different line.
+ lns := strings.TrimPrefix(line[i:], "MATCH:")
+ lns = lns[:strings.Index(lns, " ")]
+ matchLine, err = strconv.Atoi(lns)
+ if err != nil {
+ t.Fatalf("Bad match line number %q at %v:%d: %v", lns, filename, ln, err)
+ }
}
ins = append(ins, instruction{
- Line: ln,
+ Line: matchLine,
Match: rx,
})
}
@@ -133,6 +140,19 @@
return ins
}
+func extractPattern(line string) (*regexp.Regexp, error) {
+ a, b := strings.Index(line, "/"), strings.LastIndex(line, "/")
+ if a == -1 || a == b {
+ return nil, fmt.Errorf("malformed match instruction %q", line)
+ }
+ pat := line[a+1 : b]
+ rx, err := regexp.Compile(pat)
+ if err != nil {
+ return nil, fmt.Errorf("bad match pattern %q: %v", pat, err)
+ }
+ return rx, nil
+}
+
func render(fset *token.FileSet, x interface{}) string {
var buf bytes.Buffer
if err := printer.Fprint(&buf, fset, x); err != nil {
diff --git a/testdata/pkg-doc5.go b/testdata/pkg-doc5.go
index e173617..cbe5d1e 100644
--- a/testdata/pkg-doc5.go
+++ b/testdata/pkg-doc5.go
@@ -4,4 +4,6 @@
Package foo is pretty sweet.
*/
-package foo // MATCH /package comment.*detached/
+package foo
+
+// MATCH:6 /package comment.*detached/