Skip to content

Commit 7ffb19a

Browse files
committed
fix issue 61
1 parent bee6408 commit 7ffb19a

4 files changed

Lines changed: 30 additions & 12 deletions

File tree

lang/golang/parser/utils.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ func NewPackageCache(lruCapacity int) *PackageCache {
7373
}
7474
}
7575

76-
// Get retrieves a value from the cache.
77-
func (pc *PackageCache) Get(key string) (bool, bool) {
76+
// get retrieves a value from the cache.
77+
func (pc *PackageCache) get(key string) (bool, bool) {
7878
pc.lock.Lock()
7979
defer pc.lock.Unlock()
8080
if elem, ok := pc.cache[key]; ok {
@@ -84,8 +84,8 @@ func (pc *PackageCache) Get(key string) (bool, bool) {
8484
return false, false
8585
}
8686

87-
// Set adds a value to the cache.
88-
func (pc *PackageCache) Set(key string, value bool) {
87+
// set adds a value to the cache.
88+
func (pc *PackageCache) set(key string, value bool) {
8989
pc.lock.Lock()
9090
defer pc.lock.Unlock()
9191

@@ -109,19 +109,19 @@ func (pc *PackageCache) Set(key string, value bool) {
109109

110110
// IsStandardPackage 检查一个包是否为标准库,并使用内部缓存。
111111
func (pc *PackageCache) IsStandardPackage(path string) bool {
112-
if isStd, found := pc.Get(path); found {
112+
if isStd, found := pc.get(path); found {
113113
return isStd
114114
}
115115

116116
pkg, err := build.Import(path, "", build.FindOnly)
117117
if err != nil {
118118
// Cannot find the package, assume it's not a standard package
119-
pc.Set(path, false)
119+
pc.set(path, false)
120120
return false
121121
}
122122

123123
isStd := pkg.Goroot
124-
pc.Set(path, isStd)
124+
pc.set(path, isStd)
125125
return isStd
126126
}
127127

lang/golang/parser/utils_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,15 +273,15 @@ func Test_isSysPkg(t *testing.T) {
273273
assert.Equal(t, 2, stdlibCache.lru.Len(), "Cache size should remain at capacity")
274274

275275
// 4. "fmt" 应该在 Cache 中
276-
_, foundFmt := stdlibCache.Get("fmt")
276+
_, foundFmt := stdlibCache.get("fmt")
277277
assert.True(t, foundFmt, "fmt should still be in the cache")
278278

279279
// 5. "net" 应该在 Cache 中
280-
_, foundNet := stdlibCache.Get("net")
280+
_, foundNet := stdlibCache.get("net")
281281
assert.True(t, foundNet, "net should be in the cache")
282282

283283
// 6. "os" 不应该在 Cache 中
284-
_, foundOs := stdlibCache.Get("os")
284+
_, foundOs := stdlibCache.get("os")
285285
assert.False(t, foundOs, "os should have been evicted from the cache")
286286
})
287287
}

lang/golang/writer/write.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
)
3636

3737
var _ uniast.Writer = (*Writer)(nil)
38+
var testPkgPathRegex = regexp.MustCompile(`^(.+?) \[(.+)\]$`)
3839

3940
type Options struct {
4041
// RepoDir string
@@ -81,6 +82,22 @@ func (w *Writer) WriteRepo(repo *uniast.Repository, outDir string) error {
8182
return nil
8283
}
8384

85+
// sanitizePkgPath sanitize the package path, remove the suffix in brackets
86+
func sanitizePkgPath(pkgPath string) string {
87+
matches := testPkgPathRegex.FindStringSubmatch(pkgPath)
88+
// matches should be 3 elements:
89+
// 1. The full string
90+
// 2. The package name
91+
// 3. The content inside the brackets
92+
if len(matches) == 3 {
93+
packageName := matches[1]
94+
testName := matches[2]
95+
if testName == packageName+".test" {
96+
return packageName
97+
}
98+
}
99+
return pkgPath
100+
}
84101
func (w *Writer) WriteModule(repo *uniast.Repository, modPath string, outDir string) error {
85102
mod := repo.Modules[modPath]
86103
if mod == nil {
@@ -94,7 +111,9 @@ func (w *Writer) WriteModule(repo *uniast.Repository, modPath string, outDir str
94111

95112
outdir := filepath.Join(outDir, mod.Dir)
96113
for dir, pkg := range w.visited {
97-
rel := strings.TrimPrefix(dir, mod.Name)
114+
// sanitize the package path
115+
cleanDir := sanitizePkgPath(dir)
116+
rel := strings.TrimPrefix(cleanDir, mod.Name)
98117
pkgDir := filepath.Join(outdir, rel)
99118
if err := os.MkdirAll(pkgDir, 0755); err != nil {
100119
return fmt.Errorf("mkdir %s failed: %v", pkgDir, err)

testdata/asts/localsession_g.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)