Skip to content

Commit

Permalink
Simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
Villaquiranm committed May 2, 2024
1 parent 704a483 commit baf5cef
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 61 deletions.
45 changes: 34 additions & 11 deletions gnovm/cmd/gno/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"flag"
"fmt"
"go/scanner"
"io"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -68,7 +69,7 @@ func execLint(cfg *lintCfg, args []string, io commands.IO) error {
return fmt.Errorf("list packages from args: %w", err)
}

issueAdder := newIssueAdder(io.Err())
hasError := false

for _, pkgPath := range pkgPaths {
if verbose {
Expand All @@ -78,16 +79,17 @@ func execLint(cfg *lintCfg, args []string, io commands.IO) error {
// Check if 'gno.mod' exists
gnoModPath := filepath.Join(pkgPath, "gno.mod")
if !osm.FileExists(gnoModPath) {
issueAdder.addIssue(lintIssue{
issue := lintIssue{
Code: lintNoGnoMod,
Confidence: 1,
Location: pkgPath,
Msg: "missing 'gno.mod' file",
})
}
fmt.Fprint(io.Err(), issue.String()+"\n")
}

// Handle runtime errors
catchRuntimeError(pkgPath, issueAdder, func() {
hasError = catchRuntimeError(pkgPath, io.Err(), func() {
stdout, stdin, stderr := io.Out(), io.In(), io.Err()
testStore := tests.TestStore(
rootDir, "",
Expand Down Expand Up @@ -132,7 +134,7 @@ func execLint(cfg *lintCfg, args []string, io commands.IO) error {
// TODO: Add more checkers
}

if issueAdder.hasError() && cfg.setExitStatus != 0 {
if hasError && cfg.setExitStatus != 0 {
os.Exit(cfg.setExitStatus)
}

Expand Down Expand Up @@ -161,31 +163,32 @@ 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, issueAdder *issueAdder, action func()) {
func catchRuntimeError(pkgPath string, stderr io.WriteCloser, action func()) (hasError bool) {
defer func() {
// Errors catched here mostly come from: gnovm/pkg/gnolang/preprocess.go
r := recover()
if r == nil {
return
}

hasError = true
switch verr := r.(type) {
case *gno.PreprocessError:
issueAdder.addError(pkgPath, verr)
fmt.Fprint(stderr, issueWithError(pkgPath, verr).String()+"\n")
case scanner.ErrorList:
for _, err := range verr {
issueAdder.addError(pkgPath, err)
fmt.Fprint(stderr, issueWithError(pkgPath, err).String()+"\n")
}
case error:
issueAdder.addError(pkgPath, verr)
fmt.Fprint(stderr, issueWithError(pkgPath, verr).String()+"\n")
case string:
issueAdder.addError(pkgPath, errors.New(verr))
fmt.Fprint(stderr, issueWithError(pkgPath, errors.New(verr)).String()+"\n")
default:
panic(r)
}
}()

action()
return
}

type lintCode int
Expand All @@ -210,3 +213,23 @@ func (i lintIssue) String() string {
// TODO: consider crafting a doc URL based on Code.
return fmt.Sprintf("%s: %s (code=%d).", i.Location, i.Msg, i.Code)
}

func issueWithError(pkgPath string, err error) lintIssue {
var issue lintIssue
issue.Confidence = 1
issue.Code = lintGnoError

parsedError := strings.TrimSpace(err.Error())
parsedError = strings.TrimPrefix(parsedError, pkgPath+"/")

matches := reParseRecover.FindStringSubmatch(parsedError)
if len(matches) == 4 {
sourcepath := guessSourcePath(pkgPath, matches[1])
issue.Location = fmt.Sprintf("%s:%s", sourcepath, matches[2])
issue.Msg = strings.TrimSpace(matches[3])
} else {
issue.Location = fmt.Sprintf("%s:0", filepath.Clean(pkgPath))
issue.Msg = err.Error()
}
return issue
}
12 changes: 7 additions & 5 deletions gnovm/cmd/gno/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"flag"
"fmt"
"io"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -86,7 +87,7 @@ func execRun(cfg *runCfg, args []string, io commands.IO) error {
}

// read files
files, err := parseFiles(args, newIssueAdder(stderr))
files, err := parseFiles(args, stderr)
if err != nil {
return err
}
Expand All @@ -110,15 +111,16 @@ func execRun(cfg *runCfg, args []string, io commands.IO) error {
return nil
}

func parseFiles(fnames []string, issueAdder *issueAdder) ([]*gno.FileNode, error) {
func parseFiles(fnames []string, stderr io.WriteCloser) ([]*gno.FileNode, error) {
files := make([]*gno.FileNode, 0, len(fnames))
var hasError bool
for _, fname := range fnames {
if s, err := os.Stat(fname); err == nil && s.IsDir() {
subFns, err := listNonTestFiles(fname)
if err != nil {
return nil, err
}
subFiles, err := parseFiles(subFns, issueAdder)
subFiles, err := parseFiles(subFns, stderr)
if err != nil {
return nil, err
}
Expand All @@ -130,12 +132,12 @@ func parseFiles(fnames []string, issueAdder *issueAdder) ([]*gno.FileNode, error
return nil, err
}

catchRuntimeError(fname, issueAdder, func() {
hasError = catchRuntimeError(fname, stderr, func() {
files = append(files, gno.MustReadFile(fname))
})
}

if issueAdder.hasError() {
if hasError {
os.Exit(1)
}
return files, nil
Expand Down
7 changes: 3 additions & 4 deletions gnovm/cmd/gno/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,14 +325,13 @@ func gnoTestPkg(
memPkg := gno.ReadMemPackage(pkgPath, gnoPkgPath)

// tfiles, ifiles := gno.ParseMemPackageTests(memPkg)
issueAdder := newIssueAdder(stderr)
var tfiles, ifiles *gno.FileSet

catchRuntimeError(gnoPkgPath, issueAdder, func() {
hasError := catchRuntimeError(gnoPkgPath, stderr, func() {
tfiles, ifiles = parseMemPackageTests(memPkg)
})

if issueAdder.hasError() {
if hasError {
os.Exit(1)
}
testPkgName := getPkgNameFromFileset(ifiles)
Expand Down Expand Up @@ -648,7 +647,7 @@ func parseMemPackageTests(memPkg *std.MemPackage) (tset, itset *gno.FileSet) {
}
n, err := gno.ParseFile(mfile.Name, mfile.Body)
if err != nil {
panic(errors.Wrap(err, "parsing file "+mfile.Name))
panic(err)
}
if n == nil {
panic("should not happen")
Expand Down
41 changes: 0 additions & 41 deletions gnovm/cmd/gno/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,44 +326,3 @@ func prettySize(nb int64) string {
}
return fmt.Sprintf("%.1f%c", float64(nb)/float64(div), "kMGTPE"[exp])
}

type issueAdder struct {
inError bool
stderr io.WriteCloser
}

func newIssueAdder(sdterr io.WriteCloser) *issueAdder {
return &issueAdder{
stderr: sdterr,
}
}

func (i *issueAdder) hasError() bool {
return i.inError
}

func (i *issueAdder) addError(pkgPath string, err error) {
var issue lintIssue
issue.Confidence = 1
issue.Code = lintGnoError

parsedError := strings.TrimSpace(err.Error())
parsedError = strings.TrimPrefix(parsedError, pkgPath+"/")

matches := reParseRecover.FindStringSubmatch(parsedError)
if len(matches) == 4 {
sourcepath := guessSourcePath(pkgPath, matches[1])
issue.Location = fmt.Sprintf("%s:%s", sourcepath, matches[2])
issue.Msg = strings.TrimSpace(matches[3])
} else {
issue.Location = fmt.Sprintf("%s:0", filepath.Clean(pkgPath))
issue.Msg = err.Error()
}

i.addIssue(issue)
}

func (i *issueAdder) addIssue(issue lintIssue) {
fmt.Fprint(i.stderr, issue.String()+"\n")
i.inError = true
}

0 comments on commit baf5cef

Please sign in to comment.