Skip to content

Commit

Permalink
feat: lint all files in folder before panicking
Browse files Browse the repository at this point in the history
  • Loading branch information
Villaquiranm committed May 26, 2024
1 parent 50bf659 commit 5f97b9f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 17 deletions.
39 changes: 24 additions & 15 deletions gnovm/cmd/gno/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,27 +172,36 @@ func catchRuntimeError(pkgPath string, stderr io.WriteCloser, action func()) (ha
return
}
hasError = true
switch verr := r.(type) {
case *gno.PreprocessError:
err := verr.Unwrap()
fmt.Fprint(stderr, issueFromError(pkgPath, err).String()+"\n")
case scanner.ErrorList:
for _, err := range verr {
fmt.Fprint(stderr, issueFromError(pkgPath, err).String()+"\n")
}
case error:
fmt.Fprint(stderr, issueFromError(pkgPath, verr).String()+"\n")
case string:
fmt.Fprint(stderr, issueFromError(pkgPath, errors.New(verr)).String()+"\n")
default:
panic(r)
}
printRuntimeError(r, pkgPath, stderr)
}()

action()
return
}

func printRuntimeError(r interface{}, pkgPath string, stderr io.WriteCloser) {
switch verr := r.(type) {
case *gno.PreprocessError:
err := verr.Unwrap()
fmt.Fprint(stderr, issueFromError(pkgPath, err).String()+"\n")
case scanner.ErrorList:
for _, err := range verr {
fmt.Fprint(stderr, issueFromError(pkgPath, err).String()+"\n")
}
case error:
fmt.Fprint(stderr, issueFromError(pkgPath, verr).String()+"\n")
case []error:
for _, err := range verr {
// recursive call to handle specifically each error type ex: scanner.ErrorList
printRuntimeError(err, pkgPath, stderr)
}
case string:
fmt.Fprint(stderr, issueFromError(pkgPath, errors.New(verr)).String()+"\n")
default:
panic(r)
}
}

type lintCode int

const (
Expand Down
3 changes: 3 additions & 0 deletions gnovm/cmd/gno/lint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ func TestLintApp(t *testing.T) {
}, {
args: []string{"lint", "--set-exit-status=0", "../../tests/integ/several-lint-errors/main.gno"},
stderrShouldContain: "../../tests/integ/several-lint-errors/main.gno:5: expected ';', found example (code=2).\n../../tests/integ/several-lint-errors/main.gno:6",
}, {
args: []string{"lint", "--set-exit-status=0", "../../tests/integ/several-files-multiple-errors/main.gno"},
stderrShouldContain: "../../tests/integ/several-files-multiple-errors/file2.gno:3: expected 'IDENT', found '{' (code=2).\n../../tests/integ/several-files-multiple-errors/file2.gno:5: expected type, found '}' (code=2).\n../../tests/integ/several-files-multiple-errors/main.gno:5: expected ';', found example (code=2).\n../../tests/integ/several-files-multiple-errors/main.gno:6: expected '}', found 'EOF' (code=2).\n",
}, {
args: []string{"lint", "--set-exit-status=0", "../../tests/integ/run_main/"},
stderrShouldContain: "./../../tests/integ/run_main: missing 'gno.mod' file (code=1).",
Expand Down
7 changes: 6 additions & 1 deletion gnovm/cmd/gno/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,7 @@ func loadTestFuncs(pkgName string, t *testFuncs, tfiles *gno.FileSet) *testFuncs
func parseMemPackageTests(memPkg *std.MemPackage) (tset, itset *gno.FileSet) {
tset = &gno.FileSet{}
itset = &gno.FileSet{}
errors := []error{}
for _, mfile := range memPkg.Files {
if !strings.HasSuffix(mfile.Name, ".gno") {
continue // skip this file.
Expand All @@ -647,7 +648,8 @@ func parseMemPackageTests(memPkg *std.MemPackage) (tset, itset *gno.FileSet) {
}
n, err := gno.ParseFile(mfile.Name, mfile.Body)
if err != nil {
panic(err)
errors = append(errors, err)
continue
}
if n == nil {
panic("should not happen")
Expand All @@ -667,6 +669,9 @@ func parseMemPackageTests(memPkg *std.MemPackage) (tset, itset *gno.FileSet) {
memPkg.Name, memPkg.Name, n.PkgName, mfile))
}
}
if len(errors) > 0 {
panic(errors)
}
return tset, itset
}

Expand Down
7 changes: 6 additions & 1 deletion gnovm/pkg/gnolang/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -1172,14 +1172,16 @@ func ReadMemPackageFromList(list []string, pkgPath string) *std.MemPackage {
// or [ParseFile] returns an error, ParseMemPackage panics.
func ParseMemPackage(memPkg *std.MemPackage) (fset *FileSet) {
fset = &FileSet{}
errors := []error{}
for _, mfile := range memPkg.Files {
if !strings.HasSuffix(mfile.Name, ".gno") ||
endsWith(mfile.Name, []string{"_test.gno", "_filetest.gno"}) {
continue // skip spurious or test file.
}
n, err := ParseFile(mfile.Name, mfile.Body)
if err != nil {
panic(err)
errors = append(errors, err)
continue
}
if memPkg.Name != string(n.PkgName) {
panic(fmt.Sprintf(
Expand All @@ -1189,6 +1191,9 @@ func ParseMemPackage(memPkg *std.MemPackage) (fset *FileSet) {
// add package file.
fset.AddFiles(n)
}
if len(errors) > 0 {
panic(errors)
}
return fset
}

Expand Down

0 comments on commit 5f97b9f

Please sign in to comment.