Skip to content
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

feat: use PackageClauseOnly for parsing pkg name #994

Merged
merged 4 commits into from
Aug 10, 2023
Merged
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
22 changes: 13 additions & 9 deletions gnovm/pkg/gnolang/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package gnolang

import (
"fmt"
"go/parser"
"go/token"
"io/ioutil"
"os"
"path/filepath"
Expand Down Expand Up @@ -1079,14 +1081,16 @@ type FileSet struct {
Files []*FileNode
}

// TODO replace with some more efficient method
// that doesn't involve parsing the whole file.
//
// name could be anything,
// it's only used to generate better error traces.
func PackageNameFromFileBody(name string, body string) Name {
n := MustParseFile(name, body)
return n.PkgName
// PackageNameFromFileBody extracts the package name from the given Gno code body.
// The 'name' parameter is used for better error traces, and 'body' contains the Gno code.
func PackageNameFromFileBody(name, body string) Name {
harry-hov marked this conversation as resolved.
Show resolved Hide resolved
fset := token.NewFileSet()
astFile, err := parser.ParseFile(fset, name, body, parser.PackageClauseOnly)
if err != nil {
panic(err)
zivkovicmilos marked this conversation as resolved.
Show resolved Hide resolved
}

return Name(astFile.Name.Name)
}

// NOTE: panics if package name is invalid.
Expand All @@ -1096,7 +1100,6 @@ func ReadMemPackage(dir string, pkgPath string) *std.MemPackage {
panic(err)
}
memPkg := &std.MemPackage{Path: pkgPath}
var pkgName Name
allowedFiles := []string{ // make case insensitive?
"gno.mod",
"LICENSE",
Expand All @@ -1105,6 +1108,7 @@ func ReadMemPackage(dir string, pkgPath string) *std.MemPackage {
allowedFileExtensions := []string{
".gno",
}
var pkgName Name
for _, file := range files {
if file.IsDir() ||
strings.HasPrefix(file.Name(), ".") ||
Expand Down
Loading