fix: [go] fix import equal source issue

上级 a91aac1a
......@@ -110,6 +110,17 @@ 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:
vars := BuildLocalVars(it)
localVars = vars
default:
fmt.Fprintf(output, "methodCall %s\n", reflect.TypeOf(it))
}
return localVars
}
func BuildLocalVars(it *ast.AssignStmt) []CodeProperty {
var vars []CodeProperty
for _, lh := range it.Lhs {
var left string
......@@ -128,13 +139,7 @@ func BuildMethodCall(codeFunc *CodeFunction, item ast.Stmt, fields []CodeField,
vars = append(vars, property)
}
}
localVars = vars
default:
fmt.Fprintf(output, "methodCall %s\n", reflect.TypeOf(it))
}
return localVars
return vars
}
func BuildMethodCallExprStmt(it *ast.ExprStmt, codeFunc *CodeFunction, fields []CodeField, imports []CodeImport, currentPackage string, localVars []CodeProperty) {
......@@ -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.
先完成此消息的编辑!
想要评论请 注册