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 21b3e02
Show file tree
Hide file tree
Showing 7 changed files with 51 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")

Check warning on line 192 in gnovm/cmd/gno/lint.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/lint.go#L191-L192

Added lines #L191 - L192 were not covered by tests
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)

Check warning on line 201 in gnovm/cmd/gno/lint.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/lint.go#L198-L201

Added lines #L198 - L201 were not covered by tests
}
}

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{}

Check warning on line 641 in gnovm/cmd/gno/test.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/test.go#L641

Added line #L641 was not covered by tests
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

Check warning on line 652 in gnovm/cmd/gno/test.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/test.go#L651-L652

Added lines #L651 - L652 were not covered by tests
}
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)

Check warning on line 673 in gnovm/cmd/gno/test.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/test.go#L672-L673

Added lines #L672 - L673 were not covered by tests
}
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

Check warning on line 1184 in gnovm/pkg/gnolang/nodes.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/nodes.go#L1183-L1184

Added lines #L1183 - L1184 were not covered by tests
}
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)

Check warning on line 1195 in gnovm/pkg/gnolang/nodes.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/nodes.go#L1195

Added line #L1195 was not covered by tests
}
return fset
}

Expand Down
5 changes: 5 additions & 0 deletions gnovm/tests/integ/several-files-multiple-errors/file2.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package main

type{

}
1 change: 1 addition & 0 deletions gnovm/tests/integ/several-files-multiple-errors/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module gno.land/tests/severalerrors
6 changes: 6 additions & 0 deletions gnovm/tests/integ/several-files-multiple-errors/main.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package main

func main() {
for {
_ example
}

0 comments on commit 21b3e02

Please sign in to comment.