Skip to content

Commit

Permalink
check package import table
Browse files Browse the repository at this point in the history
  • Loading branch information
notJoon committed Jul 25, 2024
1 parent 94ff0b0 commit f0c3914
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 52 deletions.
31 changes: 11 additions & 20 deletions formatter/general.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,19 @@ func (f *GeneralIssueFormatter) Format(
padding := strings.Repeat(" ", len(lineNumberStr)-1)
result.WriteString(lineStyle.Sprintf(" %s|\n", padding))

// line := expandTabs(snippet.Lines[issue.Start.Line-1])
// result.WriteString(lineStyle.Sprintf("%d | ", issue.Start.Line))
// result.WriteString(line + "\n")

// visualColumn := calculateVisualColumn(line, issue.Start.Column)
// result.WriteString(lineStyle.Sprintf(" %s| ", padding))
// result.WriteString(strings.Repeat(" ", visualColumn))
// result.WriteString(messageStyle.Sprintf("^ %s\n\n", issue.Message))

if len(snippet.Lines) > 0 {
line := expandTabs(snippet.Lines[lineIndex])
result.WriteString(lineStyle.Sprintf("%d | ", issue.Start.Line))
result.WriteString(line + "\n")
line := expandTabs(snippet.Lines[lineIndex])
result.WriteString(lineStyle.Sprintf("%d | ", issue.Start.Line))
result.WriteString(line + "\n")

visualColumn := calculateVisualColumn(line, issue.Start.Column)
result.WriteString(lineStyle.Sprintf(" %s| ", padding))
result.WriteString(strings.Repeat(" ", visualColumn))
result.WriteString(messageStyle.Sprintf("^ %s\n\n", issue.Message))
} else {
result.WriteString(messageStyle.Sprintf("Unable to display line. File might be empty.\n"))
result.WriteString(messageStyle.Sprintf("Issue: %s\n\n", issue.Message))
}
visualColumn := calculateVisualColumn(line, issue.Start.Column)
result.WriteString(lineStyle.Sprintf(" %s| ", padding))
result.WriteString(strings.Repeat(" ", visualColumn))
result.WriteString(messageStyle.Sprintf("^ %s\n\n", issue.Message))
} else {
result.WriteString(messageStyle.Sprintf("Unable to display line. File might be empty.\n"))
result.WriteString(messageStyle.Sprintf("Issue: %s\n\n", issue.Message))
}

return result.String()
}
Expand Down
6 changes: 3 additions & 3 deletions internal/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (

// Engine manages the linting process.
type Engine struct {
SymbolTable *SymbolTable
rules []LintRule
ignoredRules map[string]bool
SymbolTable *SymbolTable
rules []LintRule
ignoredRules map[string]bool
}

// NewEngine creates a new lint engine.
Expand Down
57 changes: 29 additions & 28 deletions internal/lints/gno_analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"go/ast"
"go/parser"
"go/token"
"os"
"path/filepath"
"strings"

Expand Down Expand Up @@ -33,10 +32,12 @@ type (
FileMap map[string]*ast.File
)

// Package represents a Go/Gno package with its name and files.
type Package struct {
Name string
Files FileMap
// PackageInfo represents a Go/Gno package with its name and files.
type PackageInfo struct {
Name string
Files FileMap
Imports map[string]*Dependency
PkgTable map[string]string
}

// DetectGnoPackageImports analyzes the given file for Gno package imports and returns any issues found.
Expand All @@ -58,36 +59,36 @@ func DetectGnoPackageImports(filename string) ([]tt.Issue, error) {
}

// parses all gno files and collect their imports and usage.
func analyzePackage(dir string) (*Package, Dependencies, error) {
pkg := &Package{
Files: make(FileMap),
func analyzePackage(dir string) (*PackageInfo, Dependencies, error) {
pkg := &PackageInfo{
Files: make(FileMap),
Imports: make(map[string]*Dependency),
PkgTable: make(map[string]string),
}
deps := make(Dependencies)

files, err := filepath.Glob(filepath.Join(dir, "*.gno"))
files, err := filepath.Glob(filepath.Join(dir, "*.{go,gno}"))
if err != nil {
return nil, nil, err
}

// 1. Parse all file contents and collect dependencies
for _, file := range files {
f, err := parseFile(file)
f, err := parser.ParseFile(token.NewFileSet(), file, nil, 0)
if err != nil {
return nil, nil, err
}

pkg.Files[file] = f
if pkg.Name == "" {
pkg.Name = f.Name.Name
}

for _, imp := range f.Imports {
impPath := strings.Trim(imp.Path.Value, `"`)
if _, exists := deps[impPath]; !exists {
deps[impPath] = &Dependency{
ImportPath: impPath,
IsGno: isGnoPackage(impPath),
path := strings.Trim(imp.Path.Value, `"`)
if _, exists := pkg.Imports[path]; !exists {
pkg.Imports[path] = &Dependency{
ImportPath: path,
IsUsed: false,
IsGno: isGnoPackage(path),
IsIgnored: imp.Name != nil && imp.Name.Name == "_",
}
}
Expand All @@ -98,6 +99,16 @@ func analyzePackage(dir string) (*Package, Dependencies, error) {
for _, file := range pkg.Files {
ast.Inspect(file, func(n ast.Node) bool {
switch x := n.(type) {
case *ast.ImportSpec:
// add imported symbols to symbol table
path := strings.Trim(x.Path.Value, `"`)
var name string
if x.Name != nil {
name = x.Name.Name
} else {
name = filepath.Base(path)
}
pkg.PkgTable[name] = path
case *ast.SelectorExpr:
if ident, ok := x.X.(*ast.Ident); ok {
for _, imp := range file.Imports {
Expand All @@ -116,7 +127,7 @@ func analyzePackage(dir string) (*Package, Dependencies, error) {
return pkg, deps, nil
}

func runGnoPackageLinter(pkg *Package, deps Dependencies) []tt.Issue {
func runGnoPackageLinter(pkg *PackageInfo, deps Dependencies) []tt.Issue {
var issues []tt.Issue

for _, file := range pkg.Files {
Expand Down Expand Up @@ -151,16 +162,6 @@ func isGnoPackage(importPath string) bool {
return strings.HasPrefix(importPath, GNO_PKG_PREFIX) || importPath == GNO_STD_PACKAGE
}

func parseFile(filename string) (*ast.File, error) {
content, err := os.ReadFile(filename)
if err != nil {
return nil, err
}

fset := token.NewFileSet()
return parser.ParseFile(fset, filename, content, parser.ParseComments)
}

func getLastPart(path string) string {
parts := strings.Split(path, "/")
return parts[len(parts)-1]
Expand Down
2 changes: 1 addition & 1 deletion internal/lints/loop_allocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func DetectLoopAllocation(filename string) ([]tt.Issue, error) {
case *ast.CallExpr:
if isAllocationFunction(innerNode) {
issues = append(issues, tt.Issue{
Rule: "loop-allocation",
Rule: "loop-allocation",
Message: "Potential unnecessary allocation inside loop",
Start: fset.Position(innerNode.Pos()),
End: fset.Position(innerNode.End()),
Expand Down
9 changes: 9 additions & 0 deletions testdata/pkg/pkg1.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

import (
"strings"
)

func main() {
strings.Contains("foo", "o")
}

0 comments on commit f0c3914

Please sign in to comment.