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

feat: ability to ignore lint diagnostics in realms #1450

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
74 changes: 65 additions & 9 deletions gnovm/cmd/gno/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"errors"
"flag"
"fmt"
"strconv"
"go/parser"
"go/token"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -68,7 +71,57 @@ func execLint(cfg *lintCfg, args []string, io commands.IO) error {
}

hasError := false
addIssue := func(issue lintIssue) {
addIssue := func(issue lintIssue, checkComments bool) {
if (checkComments) {
Comment on lines +74 to +75
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • I don't think there is a need for checkComments? (ie. yes, always check comments)
  • Why is it currently set to false in both instances we call addIssue?

fset := token.NewFileSet()

splittedString := strings.Split(issue.Location, ":")
if len(splittedString) < 2 {
return
}

location := splittedString[0]
line, err := strconv.Atoi(splittedString[1])
if err != nil {
return
}
content, err := osm.ReadFile(location)
if err != nil {
return
}

astf, err := parser.ParseFile(fset, "", content, parser.ParseComments)
if err != nil {
return
}

for _, commentGroup := range astf.Comments {
currentPos := fset.Position(commentGroup.Pos()).Line
words := strings.FieldsFunc(commentGroup.Text(), func(r rune) bool {
return r == ' ' || r == ':' || r == ','
})
if len(words) == 0 {
continue
}

if !(strings.Contains(words[0], "nolint") && currentPos == line) {
continue
}

if len(words) > 1 {
for _, word := range words {
if word == issue.Code.rule {
// Found!
return
}
}
} else {
return
}
}
}


hasError = true
fmt.Fprint(io.Err(), issue.String()+"\n")
}
Expand All @@ -86,7 +139,7 @@ func execLint(cfg *lintCfg, args []string, io commands.IO) error {
Confidence: 1,
Location: pkgPath,
Msg: "missing 'gno.mod' file",
})
}, false)
}

// Handle runtime errors
Expand Down Expand Up @@ -164,7 +217,7 @@ func guessSourcePath(pkg, source string) string {
// XXX: Ideally, error handling should encapsulate location details within a dedicated error type.
var reParseRecover = regexp.MustCompile(`^([^:]+):(\d+)(?::\d+)?:? *(.*)$`)

func catchRuntimeError(pkgPath string, addIssue func(issue lintIssue), action func()) {
func catchRuntimeError(pkgPath string, addIssue func(issue lintIssue, checkComments bool), action func()) {
defer func() {
// Errors catched here mostly come from: gnovm/pkg/gnolang/preprocess.go
r := recover()
Expand Down Expand Up @@ -201,18 +254,21 @@ func catchRuntimeError(pkgPath string, addIssue func(issue lintIssue), action fu
issue.Msg = err.Error()
}

addIssue(issue)
addIssue(issue, false)
}()

action()
}

type lintCode int
type lintCode struct {
code int
rule string
}
Comment on lines +263 to +266
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we're already having rich information when we use addIssue

I suggest you keep this as constants and use stringer to generate a String method for lintCode. See gnovm/Makefile @ _dev.stringer for where to add this.


const (
lintUnknown lintCode = 0
lintNoGnoMod lintCode = iota
lintGnoError
var (
lintUnknown = lintCode{0, "unknown"}
lintNoGnoMod = lintCode{1, "NoGnoMod"}
lintGnoError = lintCode{2, "GnoError"}

// TODO: add new linter codes here.
)
Expand Down
Loading