Skip to content
Draft
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions examples/gno.land/r/demo/lintavltree/gnomod.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module = "gno.land/r/demo/lintavltree"
gno = "0.9"
36 changes: 36 additions & 0 deletions examples/gno.land/r/demo/lintavltree/lintavltree.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package lintavltree

import "gno.land/p/nt/avl"

var lintest *avl.Tree

func init() {
lintest = avl.NewTree()
lintest.Set("key1", "value1")
lintest.Set("key2", "value2")
lintest.Set("key3", "value3")
}

func JoinValues() string {
result := ""
//nolint
lintest.Iterate("", "", func(key string, value any) bool {
result += value.(string) + ","
return false
})
return result
}

func JoinValuesReverse() string {
result := ""
//nolint
lintest.ReverseIterate("", "", func(key string, value any) bool {
result += value.(string) + ","
return false
})
return result
}

func Render(path string) string {
return "Rendering AVL Tree at path: " + path
}
10 changes: 5 additions & 5 deletions gnovm/cmd/gno/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func execLint(cmd *lintCmd, args []string, io commands.IO) error {
tmpkgType := tmpkg.Type.(gno.MemPackageType)
m2.Store.AddMemPackage(tmpkg, tmpkgType)
return m2.PreprocessFiles(tmpkg.Name, tmpkg.Path,
m2.ParseMemPackageAsType(tmpkg, tmpkgType), true, true, "")
m2.ParseMemPackageAsType(tmpkg, tmpkgType), true, true, "", false, tmpkg)
} else {
return tgetter(pkgPath, store)
}
Expand Down Expand Up @@ -284,23 +284,23 @@ func execLint(cmd *lintCmd, args []string, io commands.IO) error {
// Preprocess fset files (no test files)
tm.Store = newProdGnoStore()
pn, _ := tm.PreprocessFiles(
mpkg.Name, mpkg.Path, fset, false, false, "")
mpkg.Name, mpkg.Path, fset, false, false, "", false, mpkg)
ppkg.AddNormal(pn, fset)
}
{
// LINT STEP 5: PreprocessFiles()
// Preprocess fset files (w/ some *_test.gno).
tm.Store = newTestGnoStore(false)
pn, _ := tm.PreprocessFiles(
mpkg.Name, mpkg.Path, tfset, false, false, "")
mpkg.Name, mpkg.Path, tfset, false, false, "", true, mpkg)
ppkg.AddTest(pn, fset)
}
{
// LINT STEP 5: PreprocessFiles()
// Preprocess _test files (all xxx_test *_test.gno).
tm.Store = newTestGnoStore(true)
pn, _ := tm.PreprocessFiles(
mpkg.Name+"_test", mpkg.Path+"_test", _tests, false, false, "")
mpkg.Name+"_test", mpkg.Path+"_test", _tests, false, false, "", false, mpkg)
ppkg.AddUnderscoreTests(pn, _tests)
}
{
Expand All @@ -318,7 +318,7 @@ func execLint(cmd *lintCmd, args []string, io commands.IO) error {
continue
}
pkgName := string(fset.Files[0].PkgName)
pn, _ := tm.PreprocessFiles(pkgName, pkgPath, fset, false, false, "")
pn, _ := tm.PreprocessFiles(pkgName, pkgPath, fset, false, false, "", false, mpkg)
ppkg.AddFileTest(pn, fset)
}
}
Expand Down
12 changes: 11 additions & 1 deletion gnovm/pkg/gnolang/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ func (m *Machine) RunFiles(fns ...*FileNode) {
// the package getter for tests, e.g. from "gnovm/tests/files/extern/*", or from
// "examples/*".
// - fixFrom: the version of gno to fix from.
func (m *Machine) PreprocessFiles(pkgName, pkgPath string, fset *FileSet, save, withOverrides bool, fixFrom string) (*PackageNode, *PackageValue) {
func (m *Machine) PreprocessFiles(pkgName, pkgPath string, fset *FileSet, save, withOverrides bool, fixFrom string, lintMode bool, mpkg *std.MemPackage) (*PackageNode, *PackageValue) {
if !withOverrides {
if err := checkDuplicates(fset); err != nil {
panic(fmt.Errorf("running package %q: %w", pkgName, err))
Expand All @@ -502,12 +502,22 @@ func (m *Machine) PreprocessFiles(pkgName, pkgPath string, fset *FileSet, save,
if fixFrom != "" {
pn.SetAttribute(ATTR_FIX_FROM, fixFrom)
}
if lintMode {
pn.SetAttribute(ATTR_LINT_MODE, true)
}
pv := pn.NewPackage(nilAllocator)
pb := pv.GetBlock(m.Store)
m.SetActivePackage(pv)
m.Store.SetBlockNode(pn)
PredefineFileSet(m.Store, pn, fset)
for _, fn := range fset.Files {
if lintMode {
mf := mpkg.GetFile(fn.FileName)
if mf != nil {
fn.SetAttribute(ATTR_FILE_SOURCE, mf.Body)
}
}

fn = Preprocess(m.Store, pn, fn).(*FileNode)
// After preprocessing, save blocknodes to store.
SaveBlockNodes(m.Store, fn)
Expand Down
2 changes: 2 additions & 0 deletions gnovm/pkg/gnolang/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ const (
ATTR_PACKAGE_DECL GnoAttribute = "ATTR_PACKAGE_DECL"
ATTR_PACKAGE_PATH GnoAttribute = "ATTR_PACKAGE_PATH" // if name expr refers to package.
ATTR_FIX_FROM GnoAttribute = "ATTR_FIX_FROM" // gno fix this version.
ATTR_LINT_MODE GnoAttribute = "ATTR_LINT_MODE"
ATTR_FILE_SOURCE GnoAttribute = "ATTR_FILE_SOURCE" // source code of file (for linting comments)
)

// Embedded in each Node.
Expand Down
51 changes: 51 additions & 0 deletions gnovm/pkg/gnolang/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,7 @@ func preprocess1(store Store, ctx BlockNode, n Node) Node {
last := ctx
stack := append(make([]BlockNode, 0, 32), last)
ctxpn := packageOf(ctx)
var currentFile *FileNode

// iterate over all nodes recursively
nn := Transcribe(n, func(ns []Node, ftype TransField, index int, n Node, stage TransStage) (Node, TransCtrl) {
Expand Down Expand Up @@ -887,6 +888,7 @@ func preprocess1(store Store, ctx BlockNode, n Node) Node {
// TRANS_BLOCK -----------------------
case *FileNode:
// only for imports.
currentFile = n
pushInitBlock(n, &last, &stack)
{
// This logic supports out-of-order
Expand Down Expand Up @@ -1847,6 +1849,55 @@ func preprocess1(store Store, ctx BlockNode, n Node) Node {
checkOrConvertType(store, last, n, &n.Args[i], spts[i].Type)
}
}
if ctxpn.HasAttribute(ATTR_LINT_MODE) {
if sel, ok := n.Func.(*SelectorExpr); ok {
methodName := string(sel.Sel)
if methodName == "Iterate" || methodName == "ReverseIterate" {
recvT := evalStaticTypeOf(store, last, sel.X)
t := unwrapPointerType(recvT)
dt, ok := t.(*DeclaredType)
if ok && dt.PkgPath == "gno.land/p/nt/avl" && dt.Name == "Tree" {
if len(n.Args) < 3 {
return n, TRANS_CONTINUE
}
startArg := n.Args[0]
endArg := n.Args[1]

isEmptyString := func(s Expr) bool {
if cs, ok := s.(*ConstExpr); ok {
if cs.T.Kind() == StringKind {
sv := cs.V.(StringValue)
return sv == ""
}
}
return false
}

hasNoLint := false
body := currentFile.GetAttribute(ATTR_FILE_SOURCE)
if bodyStr, ok := body.(string); ok {
lines := strings.Split(bodyStr, "\n")
line := n.Pos.Line

prevLineIndex := line - 2
if prevLineIndex >= 0 && prevLineIndex < len(lines) {
prevLine := strings.TrimSpace(lines[prevLineIndex])
if strings.HasPrefix(prevLine, "//nolint") {
hasNoLint = true
}
}
}

startEmpty := isEmptyString(startArg)
endEmpty := isEmptyString(endArg)

if startEmpty && endEmpty && !hasNoLint {
fmt.Printf("warning: calling %s.%s without start or end limit\n", dt.Name, methodName)
}
}
}
}
}
}
default:
panic(fmt.Sprintf(
Expand Down
4 changes: 2 additions & 2 deletions gnovm/pkg/test/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func StoreWithOptions(
return m.PreprocessFiles(
mpkg.Name, mpkg.Path,
m.ParseMemPackageAsType(mpkg, mptype),
save, false, opts.FixFrom)
save, false, opts.FixFrom, false, mpkg)
} else {
return m.RunMemPackage(mpkg, save)
}
Expand Down Expand Up @@ -314,7 +314,7 @@ func loadStdlib(
})
if preprocessOnly {
m2.Store.AddMemPackage(mpkg, mPkgType)
return m2.PreprocessFiles(mpkg.Name, mpkg.Path, m2.ParseMemPackageAsType(mpkg, mPkgType), true, true, "")
return m2.PreprocessFiles(mpkg.Name, mpkg.Path, m2.ParseMemPackageAsType(mpkg, mPkgType), true, true, "", false, mpkg)
}
// TODO: make this work when using gno lint.
return m2.RunMemPackageWithOverrides(mpkg, true)
Expand Down
Loading