-
Notifications
You must be signed in to change notification settings - Fork 441
feat(lint): add render function lint signature #4943
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
base: master
Are you sure you want to change the base?
Changes from 3 commits
2da7eb8
e03f450
d0ceeb3
5ebda05
13bc620
646ddb9
93a3181
3cebf71
f9d6e18
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ import ( | |
| "errors" | ||
| "flag" | ||
| "fmt" | ||
| "go/types" | ||
| goio "io" | ||
| "io/fs" | ||
| "path/filepath" | ||
|
|
@@ -256,7 +257,7 @@ func execLint(cmd *lintCmd, args []string, io commands.IO) error { | |
| if cmd.autoGnomod { | ||
| tcmode = gno.TCLatestRelaxed | ||
| } | ||
| errs := lintTypeCheck(io, dir, mpkg, gno.TypeCheckOptions{ | ||
| tcPkg, errs := lintTypeCheck(io, dir, mpkg, gno.TypeCheckOptions{ | ||
| Getter: newProdGnoStore(), | ||
| TestGetter: newTestGnoStore(true), | ||
| Mode: tcmode, | ||
|
|
@@ -268,6 +269,14 @@ func execLint(cmd *lintCmd, args []string, io commands.IO) error { | |
| return | ||
| } | ||
|
|
||
| // ensure the 'Render' function is correct | ||
| err = lintRenderSignature(io, tcPkg) | ||
| if err != nil { | ||
| // io.ErrPrintln(err) printed above. | ||
| hasError = true | ||
| return | ||
| } | ||
|
|
||
| // Construct machine for testing. | ||
| tm := test.Machine(newProdGnoStore(), goio.Discard, pkgPath, false) | ||
| defer tm.Release() | ||
|
|
@@ -368,10 +377,11 @@ func lintTypeCheck( | |
| mpkg *std.MemPackage, | ||
| opts gno.TypeCheckOptions) ( | ||
| // Results: | ||
| tcPkg *types.Package, | ||
| lerr error, | ||
| ) { | ||
| // gno.TypeCheckMemPackage(mpkg, testStore). | ||
| _, tcErrs := gno.TypeCheckMemPackage(mpkg, opts) | ||
| tcPkg, tcErrs := gno.TypeCheckMemPackage(mpkg, opts) | ||
|
|
||
| // Print errors, and return the first unexpected error. | ||
| errors := multierr.Errors(tcErrs) | ||
|
|
@@ -390,3 +400,59 @@ func lintTargetName(pkg *packages.Package) string { | |
|
|
||
| return tryRelativizePath(pkg.Dir) | ||
| } | ||
|
|
||
| // lintRenderSignature checks if a Render function in the package has the | ||
| // expected signature: func Render(string) string | ||
| // Methods are ignored (e.g. func (t *Type) Render()). | ||
| // Returns error if the signature is incorrect. | ||
| func lintRenderSignature(io commands.IO, pkg *types.Package) error { | ||
| if pkg == nil { | ||
| return nil | ||
| } | ||
|
|
||
| o := pkg.Scope().Lookup("Render") | ||
| if o == nil { | ||
| return nil | ||
| } | ||
|
|
||
| var ( | ||
| stringType = "string" | ||
| errPrintln = func() error { | ||
| err := fmt.Errorf("the 'Render' function signature is incorrect for the '%s' package. the signature must be of the form: func Render(string) string", pkg.Name()) | ||
| fmt.Fprintln(io.Err(), gnoIssue{ | ||
| Code: gnoParserError, | ||
| Msg: err.Error(), | ||
| Confidence: 1, | ||
| Location: pkg.Path(), | ||
| }) | ||
| return err | ||
| } | ||
| isSingleString = func(t *types.Tuple) bool { | ||
| return t != nil && | ||
| t.Len() == 1 && | ||
| t.At(0) != nil && | ||
| t.At(0).Type().String() == stringType | ||
| } | ||
| ) | ||
|
|
||
| fn, ok := o.(*types.Func) | ||
| if !ok { | ||
| return nil | ||
| } | ||
|
|
||
| s, ok := fn.Type().(*types.Signature) | ||
| if !ok { | ||
| return nil | ||
| } | ||
|
|
||
| if s.Recv() != nil { | ||
| return nil | ||
| } | ||
|
|
||
| switch { | ||
| case !isSingleString(s.Params()), !isSingleString(s.Results()): | ||
|
||
| return errPrintln() | ||
| default: | ||
| return nil | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| module = "gno.land/t/render_invalid1" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package main | ||
|
|
||
| // Invalid 'Render' function: wrong parameter (int instead of string) | ||
|
|
||
| func Render(input int) string { | ||
This comment was marked as resolved.
Sorry, something went wrong.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i already have a test that try the number of parameter, i don't think its relevant to add this test. |
||
| return "hello" | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| module = "gno.land/t/render_invalid2" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package main | ||
|
|
||
| // Invalid 'Render' function: wrong return type (int instead of string) | ||
|
|
||
| func Render(input string) int { | ||
| return 9001 | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| module = "gno.land/t/render_invalid3" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package main | ||
|
|
||
| // Invalid 'Render' function: too many parameters (2 instead of 1) | ||
|
|
||
| func Render(input string, extra int) string { | ||
| return input | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| module = "gno.land/t/render_valid1" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package main | ||
|
|
||
| // No 'Render' function is considered as valid | ||
|
|
||
| func Random() {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| module = "gno.land/t/render_valid2" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package main | ||
|
|
||
| type Test struct{} | ||
|
|
||
| // 'Render' method does not need to follow the signature of the pkg scope 'Render' function | ||
|
|
||
| func (t *Test) Render(input int) int { | ||
| return input | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| module = "gno.land/t/render_valid3" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package main | ||
|
|
||
| // Valid 'Render' pkg scope function | ||
|
|
||
| func Render(input string) string { | ||
| return input | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tests don't pass due to lint applying on every package (pure package + realm) instead of only realm.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
93a3181