Skip to content
Merged
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
76 changes: 26 additions & 50 deletions astdiff/astdiff.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,10 @@ func analyzePackage(pkg *packages.Package, result *AnalysisResult) {
fullFileName := pkg.Fset.File(file.Pos()).Name()
baseFileName := filepath.Base(fullFileName)

// 用于存储当前遍历的顶层函数信息
var currentFunc *ast.FuncDecl

ast.Inspect(file, func(n ast.Node) bool {
switch x := n.(type) {
for _, decl := range file.Decls {
switch d := decl.(type) {
case *ast.FuncDecl:
// 记录当前顶层函数
currentFunc = x
funcName := parser.GetFuncOrMethodName(x)
funcName := parser.GetFuncOrMethodName(d)
id := parser.GetObjectID(pkgName, baseFileName, funcName)
result.Objects[id] = struct {
Type string
Expand All @@ -96,41 +91,17 @@ func analyzePackage(pkg *packages.Package, result *AnalysisResult) {
}{
Type: "func",
Package: pkgName,
Position: pkg.Fset.Position(x.Pos()),
Node: x,
Position: pkg.Fset.Position(d.Pos()),
Node: d,
}
case *ast.GenDecl:
// 如果这个声明在函数内部,使用顶层函数的信息
if currentFunc != nil {
funcName := parser.GetFuncOrMethodName(currentFunc)
id := parser.GetObjectID(pkgName, baseFileName, funcName)

// 如果这个ID已经存在,说明我们已经记录过这个函数了
if _, exists := result.Objects[id]; exists {
return true
}

result.Objects[id] = struct {
Type string
Package string
Position token.Position
Node ast.Node
}{
Type: "func",
Package: pkgName,
Position: pkg.Fset.Position(currentFunc.Pos()),
Node: currentFunc,
}
return true
}

// 处理顶层声明
for _, spec := range x.Specs {
// 处理顶层声明 (var, const, type)
for _, spec := range d.Specs {
switch s := spec.(type) {
case *ast.ValueSpec:
case *ast.ValueSpec: // var, const
for _, name := range s.Names {
var objType string
if x.Tok == token.CONST {
if d.Tok == token.CONST {
objType = "const"
} else {
objType = "var"
Expand All @@ -148,7 +119,7 @@ func analyzePackage(pkg *packages.Package, result *AnalysisResult) {
Node: s,
}
}
case *ast.TypeSpec:
case *ast.TypeSpec: // type
id := parser.GetObjectID(pkgName, baseFileName, s.Name.Name)
result.Objects[id] = struct {
Type string
Expand All @@ -157,15 +128,18 @@ func analyzePackage(pkg *packages.Package, result *AnalysisResult) {
Node ast.Node
}{
Type: "type",
Package: pkg.PkgPath,
Package: pkgName,
Position: pkg.Fset.Position(s.Pos()),
Node: s,
}
case *ast.ImportSpec:
continue
default:
panic(fmt.Sprintf("unsupported node type: %T", s))
}
}
}
return true
})
}
}
}

Expand All @@ -189,14 +163,16 @@ func compareResults(old, new *AnalysisResult) *AnalysisResult {
if oldObj, exists := old.Objects[key]; exists {
// 检查包名和类型是否变化
if oldObj.Package != newObj.Package || oldObj.Type != newObj.Type {
result.Changes = append(result.Changes, Change{
Type: ChangeTypeModified,
Package: newObj.Package,
Object: objName,
ObjectType: newObj.Type,
ObjectID: key,
File: fileName,
})
// unreachable code
panic(fmt.Sprintf("package or type changed: %s, %s", oldObj.Package, newObj.Package))
// result.Changes = append(result.Changes, Change{
// Type: ChangeTypeModified,
// Package: newObj.Package,
// Object: objName,
// ObjectType: newObj.Type,
// ObjectID: key,
// File: fileName,
// })
} else {
// 检查对象内容是否变化
if !astNodesEqual(oldObj.Node, newObj.Node) {
Expand Down
8 changes: 4 additions & 4 deletions cmd/impact.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ var impactCmd = &cobra.Command{
}

var (
oldCommit string
newCommit string
repo string // 仓库路径
scope string // 报告的变更范围(all, service)
oldCommit string
newCommit string
repo string // 仓库路径
scope string // 报告的变更范围(all, service)
)

const (
Expand Down
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/spf13/cobra"
)

const Version = "1.0.6"
const Version = "1.0.8"

var versionCmd = &cobra.Command{
Use: "version",
Expand Down
Loading