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
437 changes: 201 additions & 236 deletions .golangci.yml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Inspired by Rust's [clippy](https://github.com/rust-lang/rust-clippy), tlin aims
## Installation

- Requirements:
- Go: 1.22 or higher
- Go: 1.24 or higher
- latest version of gno
- GNU Make 3.81 or higher (for building)

Expand Down
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/gnolang/tlin

go 1.23.5
go 1.24.0

require (
github.com/fatih/color v1.18.0
Expand All @@ -21,14 +21,14 @@ require (
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/tetratelabs/wazero v1.8.2 // indirect
github.com/tetratelabs/wazero v1.9.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/image v0.24.0 // indirect
golang.org/x/image v0.26.0 // indirect
golang.org/x/mod v0.24.0 // indirect
golang.org/x/sync v0.13.0 // indirect
golang.org/x/sys v0.32.0 // indirect
golang.org/x/term v0.30.0 // indirect
golang.org/x/text v0.22.0 // indirect
golang.org/x/term v0.31.0 // indirect
golang.org/x/text v0.24.0 // indirect
)

require (
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOf
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tetratelabs/wazero v1.8.2 h1:yIgLR/b2bN31bjxwXHD8a3d+BogigR952csSDdLYEv4=
github.com/tetratelabs/wazero v1.8.2/go.mod h1:yAI0XTsMBhREkM/YDAK/zNou3GoiAce1P6+rp/wQhjs=
github.com/tetratelabs/wazero v1.9.0 h1:IcZ56OuxrtaEz8UYNRHBrUa9bYeX9oVY93KspZZBf/I=
github.com/tetratelabs/wazero v1.9.0/go.mod h1:TSbcXCfFP0L2FGkRPxHphadXPjo1T6W+CseNNY7EkjM=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
Expand All @@ -51,6 +53,8 @@ go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/image v0.24.0 h1:AN7zRgVsbvmTfNyqIbbOraYL8mSwcKncEj8ofjgzcMQ=
golang.org/x/image v0.24.0/go.mod h1:4b/ITuLfqYq1hqZcjofwctIhi7sZh2WaCjvsBNjjya8=
golang.org/x/image v0.26.0 h1:4XjIFEZWQmCZi6Wv8BoxsDhRU3RVnLX04dToTDAEPlY=
golang.org/x/image v0.26.0/go.mod h1:lcxbMFAovzpnJxzXS3nyL83K27tmqtKzIJpctK8YO5c=
golang.org/x/mod v0.24.0 h1:ZfthKaKaT4NrhGVZHO1/WDTwGES4De8KtWO0SIbNJMU=
golang.org/x/mod v0.24.0/go.mod h1:IXM97Txy2VM4PJ3gI61r1YEk/gAj6zAHN3AdZt6S9Ww=
golang.org/x/sync v0.13.0 h1:AauUjRAJ9OSnvULf/ARrrVywoJDy0YS2AwQ98I37610=
Expand All @@ -60,9 +64,13 @@ golang.org/x/sys v0.32.0 h1:s77OFDvIQeibCmezSnk/q6iAfkdiQaJi4VzroCFrN20=
golang.org/x/sys v0.32.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
golang.org/x/term v0.30.0 h1:PQ39fJZ+mfadBm0y5WlL4vlM7Sx1Hgf13sMIY2+QS9Y=
golang.org/x/term v0.30.0/go.mod h1:NYYFdzHoI5wRh/h5tDMdMqCqPJZEuNqVR5xJLd/n67g=
golang.org/x/term v0.31.0 h1:erwDkOK1Msy6offm1mOgvspSkslFnIGsFnxOKoufg3o=
golang.org/x/term v0.31.0/go.mod h1:R4BeIy7D95HzImkxGkTW1UQTtP54tio2RyHz7PwK0aw=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=
golang.org/x/text v0.24.0 h1:dd5Bzh4yt5KYA8f9CJHCP4FB4D51c2c6JvN37xJJkJ0=
golang.org/x/text v0.24.0/go.mod h1:L8rBsPeo2pSS+xqN0d5u2ikmjtmoJbDBT1b7nHvFCdU=
golang.org/x/tools v0.32.0 h1:Q7N1vhpkQv7ybVzLFtTjvQya2ewbwNDZzUgfXGqtMWU=
golang.org/x/tools v0.32.0/go.mod h1:ZxrU41P/wAbZD8EDa6dDCa6XfpkhJ7HFMjHJXfBDu8s=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
Expand Down
35 changes: 27 additions & 8 deletions internal/lints/default_golangci.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package lints

import (
"encoding/json"
_ "fmt"
"fmt"
"go/ast"
"go/parser"
"go/token"
"os"
"os/exec"
"path/filepath"

tt "github.com/gnolang/tlin/internal/types"
)
Expand Down Expand Up @@ -40,20 +42,37 @@ type golangciOutput struct {
}

func RunGolangciLint(filename string, _ *ast.File, _ *token.FileSet, severity tt.Severity) ([]tt.Issue, error) {
cmd := exec.Command("golangci-lint", "run", "--config=./.golangci.yml", "--out-format=json", filename)
output, _ := cmd.CombinedOutput()
// find config file in current working directory
wd, err := os.Getwd()
if err != nil {
return nil, fmt.Errorf("failed to get working directory: %v", err)
}

var golangciResult golangciOutput
configPath := filepath.Join(wd, ".golangci.yml")
if _, err := os.Stat(configPath); os.IsNotExist(err) {
return nil, fmt.Errorf("failed to find .golangci.yml file: %v", err)
}

cmd := exec.Command("golangci-lint", "run", "--out-format=json", "--config="+configPath, filename)
output, err := cmd.CombinedOutput()

// @notJoon: Ignore Unmarshal error. We cannot unmarshal the output of golangci-lint
// when source code contains gno package imports (i.e. p/demo, r/demo, std). [07/25/24]
json.Unmarshal(output, &golangciResult)
// If golangci-lint finds issues, it returns a non-zero exit code,
// so we ignore the error if there is json output.
if err != nil && len(output) == 0 {
return nil, fmt.Errorf("failed to run golangci-lint: %v", err)
}

var golangciResult golangciOutput
err = json.Unmarshal(output, &golangciResult)
if err != nil {
return nil, fmt.Errorf("failed to parse golangci-lint output: %v", err)
}

issues := make([]tt.Issue, 0, len(golangciResult.Issues))
for _, gi := range golangciResult.Issues {
issues = append(issues, tt.Issue{
Rule: gi.FromLinter,
Filename: gi.Pos.Filename, // Use the filename from golangci-lint output
Filename: gi.Pos.Filename,
Start: token.Position{Filename: gi.Pos.Filename, Line: gi.Pos.Line, Column: gi.Pos.Column},
End: token.Position{Filename: gi.Pos.Filename, Line: gi.Pos.Line, Column: gi.Pos.Column + 1},
Message: gi.Text,
Expand Down
80 changes: 80 additions & 0 deletions internal/lints/default_golangci_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package lints

import (
"os"
"path/filepath"
"testing"

tt "github.com/gnolang/tlin/internal/types"
)

func TestRunGolangciLint(t *testing.T) {
tmpDir := t.TempDir()
testFile := filepath.Join(tmpDir, "test.go")

code := `package main

import "fmt"

func main() {
var unused int
fmt.Println("Hello, World!")
}
`

if err := os.WriteFile(testFile, []byte(code), 0644); err != nil {
t.Fatalf("failed to create test file: %v", err)
}

// temporary .golangci.yml file for testing
golangciConfig := `linters:
enable:
- unused
- typecheck
`
golangciFile := filepath.Join(tmpDir, ".golangci.yml")
if err := os.WriteFile(golangciFile, []byte(golangciConfig), 0644); err != nil {
t.Fatalf("failed to create golangci.yml file: %v", err)
}

// change working directory to temporary directory
oldDir, err := os.Getwd()
if err != nil {
t.Fatalf("failed to get working directory: %v", err)
}
defer os.Chdir(oldDir)

if err := os.Chdir(tmpDir); err != nil {
t.Fatalf("failed to change working directory: %v", err)
}

node, fset, err := ParseFile(testFile, nil)
if err != nil {
t.Fatalf("failed to parse file: %v", err)
}

issues, err := RunGolangciLint(testFile, node, fset, tt.SeverityWarning)
if err != nil {
t.Fatalf("failed to run golangci-lint: %v", err)
}

if len(issues) == 0 {
t.Error("golangci-lint did not find any issues")
}

for _, issue := range issues {
t.Logf("issue: %+v", issue)
}

foundUnused := false
for _, issue := range issues {
if issue.Rule == "unused" || issue.Rule == "typecheck" {
foundUnused = true
break
}
}

if !foundUnused {
t.Error("unused variable issue was not found")
}
}
9 changes: 2 additions & 7 deletions internal/rule_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,8 @@ type LintRule struct {
name string
}

func (r LintRule) Severity() tt.Severity {
return r.severity
}

func (r LintRule) Name() string {
return r.name
}
func (r LintRule) Severity() tt.Severity { return r.severity }
func (r LintRule) Name() string { return r.name }

func (r LintRule) Check(filename string, node *ast.File, fset *token.FileSet) ([]tt.Issue, error) {
return r.check(filename, node, fset, r.severity)
Expand Down
8 changes: 8 additions & 0 deletions testdata/basic/basic0.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package main

import "fmt"

func main() {
var unused int
fmt.Println("Hello, World!")
}