fix: [go] fix import equal source issue

上级 a91aac1a
......@@ -110,24 +110,7 @@ func BuildMethodCall(codeFunc *CodeFunction, item ast.Stmt, fields []CodeField,
call := BuildCallFromExpr(it.Call, fields, imports, packageName, localVars)
codeFunc.FunctionCalls = append(codeFunc.FunctionCalls, call)
case *ast.AssignStmt:
var vars []CodeProperty
for _, lh := range it.Lhs {
var left string
switch lhx := lh.(type) {
case *ast.Ident:
left = lhx.Name
}
for _, expr := range it.Rhs {
_, _, kind := BuildExpr(expr)
property := CodeProperty{
TypeValue: left,
TypeType: kind,
}
vars = append(vars, property)
}
}
vars := BuildLocalVars(it)
localVars = vars
default:
......@@ -137,6 +120,28 @@ func BuildMethodCall(codeFunc *CodeFunction, item ast.Stmt, fields []CodeField,
return localVars
}
func BuildLocalVars(it *ast.AssignStmt) []CodeProperty {
var vars []CodeProperty
for _, lh := range it.Lhs {
var left string
switch lhx := lh.(type) {
case *ast.Ident:
left = lhx.Name
}
for _, expr := range it.Rhs {
_, _, kind := BuildExpr(expr)
property := CodeProperty{
TypeValue: left,
TypeType: kind,
}
vars = append(vars, property)
}
}
return vars
}
func BuildMethodCallExprStmt(it *ast.ExprStmt, codeFunc *CodeFunction, fields []CodeField, imports []CodeImport, currentPackage string, localVars []CodeProperty) {
switch expr := it.X.(type) {
case *ast.CallExpr:
......@@ -151,7 +156,6 @@ func BuildCallFromExpr(expr *ast.CallExpr, fields []CodeField, imports []CodeImp
_, selector, selName := BuildExpr(expr.Fun.(ast.Expr))
target := ParseTarget(selector, fields, localVars)
packageName := getPackageName(target, imports)
//fmt.Println(packageName)
if packageName == "" {
packageName = currentPackage
}
......@@ -185,6 +189,12 @@ func getPackageName(target string, imports []CodeImport) string {
}
}
}
for _, imp := range imports {
if imp.Source == target {
return target
}
}
return packageName
}
......@@ -200,5 +210,6 @@ func ParseTarget(selector string, fields []CodeField, localVars []CodeProperty)
return field.TypeType
}
}
return ""
return selector
}
......@@ -147,5 +147,6 @@ func main() {
fmt.Println(calls)
g.Expect(calls[0].Package).To(Equal("sync"))
g.Expect(calls[0].Type).To(Equal("sync.Mutex"))
g.Expect(calls[2].Package).To(Equal("fmt"))
g.Expect(len(calls)).To(Equal(3))
}
......@@ -23,7 +23,7 @@
{
"MethodName": "Println",
"NodeName": "fmt",
"Package": "main",
"Package": "fmt",
"Parameters": [
{
"Modifiers": null,
......@@ -40,7 +40,7 @@
"StopLine": 0,
"StopLinePosition": 0
},
"Type": ""
"Type": "fmt"
}
],
"InnerFunctions": null,
......
......@@ -15,7 +15,7 @@
{
"MethodName": "Println",
"NodeName": "fmt",
"Package": "testdata",
"Package": "fmt",
"Parameters": [
{
"Modifiers": null,
......@@ -32,7 +32,7 @@
"StopLine": 0,
"StopLinePosition": 0
},
"Type": ""
"Type": "fmt"
}
],
"InnerFunctions": null,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册